-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
81 lines (59 loc) · 1.82 KB
/
tests.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# ______ __
# /_ __/__ _____/ /____ _____
# / / / _ \/ ___/ __/ _ \/ ___/
# / / / __(__ ) /_/ __/ /
# /_/ \___/____/\__/\___/_/
"""General purpose tester for all Region-V code.
Each test should be in a class that extends
```unittest.TestCase```. This module should then be imported in some form by
this file.
This file will recursively search all imported modules for
```unittest.TestCase``` objects, and adds them to a test suite. Only modules
with source in the ```r5-2019``` directory will be tested.
"""
import os
import unittest
from types import ModuleType
__base_name = os.path.basename(os.getcwd())
def get_submodules(module):
"""Get submodules for a module
Parameters
----------
module : python module
Module to add recursively. If ```module``` has any other modules
in it's ```__dict__```, those modules are also added.
Returns
module[]
List of found modules.
"""
modules = [module]
for key, value in module.__dict__.items():
if (
isinstance(value, ModuleType) and
hasattr(value, "__file__") and
__base_name in value.__file__):
modules += get_submodules(value)
return modules
def run_tests(modules):
"""Run tests on a set of modules.
Parameters
----------
modules : module[]
LIst of modules to test
"""
loader = unittest.TestLoader()
suite = unittest.TestSuite()
modlist = []
for module in modules:
modlist += get_submodules(module)
for mod in modlist:
suite.addTests(loader.loadTestsFromModule(mod))
runner = unittest.TextTestRunner(verbosity=3)
runner.run(suite)
if __name__ == "__main__":
import control
# Add more modules here
# Ex.
# import foo
# import bar
run_tests([control])