Skip to content

Commit 533411d

Browse files
committed
Use pytest fixture for writing the report by the end of the test.
1 parent f4f5ac5 commit 533411d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

tests/IVIMmodels/unit_tests/test_ivim_fit.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010
logger = logging.getLogger(__name__)
1111
#run using python -m pytest from the root folder
1212

13-
test_results = []
13+
@pytest.fixture(scope="session")
14+
def test_results():
15+
return []
16+
17+
@pytest.fixture(scope="session", autouse=True)
18+
def write_report(test_results, request):
19+
def finalize():
20+
with open('test_results_report.json', 'w') as f:
21+
json.dump({"results": test_results}, f, indent=4)
22+
request.addfinalizer(finalize)
1423

1524

1625
def signal_helper(signal):
@@ -61,8 +70,7 @@ def data_ivim_fit_saved():
6170

6271

6372
@pytest.mark.parametrize("name, bvals, data, algorithm, xfail, kwargs, tolerances", data_ivim_fit_saved())
64-
def test_ivim_fit_saved(name, bvals, data, algorithm, xfail, kwargs, tolerances, request):
65-
global test_results
73+
def test_ivim_fit_saved(name, bvals, data, algorithm, xfail, kwargs, tolerances, request, test_results):
6674
if xfail["xfail"]:
6775
mark = pytest.mark.xfail(reason="xfail", strict=xfail["strict"])
6876
request.node.add_marker(mark)
@@ -88,9 +96,7 @@ def to_list_if_needed(value):
8896
test_result['status'] = "XFAILED"
8997

9098
test_results.append(test_result)
91-
with open('test_results_report.json', 'w') as f:
92-
json.dump({"results": test_results, "rtol": tolerances["rtol"],
93-
"atol": tolerances["atol"], }, f, indent=4)
99+
94100
npt.assert_allclose(data['f'], f_fit, rtol=tolerances["rtol"]["f"], atol=tolerances["atol"]["f"])
95101
npt.assert_allclose(data['D'], D_fit, rtol=tolerances["rtol"]["D"], atol=tolerances["atol"]["D"])
96102
npt.assert_allclose(data['Dp'], Dp_fit, rtol=tolerances["rtol"]["Dp"], atol=tolerances["atol"]["Dp"])

0 commit comments

Comments
 (0)