-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.py
executable file
·122 lines (96 loc) · 3.34 KB
/
run_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
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
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
import argparse
import importlib.util
import os
import subprocess
import sys
from pathlib import Path
from typing import List
def get_script_dir() -> Path:
"""Return path to the script directory."""
return Path(__file__).resolve().parent
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--coverage",
action="store_true",
help="Collect coverage from the unit test run",
)
parser.add_argument(
"--force-pythonpath",
action="store_true",
help=(
"Point PYTHONPATH to the source directory, so that the tests "
"can run even without having the package installed"
),
)
parser.add_argument(
"--openocd-path", help="Path to OpenOCD executable", default=None
)
parser.add_argument(
"test_type", choices=["unit", "integration"], help="Type of the tests"
)
return parser.parse_args()
def run_subproc(cmd: List[str]) -> None:
print("Running command: " + repr(cmd))
subprocess.check_call(cmd, cwd=get_script_dir())
def run_unittests(enable_coverage: bool) -> None:
pytest_args = ["-m", "pytest", "tests_unit/", "-vv"]
if enable_coverage:
cmd = [sys.executable, "-m", "coverage", "run"] + pytest_args
else:
cmd = [sys.executable] + pytest_args
run_subproc(cmd)
def run_integration_tests(openocd_path: Path, enable_coverage: bool) -> None:
pytest_args = [
"-m",
"pytest",
"tests_integration/",
"-vv",
"--openocd-path",
str(openocd_path),
]
if enable_coverage:
cmd = [sys.executable, "-m", "coverage", "run"] + pytest_args
else:
cmd = [sys.executable] + pytest_args
run_subproc(cmd)
def run_coverage_html_generation() -> None:
cmd = [sys.executable, "-m", "coverage", "html"]
run_subproc(cmd)
def run_coverage_xml_generation() -> None:
cmd = [sys.executable, "-m", "coverage", "xml"]
run_subproc(cmd)
def main() -> int:
args = parse_args()
if args.force_pythonpath:
# Allow running the unit tests even without having the package installed
os.environ["PYTHONPATH"] = str(get_script_dir() / "src")
else:
if importlib.util.find_spec("py_openocd_client") is None:
print("Error: Package py_openocd_client not found.")
print("You can:")
print("- install the package before running the tests, or")
print(
"- start the script with --force-pythonpath to modify PYTHONPATH "
"and use the source code directly, without having it installed"
)
return 1
if args.test_type == "unit":
if args.openocd_path:
raise RuntimeError("--openocd-path is irrelevant for unittests")
run_unittests(args.coverage)
elif args.test_type == "integration":
if not args.openocd_path:
raise RuntimeError("--openocd-path is required for integration tests")
openocd_path = Path(args.openocd_path).resolve()
run_integration_tests(openocd_path, args.coverage)
else:
assert False
if args.coverage:
run_coverage_html_generation()
run_coverage_xml_generation()
return 0
if __name__ == "__main__":
sys.exit(main())