Debian Codegraph @

tests/__init__.py

raw
1 # SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB)
2 # Copyright (c) 2019 Mellanox Technologies, Inc . All rights reserved. See COPYING file
3
4 import importlib
5 import os
6
7 from args_parser import parser
8
9 # Load every test as a module in the system so that unittest's loader can find it
10 def _load_tests():
11 res = []
12 for fn in sorted(os.listdir(os.path.dirname(__file__))):
13 if fn.endswith(".py") and fn.startswith("test_"):
14 m = importlib.import_module("." + os.path.basename(fn)[:-3], __name__)
15 res.append(m)
16 return res
17 __test_modules__ = _load_tests()
18
19 # unittest -v prints names like 'tests.test_foo', but it always starts
20 # searching from the tests module, adding the name 'tests.test' lets the user
21 # specify the same test name from logging on the command line to trivially run
22 # a single test.
23 tests = importlib.import_module(".", __name__)
24
25
26 def _show_tests_and_exit(loader, standard_tests, pattern):
27 """
28 Prints the full test names that are loaded with the current modules via
29 loadTestsFromModule protocol, without modifying standard_tests.
30 """
31 for mod in __test_modules__:
32 for test in loader.loadTestsFromModule(mod, pattern=pattern):
33 for test_case in test:
34 print(test_case.id())
35 return standard_tests
36
37
38 def load_tests(loader, standard_tests, pattern):
39 """Implement the loadTestsFromModule protocol"""
40 if parser.args['list_tests']:
41 return _show_tests_and_exit(loader, standard_tests, pattern)
42 for mod in __test_modules__:
43 standard_tests.addTests(loader.loadTestsFromModule(mod, pattern=pattern))
44 return standard_tests