-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun_all_unit_tests.py
92 lines (73 loc) · 2.85 KB
/
run_all_unit_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
82
83
84
85
86
87
88
89
90
91
92
"""
This script searches recursively for unit tests and generates the tests results in xml format in the way that Jenkins expects.
In the case that it's a Jenkins job, it should delete any created cache (not implemented yet)
"""
import logging
import sys
from unittest import TestLoader
import os
import termcolor
from xmlrunner import XMLTestRunner
from os import listdir
from os.path import isfile, join
print(os.path.dirname(os.path.realpath(__file__)))
def mehikon(a, b): # type: ignore
print(a)
termcolor.cprint = mehikon # since junit/jenkins doesn't like text color ...
if __name__ == "__main__":
print(
f'info:in {__file__} in conda env {os.environ.get("CONDA_PREFIX", "missing???")}'
)
mode = None
if len(sys.argv) > 1:
mode = sys.argv[
1
] # options "examples", "core" or None for both "core" and "examples"
os.environ["DISPLAY"] = "" # disable display in unit tests
is_jenkins_job = "WORKSPACE" in os.environ and len(os.environ["WORKSPACE"]) > 2
search_base = os.path.dirname(os.path.realpath(__file__))
output = f"{search_base}/test-reports/"
print("will generate unit tests output xml at :", output)
sub_sections_core = [("fusedrug", search_base)]
sub_sections_examples = [("fusedrug_examples/tests", search_base)]
if mode is None:
sub_sections = sub_sections_core + sub_sections_examples
elif mode == "core":
sub_sections = sub_sections_core
elif mode == "examples":
sub_sections = sub_sections_examples
else:
raise Exception(f"Error: unexpected mode {mode}")
suite = None
for curr_subsection, top_dir in sub_sections:
curr_subsuite = TestLoader().discover(
f"{search_base}/{curr_subsection}", "test*.py", top_level_dir=top_dir
)
if suite is None:
suite = curr_subsuite
else:
suite.addTest(curr_subsuite)
# enable fuse-drug logger and avoid colors format
lgr = logging.getLogger("Fuse")
lgr.setLevel(logging.INFO)
test_results = XMLTestRunner(output=output, verbosity=2, stream=sys.stdout).run(
suite,
)
### A workaround for "An invalid XML character" issue for the examples' unit-tests
examples_test_files = [
f
for f in listdir(output)
if (isfile(join(output, f)) and f.startswith("TEST-"))
]
for file in examples_test_files:
file_path = join(output, file)
# Open the file for reading
with open(file_path, "r") as f:
# Read the contents of the file
contents = f.read()
# Replace all occurrences
contents = contents.replace("\u001b", "?")
# Open the file for writing (this will overwrite the original file)
with open(file_path, "w") as f:
# Write the modified contents back to the file
f.write(contents)