2 # -*- coding: utf-8 -*-
5 # By Gerald Combs <gerald@wireshark.org>
7 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
9 # SPDX-License-Identifier: GPL-2.0-or-later
11 '''Main test script'''
14 # - Avoid printing Python tracebacks when we assert? It looks like we'd need
15 # to override unittest.TextTestResult.addFailure().
16 # - Switch to Python 3 only? [Windows, Linux, macOS] x [Python 2, Python 3]
18 # - Remove BIN_PATH/hosts via config.tearDownHostFiles + case_name_resolution.tearDownClass?
27 def find_test_ids(suite, all_ids):
28 if hasattr(suite, '__iter__'):
30 find_test_ids(s, all_ids)
32 all_ids.append(suite.id())
34 def dump_failed_output(suite):
35 if hasattr(suite, '__iter__'):
37 dump_failures = getattr(s, 'dump_failures', None)
44 parser = argparse.ArgumentParser(description='Wireshark unit tests')
45 cap_group = parser.add_mutually_exclusive_group()
46 cap_group.add_argument('-e', '--enable-capture', action='store_true', help='Enable capture tests')
47 cap_group.add_argument('-E', '--disable-capture', action='store_true', help='Disable capture tests')
48 cap_group.add_argument('-i', '--capture-interface', nargs=1, default=None, help='Capture interface index or name')
49 parser.add_argument('-p', '--program-path', nargs=1, default=os.path.curdir, help='Path to Wireshark executables.')
50 list_group = parser.add_mutually_exclusive_group()
51 list_group.add_argument('-l', '--list', action='store_true', help='List tests. One of "all" or a full or partial test name.')
52 list_group.add_argument('--list-suites', action='store_true', help='List all suites.')
53 list_group.add_argument('--list-cases', action='store_true', help='List all suites and cases.')
54 parser.add_argument('-v', '--verbose', action='store_const', const=2, default=1, help='Verbose tests.')
55 parser.add_argument('tests_to_run', nargs='*', metavar='test', default=['all'], help='Tests to run. One of "all" or a full or partial test name. Default is "all".')
56 args = parser.parse_args()
58 if args.enable_capture:
59 config.setCanCapture(True)
60 elif args.disable_capture:
61 config.setCanCapture(False)
63 if args.capture_interface:
64 config.setCaptureInterface(args.capture_interface[0])
66 all_tests = unittest.defaultTestLoader.discover(os.path.dirname(__file__), pattern='suite_*.py')
69 find_test_ids(all_tests, all_ids)
73 for ttr in args.tests_to_run:
78 if ttrl in tid.lower():
82 print('No tests found. You asked for:\n ' + '\n '.join(args.tests_to_run))
87 print('\n'.join(run_ids))
93 rparts = rid.split('.')
95 print('\n'.join(list(suites)))
101 rparts = rid.split('.')
102 cases |= {'.'.join(rparts[:2])}
103 print('\n'.join(list(cases)))
106 program_path = args.program_path[0]
107 if not config.setProgramPath(program_path):
108 print('One or more required executables not found at {}\n'.format(program_path))
112 run_suite = unittest.defaultTestLoader.loadTestsFromNames(run_ids)
113 runner = unittest.TextTestRunner(verbosity=args.verbose)
114 test_result = runner.run(run_suite)
116 dump_failed_output(run_suite)
118 if test_result.errors:
121 if test_result.failures:
124 if __name__ == '__main__':