File: testrunner.py

package info (click to toggle)
babeltrace2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,660 kB
  • sloc: cpp: 106,162; ansic: 78,276; python: 27,115; sh: 9,053; makefile: 1,807; xml: 46
file content (67 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# SPDX-License-Identifier: MIT
#
# Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
#

import sys
import argparse
import unittest

from tap import TAPTestRunner

if __name__ == "__main__":
    argparser = argparse.ArgumentParser()
    argparser.add_argument(
        "-f", "--failfast", help="Stop on first fail or error", action="store_true"
    )

    argparser.add_argument(
        "start_dir", help="Base directory where to search for tests", type=str
    )

    mut_exclu_group = argparser.add_mutually_exclusive_group(required=True)

    mut_exclu_group.add_argument(
        "-p",
        "--pattern",
        help="Glob-style pattern of test files to run " "(e.g. test_event*.py)",
        type=str,
    )

    mut_exclu_group.add_argument(
        "-t",
        "--test-case",
        help="Run a specific test module name, test class "
        "name, or test method name "
        "(e.g. test_event.EventTestCase.test_clock_value)",
        type=str,
    )

    args = argparser.parse_args()

    loader = unittest.TestLoader()

    start_dir = args.start_dir
    pattern = args.pattern
    failfast = args.failfast
    test_case = args.test_case

    if test_case:
        sys.path.append(start_dir)
        tests = loader.loadTestsFromName(test_case)
    elif pattern:
        tests = loader.discover(start_dir, pattern)
    else:
        # This will never happen because the mutual exclusion group has the
        # `required` parameter set to True. So one or the other must be set or
        # else it will fail to parse.
        sys.exit(1)

    if tests.countTestCases() < 1:
        print("No tests matching '%s' found in '%s'" % (pattern, start_dir))
        sys.exit(1)

    runner = TAPTestRunner(failfast=failfast)
    runner.set_stream(True)
    runner.set_format("{method_name}")
    sys.exit(0 if runner.run(tests).wasSuccessful() else 1)