-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconftest.py
More file actions
133 lines (103 loc) · 4.97 KB
/
conftest.py
File metadata and controls
133 lines (103 loc) · 4.97 KB
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
import subprocess
import pytest
collect_ignore = ["marrow/tests/bench_popcount_py.py"]
def pytest_addoption(parser):
parser.addoption("--mojo", action="store_true", default=False, help="Select Mojo tests")
parser.addoption("--no-mojo", action="store_true", default=False, help="Exclude Mojo tests")
parser.addoption("--python", action="store_true", default=False, help="Select Python tests")
parser.addoption("--no-python", action="store_true", default=False, help="Exclude Python tests")
parser.addoption("--gpu", action="store_true", default=False, help="Select GPU tests")
parser.addoption("--no-gpu", action="store_true", default=False, help="Exclude GPU tests")
parser.addoption(
"--benchmark",
action="store_true",
default=False,
help="Include benchmarks (Python pytest-benchmark and Mojo bench_*.mojo); skipped by default",
)
def pytest_configure(config):
config.addinivalue_line("markers", "mojo: Mojo language tests")
config.addinivalue_line("markers", "python: Python tests")
config.addinivalue_line("markers", "gpu: requires GPU hardware")
config.addinivalue_line(
"markers",
"benchmark: performance benchmarks (skipped by default, run with --benchmark)",
)
def pytest_collection_modifyitems(config, items):
sel_mojo = config.getoption("--mojo")
sel_python = config.getoption("--python")
sel_gpu = config.getoption("--gpu")
no_mojo = config.getoption("--no-mojo")
no_python = config.getoption("--no-python")
no_gpu = config.getoption("--no-gpu")
run_benchmark = config.getoption("--benchmark")
selective = sel_mojo or sel_python or sel_gpu
for item in items:
is_gpu = "gpu" in item.keywords
is_mojo = "mojo" in item.keywords and not is_gpu
is_python = "python" in item.keywords
is_benchmark = "benchmark" in item.keywords
if is_benchmark and not run_benchmark:
item.add_marker(pytest.mark.skip(reason="benchmarks excluded; pass --benchmark to include"))
elif (no_gpu and is_gpu) or (selective and is_gpu and not sel_gpu):
item.add_marker(pytest.mark.skip(reason="GPU tests excluded; pass --gpu to include"))
elif (no_mojo and is_mojo) or (selective and is_mojo and not sel_mojo):
item.add_marker(pytest.mark.skip(reason="Mojo tests excluded; pass --mojo to include"))
elif (no_python and is_python) or (selective and is_python and not sel_python):
item.add_marker(pytest.mark.skip(reason="Python tests excluded; pass --python to include"))
def pytest_collect_file(parent, file_path):
if file_path.suffix == ".mojo" and file_path.name.startswith("test_"):
return MojoTestFile.from_parent(parent, path=file_path)
if file_path.suffix == ".mojo" and file_path.name.startswith("bench_"):
return MojoBenchFile.from_parent(parent, path=file_path)
def pytest_itemcollected(item):
if item.fspath.ext == ".py":
item.add_marker(pytest.mark.python)
if item.fspath.basename.startswith("bench_"):
item.add_marker(pytest.mark.benchmark)
class MojoTestFile(pytest.File):
def collect(self):
is_gpu = self.path.stem.endswith("_gpu")
yield MojoTestItem.from_parent(self, name=self.path.stem, is_gpu=is_gpu)
class MojoTestItem(pytest.Item):
def __init__(self, name, parent, is_gpu=False):
super().__init__(name, parent)
self.is_gpu = is_gpu
self.add_marker(pytest.mark.mojo)
if is_gpu:
self.add_marker(pytest.mark.gpu)
def runtest(self):
capman = self.config.pluginmanager.getplugin("capturemanager")
with capman.global_and_fixture_disabled():
result = subprocess.run(
["mojo", "run", "-I", ".", str(self.fspath)],
cwd=self.config.rootpath,
)
if result.returncode != 0:
raise MojoTestFailure(f"exit code {result.returncode}")
def repr_failure(self, excinfo):
return str(excinfo.value)
def reportinfo(self):
return self.fspath, 0, f"mojo::{self.name}"
class MojoBenchFile(pytest.File):
def collect(self):
yield MojoBenchItem.from_parent(self, name=self.path.stem)
class MojoBenchItem(pytest.Item):
def __init__(self, name, parent):
super().__init__(name, parent)
self.add_marker(pytest.mark.mojo)
self.add_marker(pytest.mark.benchmark)
def runtest(self):
capman = self.config.pluginmanager.getplugin("capturemanager")
with capman.global_and_fixture_disabled():
result = subprocess.run(
["mojo", "run", "-I", ".", str(self.fspath)],
cwd=self.config.rootpath,
)
if result.returncode != 0:
raise MojoTestFailure(f"exit code {result.returncode}")
def repr_failure(self, excinfo):
return str(excinfo.value)
def reportinfo(self):
return self.fspath, 0, f"mojo::bench::{self.name}"
class MojoTestFailure(Exception):
pass