Skip to content

Commit 96bce47

Browse files
committed
tests: Factor out common helper functions to separate Python module.
The test runners have evolved over time and become more and more complex. In particular `tests/run-tests.py` is rather large now. The test runners also duplicate some functionality amongst themselves. As a start to improving this situation, this commit factors out the helper functions from `run-tests.py` into a new `test_utils.py` file, and uses that new module in all test runners. There should be no functional change here. Signed-off-by: Damien George <damien@micropython.org>
1 parent af76da9 commit 96bce47

7 files changed

Lines changed: 429 additions & 381 deletions

File tree

tests/run-internalbench.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
from glob import glob
99
from collections import defaultdict
1010

11-
run_tests_module = __import__("run-tests")
12-
sys.path.append(run_tests_module.base_path("../tools"))
13-
import pyboard
11+
from test_utils import (
12+
base_path,
13+
pyboard,
14+
test_instance_description,
15+
test_instance_epilog,
16+
test_directory_description,
17+
get_test_instance,
18+
)
1419

1520
if os.name == "nt":
1621
MICROPYTHON = os.getenv(
@@ -97,10 +102,10 @@ def main():
97102
formatter_class=argparse.RawDescriptionHelpFormatter,
98103
description=f"""Run and manage tests for MicroPython.
99104
100-
{run_tests_module.test_instance_description}
101-
{run_tests_module.test_directory_description}
105+
{test_instance_description}
106+
{test_directory_description}
102107
""",
103-
epilog=run_tests_module.test_instance_epilog,
108+
epilog=test_instance_epilog,
104109
)
105110
cmd_parser.add_argument(
106111
"-t", "--test-instance", default="unix", help="the MicroPython instance to test"
@@ -124,9 +129,7 @@ def main():
124129
args = cmd_parser.parse_args()
125130

126131
# Note pyboard support is copied over from run-tests.py, not tests, and likely needs revamping
127-
pyb = run_tests_module.get_test_instance(
128-
args.test_instance, args.baudrate, args.user, args.password
129-
)
132+
pyb = get_test_instance(args.test_instance, args.baudrate, args.user, args.password)
130133

131134
if len(args.files) == 0:
132135
if args.test_dirs:

tests/run-multitests.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
import subprocess
1616
import tempfile
1717

18-
run_tests_module = __import__("run-tests")
18+
from test_utils import (
19+
base_path,
20+
pyboard,
21+
test_instance_epilog,
22+
convert_device_shortcut_to_real_device,
23+
create_test_report,
24+
)
1925

2026
test_dir = os.path.abspath(os.path.dirname(__file__))
2127

@@ -24,9 +30,6 @@
2430
# accidentally importing tests like micropython/const.py
2531
sys.path.pop(0)
2632

27-
sys.path.insert(0, test_dir + "/../tools")
28-
import pyboard
29-
3033
if os.name == "nt":
3134
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3.exe")
3235
MICROPYTHON = os.path.abspath(
@@ -554,7 +557,7 @@ def main():
554557
cmd_parser = argparse.ArgumentParser(
555558
description="Run network tests for MicroPython",
556559
epilog=(
557-
run_tests_module.test_instance_epilog
560+
test_instance_epilog
558561
+ "Each instance arg can optionally have custom env provided, eg. <cmd>,ENV=VAR,ENV=VAR...\n"
559562
),
560563
formatter_class=argparse.RawTextHelpFormatter,
@@ -582,7 +585,7 @@ def main():
582585
cmd_parser.add_argument(
583586
"-r",
584587
"--result-dir",
585-
default=run_tests_module.base_path("results"),
588+
default=base_path("results"),
586589
help="directory for test results",
587590
)
588591
cmd_parser.add_argument("files", nargs="+", help="input test files")
@@ -612,7 +615,7 @@ def main():
612615
print("unsupported instance string: {}".format(cmd), file=sys.stderr)
613616
sys.exit(2)
614617
else:
615-
device = run_tests_module.convert_device_shortcut_to_real_device(cmd)
618+
device = convert_device_shortcut_to_real_device(cmd)
616619
instances_test.append(PyInstancePyboard(device))
617620

618621
for _ in range(max_instances - len(instances_test)):
@@ -626,7 +629,7 @@ def main():
626629
break
627630

628631
test_results = run_tests(test_files, instances_truth, instances_test_permutation)
629-
all_pass &= run_tests_module.create_test_report(cmd_args, test_results)
632+
all_pass &= create_test_report(cmd_args, test_results)
630633

631634
finally:
632635
for i in instances_truth:

tests/run-natmodtests.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
import sys
1010
import argparse
1111

12-
run_tests_module = __import__("run-tests")
12+
from test_utils import (
13+
base_path,
14+
pyboard,
15+
test_instance_epilog,
16+
get_test_instance,
17+
create_test_report,
18+
)
1319

1420
# Paths for host executables
1521
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")
@@ -110,7 +116,7 @@ def run_script(self, script):
110116
output = self.pyb.exec_(script)
111117
output = output.replace(b"\r\n", b"\n")
112118
return output, None
113-
except run_tests_module.pyboard.PyboardError as er:
119+
except pyboard.PyboardError as er:
114120
return b"", er
115121

116122

@@ -221,7 +227,7 @@ def main():
221227

222228
cmd_parser = argparse.ArgumentParser(
223229
description="Run dynamic-native-module tests under MicroPython",
224-
epilog=run_tests_module.test_instance_epilog,
230+
epilog=test_instance_epilog,
225231
formatter_class=argparse.RawDescriptionHelpFormatter,
226232
)
227233
cmd_parser.add_argument(
@@ -243,7 +249,7 @@ def main():
243249
cmd_parser.add_argument(
244250
"-r",
245251
"--result-dir",
246-
default=run_tests_module.base_path("results"),
252+
default=base_path("results"),
247253
help="directory for test results",
248254
)
249255
cmd_parser.add_argument("files", nargs="*", help="input test files")
@@ -257,9 +263,7 @@ def main():
257263

258264
target_truth = TargetSubprocess([CPYTHON3])
259265

260-
target = run_tests_module.get_test_instance(
261-
args.test_instance, args.baudrate, args.user, args.password
262-
)
266+
target = get_test_instance(args.test_instance, args.baudrate, args.user, args.password)
263267
if target is None:
264268
# Use the unix port of MicroPython.
265269
target = TargetSubprocess([MICROPYTHON])
@@ -283,7 +287,7 @@ def main():
283287

284288
os.makedirs(args.result_dir, exist_ok=True)
285289
test_results = run_tests(target_truth, target, args, target_arch)
286-
res = run_tests_module.create_test_report(args, test_results)
290+
res = create_test_report(args, test_results)
287291

288292
target.close()
289293
target_truth.close()

tests/run-perfbench.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
import argparse
1111
from glob import glob
1212

13-
run_tests_module = __import__("run-tests")
14-
15-
prepare_script_for_target = run_tests_module.prepare_script_for_target
13+
from test_utils import (
14+
base_path,
15+
pyboard,
16+
get_test_instance,
17+
prepare_script_for_target,
18+
create_test_report,
19+
)
1620

1721
# Paths for host executables
1822
if os.name == "nt":
@@ -49,7 +53,7 @@ def run_script_on_target(target, script):
4953
try:
5054
target.enter_raw_repl()
5155
output = target.exec_(script)
52-
except run_tests_module.pyboard.PyboardError as er:
56+
except pyboard.PyboardError as er:
5357
err = er
5458
else:
5559
# Run local executable
@@ -277,7 +281,7 @@ def main():
277281
cmd_parser.add_argument(
278282
"-r",
279283
"--result-dir",
280-
default=run_tests_module.base_path("results"),
284+
default=base_path("results"),
281285
help="directory for test results",
282286
)
283287
cmd_parser.add_argument(
@@ -298,9 +302,7 @@ def main():
298302
M = int(args.M[0])
299303
n_average = int(args.average)
300304

301-
target = run_tests_module.get_test_instance(
302-
args.test_instance, args.baudrate, args.user, args.password
303-
)
305+
target = get_test_instance(args.test_instance, args.baudrate, args.user, args.password)
304306
if target is None:
305307
# Use the unix port of MicroPython.
306308
target = [MICROPYTHON, "-X", "emit=" + args.emit]
@@ -328,7 +330,7 @@ def main():
328330

329331
os.makedirs(args.result_dir, exist_ok=True)
330332
test_results = run_benchmarks(args, target, N, M, n_average, tests)
331-
res = run_tests_module.create_test_report(args, test_results)
333+
res = create_test_report(args, test_results)
332334

333335
if hasattr(target, "exit_raw_repl"):
334336
target.exit_raw_repl()

0 commit comments

Comments
 (0)