| 1 | # SPDX-License-Identifier: MIT |
| 2 | # |
| 3 | # Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com> |
| 4 | # |
| 5 | |
| 6 | import sys |
| 7 | import argparse |
| 8 | import unittest |
| 9 | |
| 10 | from tap import TAPTestRunner |
| 11 | |
| 12 | if __name__ == "__main__": |
| 13 | argparser = argparse.ArgumentParser() |
| 14 | argparser.add_argument( |
| 15 | "-f", "--failfast", help="Stop on first fail or error", action="store_true" |
| 16 | ) |
| 17 | |
| 18 | argparser.add_argument( |
| 19 | "start_dir", help="Base directory where to search for tests", type=str |
| 20 | ) |
| 21 | |
| 22 | mut_exclu_group = argparser.add_mutually_exclusive_group(required=True) |
| 23 | |
| 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 | ) |
| 30 | |
| 31 | mut_exclu_group.add_argument( |
| 32 | "-t", |
| 33 | "--test-case", |
| 34 | help="Run a specific test module name, test class " |
| 35 | "name, or test method name " |
| 36 | "(e.g. test_event.EventTestCase.test_clock_value)", |
| 37 | type=str, |
| 38 | ) |
| 39 | |
| 40 | args = argparser.parse_args() |
| 41 | |
| 42 | loader = unittest.TestLoader() |
| 43 | |
| 44 | start_dir = args.start_dir |
| 45 | pattern = args.pattern |
| 46 | failfast = args.failfast |
| 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) |
| 59 | |
| 60 | if tests.countTestCases() < 1: |
| 61 | print("No tests matching '%s' found in '%s'" % (pattern, start_dir)) |
| 62 | sys.exit(1) |
| 63 | |
| 64 | runner = TAPTestRunner(failfast=failfast) |
| 65 | runner.set_stream(True) |
| 66 | runner.set_format("{method_name}") |
| 67 | sys.exit(0 if runner.run(tests).wasSuccessful() else 1) |