Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
9cf643d1 PP |
2 | # |
3 | # Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com> | |
4 | # | |
9cf643d1 PP |
5 | |
6 | from tap import TAPTestRunner | |
7 | import unittest | |
8 | import sys | |
274a82ad | 9 | import argparse |
9cf643d1 PP |
10 | |
11 | ||
12 | if __name__ == '__main__': | |
274a82ad | 13 | argparser = argparse.ArgumentParser() |
cfbd7cf3 FD |
14 | argparser.add_argument( |
15 | '-f', '--failfast', help='Stop on first fail or error', action='store_true' | |
16 | ) | |
a818a617 | 17 | |
cfbd7cf3 FD |
18 | argparser.add_argument( |
19 | 'start_dir', help='Base directory where to search for tests', type=str | |
20 | ) | |
a818a617 FD |
21 | |
22 | mut_exclu_group = argparser.add_mutually_exclusive_group(required=True) | |
23 | ||
cfbd7cf3 FD |
24 | mut_exclu_group.add_argument( |
25 | '-p', | |
26 | '--pattern', | |
27 | help='Glob-style pattern of test files to run ' '(e.g. test_event*.py)', | |
28 | type=str, | |
29 | ) | |
a818a617 | 30 | |
cfbd7cf3 FD |
31 | mut_exclu_group.add_argument( |
32 | '-t', | |
33 | '--test-case', | |
34 | help='Run a specfic test module name, test class ' | |
35 | 'name, or test method name ' | |
36 | '(e.g. test_event.EventTestCase.test_clock_value)', | |
37 | type=str, | |
38 | ) | |
274a82ad SM |
39 | |
40 | args = argparser.parse_args() | |
41 | ||
9cf643d1 | 42 | loader = unittest.TestLoader() |
b83084f5 | 43 | |
274a82ad SM |
44 | start_dir = args.start_dir |
45 | pattern = args.pattern | |
46 | failfast = args.failfast | |
a818a617 FD |
47 | test_case = args.test_case |
48 | ||
49 | if test_case: | |
50 | sys.path.append(start_dir) | |
51 | tests = loader.loadTestsFromName(test_case) | |
52 | elif pattern: | |
53 | tests = loader.discover(start_dir, pattern) | |
54 | else: | |
55 | # This will never happen because the mutual exclusion group has the | |
56 | # `required` parameter set to True. So one or the other must be set or | |
57 | # else it will fail to parse. | |
58 | sys.exit(1) | |
b83084f5 | 59 | |
644e0364 MJ |
60 | if tests.countTestCases() < 1: |
61 | print("No tests matching '%s' found in '%s'" % (pattern, start_dir)) | |
62 | sys.exit(1) | |
63 | ||
274a82ad | 64 | runner = TAPTestRunner(failfast=failfast) |
9cf643d1 PP |
65 | runner.set_stream(True) |
66 | runner.set_format('{method_name}') | |
67 | sys.exit(0 if runner.run(tests).wasSuccessful() else 1) |