-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.py
205 lines (192 loc) · 5.67 KB
/
run_test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# flake8: noqa
import argparse
import atexit
import os
import shutil
import tempfile
# refer https://github.com/taichi-dev/taichi/tree/master/tests/python
def _test_python(args, default_dir="tests"):
print("\nRunning Python tests...\n")
curr_dir = os.path.dirname(os.path.abspath(__file__))
test_dir = os.path.join(curr_dir, default_dir)
pytest_args = []
# TODO: use pathlib to deal with suffix and stem name manipulation
if args.files:
# run individual tests
for f in args.files:
# auto-complete file names
if not f.startswith("test_"):
f = "test_" + f
if not (f.endswith(".py") or f.endswith(".ipynb")):
f = f + ".py"
file = os.path.join(test_dir, f)
has_tests = False
if os.path.exists(file):
pytest_args.append(file)
has_tests = True
assert has_tests, f"Test {f} does not exist."
else:
# run all the tests
pytest_args = [test_dir]
pytest_args += ["--nbmake"]
if args.verbose:
pytest_args += ["-v"]
if args.rerun:
pytest_args += ["--reruns", args.rerun]
try:
if args.coverage:
pytest_args += ["--cov-branch", "--cov=python/bisheng"]
if args.cov_append:
pytest_args += ["--cov-append"]
if args.keys:
pytest_args += ["-k", args.keys]
if args.marks:
pytest_args += ["-m", args.marks]
if args.failed_first:
pytest_args += ["--failed-first"]
if args.fail_fast:
pytest_args += ["--exitfirst"]
if args.timeout > 0:
pytest_args += [
"--durations=15",
"-p",
"pytest_hardtle",
f"--timeout={args.timeout}",
]
except AttributeError:
pass
try:
from multiprocessing import cpu_count # pylint: disable=C0415
threads = min(8, cpu_count()) # To prevent running out of memory
except NotImplementedError:
threads = 2
env_threads = os.environ.get("BISHENG_TEST_THREADS", "")
threads = args.threads or env_threads or threads
print(f"Starting {threads} testing thread(s)...")
if args.show_output:
pytest_args += ["-s"]
print(f"Due to how pytest-xdist is implemented, the -s option does not work with multiple thread...")
else:
if int(threads) > 1:
pytest_args += ["-n", str(threads), "--dist=worksteal"]
import pytest # pylint: disable=C0415
return int(pytest.main(pytest_args))
def test():
"""Run the tests"""
parser = argparse.ArgumentParser(description=f"Run bisheng python test")
parser.add_argument("files", nargs="*", help='Test name(s) to be run, e.g. "cli"')
parser.add_argument(
"-c",
"--cpp",
dest="cpp",
default=False,
action="store_true",
help="Only run the C++ tests",
)
parser.add_argument(
"-s",
"--show",
dest="show_output",
action="store_true",
help="Show output (do not capture)",
)
parser.add_argument(
"-v",
"--verbose",
dest="verbose",
action="store_true",
help="Run with verbose outputs",
)
parser.add_argument(
"-r",
"--rerun",
required=False,
default=None,
dest="rerun",
type=str,
help="Rerun failed tests for given times",
)
parser.add_argument(
"-k",
"--keys",
required=False,
default=None,
dest="keys",
type=str,
help="Only run tests that match the keys",
)
parser.add_argument(
"-m",
"--marks",
required=False,
default=None,
dest="marks",
type=str,
help="Only run tests with specific marks",
)
parser.add_argument(
"-f",
"--failed-first",
required=False,
default=None,
dest="failed_first",
action="store_true",
help="Run the previously failed test first",
)
parser.add_argument(
"-x",
"--fail-fast",
required=False,
default=None,
dest="fail_fast",
action="store_true",
help="Exit instantly on the first failed test",
)
parser.add_argument(
"-C",
"--coverage",
required=False,
default=None,
dest="coverage",
action="store_true",
help="Run tests and record the coverage result",
)
parser.add_argument(
"-T",
"--timeout",
required=False,
default=600,
type=int,
dest="timeout",
help="Per test timeout (only apply to python tests)",
)
parser.add_argument(
"-A",
"--cov-append",
required=False,
default=None,
dest="cov_append",
action="store_true",
help="Append coverage result to existing one instead of overriding it",
)
parser.add_argument(
"-t",
"--threads",
required=False,
default=None,
dest="threads",
type=str,
help="Custom number of threads for parallel testing",
)
run_count = 1
args = parser.parse_args()
print(args)
for _ in range(run_count):
ret = _test_python(args)
if ret == 5:
# treat 'no tests collected' as success
ret = 0
if ret != 0:
exit(ret)
if __name__ == "__main__":
test()