| 1 |
""" |
| 2 |
Module containing fixtures and hooks used by the pytest test suite. |
| 3 |
""" |
| 4 |
|
| 5 |
import re |
| 6 |
|
| 7 |
import numpy as np |
| 8 |
import pytest |
| 9 |
|
| 10 |
from brian2.core.clocks import defaultclock |
| 11 |
from brian2.core.functions import DEFAULT_FUNCTIONS, Function |
| 12 |
from brian2.devices import get_device, reinit_devices |
| 13 |
from brian2.devices.device import reinit_and_delete, set_device |
| 14 |
from brian2.units import ms |
| 15 |
|
| 16 |
|
| 17 |
def pytest_ignore_collect(collection_path, config): |
| 18 |
if config.option.doctestmodules: |
| 19 |
if "tests" in collection_path.parts: |
| 20 |
return True # Ignore tests package for doctests |
| 21 |
# Do not test brian2.hears bridge (needs Brian1) |
| 22 |
if collection_path.name == "hears.py": |
| 23 |
return True |
| 24 |
|
| 25 |
|
| 26 |
# The "random" values are always 0.5 |
| 27 |
def fake_randn(vectorisation_idx): |
| 28 |
return 0.5 * np.ones_like(vectorisation_idx) |
| 29 |
|
| 30 |
|
| 31 |
fake_randn = Function( |
| 32 |
fake_randn, arg_units=[], return_unit=1, auto_vectorise=True, stateless=False |
| 33 |
) |
| 34 |
fake_randn.implementations.add_implementation( |
| 35 |
"cpp", |
| 36 |
""" |
| 37 |
double randn(int vectorisation_idx) |
| 38 |
{ |
| 39 |
return 0.5; |
| 40 |
} |
| 41 |
""", |
| 42 |
) |
| 43 |
fake_randn.implementations.add_implementation( |
| 44 |
"cython", |
| 45 |
""" |
| 46 |
cdef double randn(int vectorisation_idx): |
| 47 |
return 0.5 |
| 48 |
""", |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.fixture |
| 53 |
def fake_randn_randn_fixture(): |
| 54 |
orig_randn = DEFAULT_FUNCTIONS["randn"] |
| 55 |
DEFAULT_FUNCTIONS["randn"] = fake_randn |
| 56 |
yield None |
| 57 |
DEFAULT_FUNCTIONS["randn"] = orig_randn |
| 58 |
|
| 59 |
|
| 60 |
# Fixture that is used for all tests |
| 61 |
@pytest.fixture(autouse=True) |
| 62 |
def setup_and_teardown(request): |
| 63 |
# Set preferences before each test |
| 64 |
import brian2 |
| 65 |
|
| 66 |
if hasattr(request.config, "workerinput"): |
| 67 |
config = request.config.workerinput |
| 68 |
for key, value in config["brian_prefs"].items(): |
| 69 |
if isinstance(value, tuple) and value[0] == "TYPE": |
| 70 |
matches = re.match(r"<(type|class) 'numpy\.(.+)'>", value[1]) |
| 71 |
if matches is None or len(matches.groups()) != 2: |
| 72 |
raise TypeError( |
| 73 |
f"Do not know how to handle {value[1]} in preferences" |
| 74 |
) |
| 75 |
t = matches.groups()[1] |
| 76 |
if t == "float64": |
| 77 |
value = np.float64 |
| 78 |
elif t == "float32": |
| 79 |
value = np.float32 |
| 80 |
elif t == "int64": |
| 81 |
value = np.int64 |
| 82 |
elif t == "int32": |
| 83 |
value = np.int32 |
| 84 |
|
| 85 |
brian2.prefs[key] = value |
| 86 |
set_device(config["device"], **config["device_options"]) |
| 87 |
else: |
| 88 |
for k, v in request.config.brian_prefs.items(): |
| 89 |
brian2.prefs[k] = v |
| 90 |
set_device(request.config.device, **request.config.device_options) |
| 91 |
brian2.prefs._backup() |
| 92 |
# Print output changed in numpy 1.14, stick with the old format to |
| 93 |
# avoid doctest failures |
| 94 |
try: |
| 95 |
np.set_printoptions(legacy="1.13") |
| 96 |
except TypeError: |
| 97 |
pass # using a numpy version < 1.14 |
| 98 |
|
| 99 |
yield # run test |
| 100 |
|
| 101 |
# Reset defaultclock.dt to be sure |
| 102 |
defaultclock.dt = 0.1 * ms |
| 103 |
|
| 104 |
|
| 105 |
# (Optionally) mark tests raising NotImplementedError as skipped (mostly used |
| 106 |
# for testing Brian2GeNN) |
| 107 |
@pytest.hookimpl(hookwrapper=True) |
| 108 |
def pytest_runtest_makereport(item, call): |
| 109 |
if hasattr(item.config, "workerinput"): |
| 110 |
fail_for_not_implemented = item.config.workerinput["fail_for_not_implemented"] |
| 111 |
else: |
| 112 |
fail_for_not_implemented = item.config.fail_for_not_implemented |
| 113 |
outcome = yield |
| 114 |
rep = outcome.get_result() |
| 115 |
if rep.outcome == "failed": |
| 116 |
project_dir = getattr(get_device(), "project_dir", None) |
| 117 |
if project_dir is not None: |
| 118 |
rep.sections.append(("Standalone project directory", f"{project_dir}")) |
| 119 |
reinit_devices() |
| 120 |
if not fail_for_not_implemented: |
| 121 |
exc_cause = getattr(call.excinfo.value, "__cause__", None) |
| 122 |
if call.excinfo.errisinstance(NotImplementedError) or isinstance( |
| 123 |
exc_cause, NotImplementedError |
| 124 |
): |
| 125 |
rep.outcome = "skipped" |
| 126 |
r = call.excinfo._getreprcrash() |
| 127 |
rep.longrepr = (str(r.path), r.lineno, r.message) |
| 128 |
else: |
| 129 |
# clean up after the test (delete directory for standalone) |
| 130 |
reinit_and_delete() |