-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconftest.py
184 lines (153 loc) · 6.25 KB
/
conftest.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
import os
import shlex
import warnings
from distutils.version import LooseVersion
import matplotlib as mpl
import nengo
import pytest
from pytest_allclose import report_rmses
import nengo_loihi
from nengo_loihi.builder.builder import Model, ValidationLevel
from nengo_loihi.emulator import EmulatorInterface
from nengo_loihi.tests import make_test_sim
def pytest_configure(config):
mpl.use("Agg")
EmulatorInterface.strict = True
nengo_loihi.set_defaults()
Model.default_validation_level = ValidationLevel.FULL
config.addinivalue_line(
"markers", "target_sim: mark test as only running on emulator"
)
config.addinivalue_line(
"markers", "target_loihi: mark test as only running on hardware"
)
config.addinivalue_line(
"markers", "hang: mark test as hanging indefinitely on hardware"
)
config.addinivalue_line(
"markers",
"requires_multichip_snips: "
"mark test as requiring boards that can run multichip Snips",
)
# add unsupported attribute to Simulator (for compatibility with nengo<3.0)
# join all the lines and then split (preserving quoted strings)
if nengo.version.version_info <= (2, 8, 0):
unsupported = shlex.split(" ".join(config.getini("nengo_test_unsupported")))
# group pairs (representing testname + reason)
unsupported = [unsupported[i : i + 2] for i in range(0, len(unsupported), 2)]
# wrap square brackets to interpret them literally
# (see https://docs.python.org/3/library/fnmatch.html)
for i, (testname, _) in enumerate(unsupported):
unsupported[i][0] = "".join(
"[%s]" % c if c in ("[", "]") else c for c in testname
).replace("::", ":")
nengo_loihi.Simulator.unsupported = unsupported
def pytest_addoption(parser):
parser.addoption(
"--target",
type=str,
default="sim",
help="Platform on which to run tests ('sim' or 'loihi')",
)
parser.addoption(
"--no-hang", action="store_true", default=False, help="Skip tests that hang"
)
if nengo.version.version_info <= (2, 8, 0):
# add the pytest option from future nengo versions
parser.addini(
"nengo_test_unsupported",
type="linelist",
help="List of unsupported unit tests with reason for exclusion",
)
def pytest_report_header(config, startdir):
target = config.getoption("--target")
return "Nengo Loihi is using Loihi {}".format(
"hardware" if target == "loihi" else "emulator"
)
def pytest_terminal_summary(terminalreporter):
report_rmses(terminalreporter)
@pytest.fixture(scope="session")
def Simulator(request):
"""Simulator class to be used in tests"""
return make_test_sim(request)
def pytest_collection_modifyitems(session, config, items):
target = config.getoption("--target")
# Skip tests specific to emulator/hardware
if target == "loihi":
skip_sim = pytest.mark.skip(reason="test only runs on emulator")
for item in items:
if "target_sim" in item.keywords:
item.add_marker(skip_sim)
elif target == "sim":
skip_loihi = pytest.mark.skip(reason="test only runs on hardware")
for item in items:
if "target_loihi" in item.keywords:
item.add_marker(skip_loihi)
# Skip hanging tests when running on hardware
if target != "loihi":
return
hanging_nengo_tests = [
"nengo/tests/test_learning_rules.py::test_slicing",
"nengo/tests/test_neurons.py::test_direct_mode_nonfinite_value",
"nengo/tests/test_neurons.py::test_lif_min_voltage[-inf]",
"nengo/tests/test_neurons.py::test_lif_min_voltage[-1]",
"nengo/tests/test_neurons.py::test_lif_min_voltage[0]",
"nengo/tests/test_neurons.py::test_lif_zero_tau_ref",
"nengo/tests/test_node.py::test_none",
"nengo/tests/test_node.py::test_invalid_values[inf]",
"nengo/tests/test_node.py::test_invalid_values[nan]",
"nengo/tests/test_node.py::test_invalid_values[string]",
"nengo/utils/tests/test_network.py::" # no comma
"test_activate_direct_mode_learning[learning_rule0-False]",
"nengo/utils/tests/test_network.py::" # no comma
"test_activate_direct_mode_learning[learning_rule1-True]",
"nengo/utils/tests/test_network.py::" # no comma
"test_activate_direct_mode_learning[learning_rule2-True]",
"nengo/utils/tests/test_network.py::" # no comma
"test_activate_direct_mode_learning[learning_rule3-False]",
]
skip_hanging = pytest.mark.skip(reason="test hangs on hardware")
for item in items:
if "hang" in item.keywords or item.nodeid in hanging_nengo_tests:
# pragma: no cover, because we may find no hanging tests
item.add_marker(skip_hanging)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
set_partition = False
set_lmtoptions = False
if item.get_closest_marker(name="requires_multichip_snips") is not None:
partition = os.getenv("PARTITION")
good_partitions = [
None,
"nahuku32",
"pohoiki",
"nahuku32,pohoiki",
"pohoiki,nahuku32",
]
if partition not in good_partitions:
warnings.warn(
f"{item.nodeid} requires nahuku32 or pohoiki partition. You set "
f"PARTITION={partition} which will likely hang"
)
if partition is None:
os.environ["PARTITION"] = "nahuku32,pohoiki"
set_partition = True
lmtoptions = os.getenv("LMTOPTIONS")
if lmtoptions is None:
os.environ["LMTOPTIONS"] = "--skip-power=1"
set_lmtoptions = True
elif "--skip-power=1" not in lmtoptions:
warnings.warn(
f"Tests may hang if LMTOPTIONS does not contain --skip-power=1. "
f"You set LMTOPTIONS={lmtoptions}"
)
yield
if set_partition:
del os.environ["PARTITION"]
if set_lmtoptions:
del os.environ["LMTOPTIONS"]
if LooseVersion(nengo.__version__) < "3.0.0":
from nengo import conftest
conftest.Simulator = Simulator
conftest.RefSimulator = Simulator
plt = conftest.plt