From 64a7c051f1a51ff2a89b9da9257a21cea0ef00fa Mon Sep 17 00:00:00 2001 From: Jonathan Barnoud Date: Wed, 14 Oct 2020 13:39:56 +0100 Subject: [PATCH] Use a fixture for npz files in test_bat (#2981) Fixes #2979 ## Work done in this PR The file test_bat_IO.npy was created in one test and used in another one. This leads to the second test possibly failing when the tests are run in parallel. This commit moves creating the file into a fixture that the two tests depends. The new fixture also uses the tmpdir fixture to avoid the file created during the tests to remain in the test directory. --- package/CHANGELOG | 2 ++ .../MDAnalysisTests/analysis/test_bat.py | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 2f51f4c37e6..70b504d22f5 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -58,6 +58,8 @@ Fixes Universe (PR #2893) * ensure that unistd.h is included on macOS when compiling ENCORE's spe.c (Issue #2934) + * Fix tests for analysis.bat that could fail when run in parallel and that + would create a test artifact (Issue #2979, PR #2981) Enhancements * Refactored analysis.helanal into analysis.helix_analysis diff --git a/testsuite/MDAnalysisTests/analysis/test_bat.py b/testsuite/MDAnalysisTests/analysis/test_bat.py index 36ef684319b..9fb0e93b977 100644 --- a/testsuite/MDAnalysisTests/analysis/test_bat.py +++ b/testsuite/MDAnalysisTests/analysis/test_bat.py @@ -45,6 +45,14 @@ def bat(self, selected_residues): R.run() return R.bat + @pytest.fixture + def bat_npz(self, tmpdir, selected_residues): + filename = str(tmpdir / 'test_bat_IO.npy') + R = BAT(selected_residues) + R.run() + R.save(filename) + return filename + def test_bat_root_selection(self, selected_residues): R = BAT(selected_residues) assert_equal(R._root.indices, [8, 2, 1], @@ -79,11 +87,8 @@ def test_bat_reconstruction(self, selected_residues, bat): err_msg="error: Reconstructed Cartesian coordinates " + \ "don't match original") - def test_bat_IO(self, selected_residues, bat): - R = BAT(selected_residues) - R.run() - R.save('test_bat_IO.npy') - R2 = BAT(selected_residues, filename='test_bat_IO.npy') + def test_bat_IO(self, bat_npz, selected_residues, bat): + R2 = BAT(selected_residues, filename=bat_npz) test_bat = R2.bat assert_almost_equal( bat, @@ -107,8 +112,8 @@ def test_bat_disconnected_atom_group(self): with pytest.raises(ValueError): R = BAT(selected_residues) - def test_bat_incorrect_dims(self): + def test_bat_incorrect_dims(self, bat_npz): u = mda.Universe(PSF, DCD) selected_residues = u.select_atoms("resid 1-3") with pytest.raises(ValueError): - R = BAT(selected_residues, filename='test_bat_IO.npy') + R = BAT(selected_residues, filename=bat_npz)