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().
24 # Required to make fixtures available to tests!
27 _all_test_groups = None
29 @fixtures.fixture(scope='session')
30 def all_test_groups():
31 return _all_test_groups
33 def find_test_ids(suite, all_ids):
34 if hasattr(suite, '__iter__'):
36 find_test_ids(s, all_ids)
38 all_ids.append(suite.id())
40 def dump_failed_output(suite):
41 if hasattr(suite, '__iter__'):
43 dump_failures = getattr(s, 'dump_failures', None)
50 if sys.version_info[0] < 3:
51 print("Unit tests require Python 3")
54 parser = argparse.ArgumentParser(description='Wireshark unit tests')
55 cap_group = parser.add_mutually_exclusive_group()
56 cap_group.add_argument('-E', '--disable-capture', action='store_true', help='Disable capture tests')
57 cap_group.add_argument('-i', '--capture-interface', help='Capture interface index or name')
58 parser.add_argument('-p', '--program-path', default=os.path.curdir, help='Path to Wireshark executables.')
59 list_group = parser.add_mutually_exclusive_group()
60 list_group.add_argument('-l', '--list', action='store_true', help='List tests. One of "all" or a full or partial test name.')
61 list_group.add_argument('--list-suites', action='store_true', help='List all suites.')
62 list_group.add_argument('--list-groups', action='store_true', help='List all suites and groups.')
63 list_group.add_argument('--list-cases', action='store_true', help='List all suites, groups, and cases.')
64 parser.add_argument('-v', '--verbose', action='store_const', const=2, default=1, help='Verbose tests.')
65 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".')
66 args = parser.parse_args()
68 all_tests = unittest.defaultTestLoader.discover(os.path.dirname(__file__), pattern='suite_*')
71 find_test_ids(all_tests, all_ids)
75 for ttr in args.tests_to_run:
80 if ttrl in tid.lower():
84 print('No tests found. You asked for:\n ' + '\n '.join(args.tests_to_run))
89 print('\n'.join(run_ids))
94 aparts = aid.split('.')
95 all_suites |= {aparts[0]}
96 all_suites = sorted(all_suites)
100 aparts = aid.split('.')
101 if aparts[1].startswith('group_'):
102 all_groups |= {'.'.join(aparts[:2])}
104 all_groups |= {aparts[0]}
105 all_groups = sorted(all_groups)
106 global _all_test_groups
107 _all_test_groups = all_groups
110 print('\n'.join(all_suites))
114 print('\n'.join(all_groups))
120 rparts = rid.split('.')
121 cases |= {'.'.join(rparts[:2])}
122 print('\n'.join(list(cases)))
125 if codecs.lookup(sys.stdout.encoding).name != 'utf-8':
127 sys.stderr.write('Warning: Output encoding is {0} and not utf-8.\n'.format(sys.stdout.encoding))
128 sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout.buffer, 'backslashreplace')
129 sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr.buffer, 'backslashreplace')
131 run_suite = unittest.defaultTestLoader.loadTestsFromNames(run_ids)
132 runner = unittest.TextTestRunner(verbosity=args.verbose)
133 # for unittest compatibility (not needed with pytest)
134 fixtures_ws.fixtures.create_session(args)
136 test_result = runner.run(run_suite)
138 # for unittest compatibility (not needed with pytest)
139 fixtures_ws.fixtures.destroy_session()
141 dump_failed_output(run_suite)
143 if test_result.errors:
146 if test_result.failures:
149 if __name__ == '__main__':