游雁
2024-03-27 9b4e9cc8a0311e5243d69b73ed073e7ea441982e
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
#!/usr/bin/env python
 
import argparse
import os
import sys
import unittest
from fnmatch import fnmatch
 
 
def gather_test_cases(test_dir, pattern, list_tests):
    case_list = []
    for dirpath, dirnames, filenames in os.walk(test_dir):
        for file in filenames:
            if fnmatch(file, pattern):
                case_list.append(file)
 
    test_suite = unittest.TestSuite()
 
    for case in case_list:
        test_case = unittest.defaultTestLoader.discover(start_dir=test_dir, pattern=case)
        test_suite.addTest(test_case)
        if hasattr(test_case, '__iter__'):
            for subcase in test_case:
                if list_tests:
                    print(subcase)
        else:
            if list_tests:
                print(test_case)
    return test_suite
 
 
def main(args):
    runner = unittest.TextTestRunner()
    test_suite = gather_test_cases(os.path.abspath(args.test_dir), args.pattern, args.list_tests)
    if not args.list_tests:
        result = runner.run(test_suite)
        if len(result.failures) > 0:
            sys.exit(len(result.failures))
        if len(result.errors) > 0:
            sys.exit(len(result.errors))
 
 
if __name__ == '__main__':
    parser = argparse.ArgumentParser('test runner')
    parser.add_argument('--list_tests', action='store_true', help='list all tests')
    parser.add_argument('--pattern', default='test_*.py', help='test file pattern')
    parser.add_argument('--test_dir', default='tests', help='directory to be tested')
    parser.add_argument('--disable_profile', action='store_true', help='disable profiling')
    args = parser.parse_args()
    print(f'working dir: {os.getcwd()}')
    main(args)