From 77d1ace40256e598cc9c56a779c7ca20ed32f36c Mon Sep 17 00:00:00 2001 From: Bansal Utkarsh Date: Sun, 16 Jul 2017 01:41:36 +0530 Subject: [PATCH 1/4] Pytest style auxiliary/base.py --- testsuite/MDAnalysisTests/auxiliary/base.py | 46 +++++++++------------ 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/testsuite/MDAnalysisTests/auxiliary/base.py b/testsuite/MDAnalysisTests/auxiliary/base.py index de082238290..f0c076ce381 100644 --- a/testsuite/MDAnalysisTests/auxiliary/base.py +++ b/testsuite/MDAnalysisTests/auxiliary/base.py @@ -24,14 +24,15 @@ from six.moves import range import pytest import numpy as np -from numpy.testing import (assert_equal, assert_raises, assert_almost_equal, - raises) +from numpy.testing import assert_almost_equal + import MDAnalysis as mda from MDAnalysisTests.datafiles import (COORDINATES_XTC, COORDINATES_TOPOLOGY) -@raises(ValueError) + +@pytest.mark.raises(exception=ValueError) def test_get_bad_auxreader_format_raises_ValueError(): # should raise a ValueError when no AuxReaders with match the specified format mda.auxiliary.core.get_auxreader_for(format='bad-format') @@ -132,16 +133,13 @@ def format_data(self, data): class BaseAuxReaderTest(object): def test_n_steps(self, ref, reader): - assert_equal(len(reader), ref.n_steps, - "number of steps does not match") + assert len(reader) == ref.n_steps, "number of steps does not match" def test_dt(self, ref, reader): - assert_equal(reader.dt, ref.dt, - "dt does not match") + assert reader.dt == ref.dt, "dt does not match" def test_initial_time(self, ref, reader): - assert_equal(reader.initial_time, ref.initial_time, - "initial time does not match") + assert reader.initial_time == ref.initial_time, "initial time does not match" def test_first_step(self, ref, reader): # on first loading we should start at step 0 @@ -173,7 +171,8 @@ def test_next_past_last_step_raises_StopIteration(self, ref, reader): # should take us to the last step reader[-1] # if we try to move to next step from here, should raise StopIteration - assert_raises(StopIteration, reader.next) + with pytest.raises(StopIteration): + reader.next def test_move_to_invalid_step_raises_IndexError(self, ref, reader): # last step is number n_steps -1 ; if we try move to step number @@ -228,8 +227,7 @@ def test_time_selector(self, ref): time_selector = ref.time_selector) # time should still match reference time for each step for i, val in enumerate(reader): - assert_equal(val.time, ref.select_time_ref[i], - "time for step {} does not match".format(i)) + assert val.time == ref.select_time_ref[i], "time for step {} does not match".format(i) def test_data_selector(self, ref): # reload reader, passing in a data selector @@ -237,8 +235,7 @@ def test_data_selector(self, ref): data_selector=ref.data_selector) # data should match reference data for each step for i, val in enumerate(reader): - assert_equal(val.data, ref.select_data_ref[i], - "data for step {0} does not match".format(i)) + assert val.data == ref.select_data_ref[i], "data for step {0} does not match".format(i) def test_no_constant_dt(self, ref): ## assume we can select time... @@ -248,8 +245,7 @@ def test_no_constant_dt(self, ref): constant_dt=False) # time should match reference for selecting time, for each step for i, val in enumerate(reader): - assert_equal(val.time, ref.select_time_ref[i], - "data for step {} does not match".format(i)) + assert val.time == ref.select_time_ref[i], "data for step {} does not match".format(i) def test_update_ts_without_auxname_raises_ValueError(self, ref): # reload reader without auxname @@ -319,7 +315,7 @@ def test_read_higher_freq_timestep(self, ref, reader): def test_get_auxreader_for(self, ref, reader): # check guesser gives us right reader reader = mda.auxiliary.core.get_auxreader_for(ref.testdata) - assert_equal(reader, ref.reader) + assert reader == ref.reader def test_iterate_through_trajectory(self, ref): # add to trajectory @@ -329,9 +325,8 @@ def test_iterate_through_trajectory(self, ref): # trajectory here has same dt, offset; so there's a direct correspondence # between frames and steps for i, ts in enumerate(u.trajectory): - assert_equal(ts.aux.test, ref.auxsteps[i].data, - "representative value does not match when iterating through " - "all trajectory timesteps") + assert ts.aux.test == ref.auxsteps[i].data, "representative value does not match when iterating through "\ + "all trajectory timesteps" u.trajectory.close() def test_iterate_as_auxiliary_from_trajectory(self, ref): @@ -342,22 +337,19 @@ def test_iterate_as_auxiliary_from_trajectory(self, ref): # trahectory here has same dt, offset, so there's a direct correspondence # between frames and steps, and iter_as_aux will run through all frames for i, ts in enumerate(u.trajectory.iter_as_aux('test')): - assert_equal(ts.aux.test, ref.auxsteps[i].data, - "representative value does not match when iterating through " - "all trajectory timesteps") + assert ts.aux.test == ref.auxsteps[i].data, "representative value does not match when iterating through "\ + "all trajectory timesteps" u.trajectory.close() def test_get_description(self, ref, reader): description = reader.get_description() for attr in ref.description: - assert_equal(description[attr], ref.description[attr], - "'Description' does not match for {}".format(attr)) + assert description[attr] == ref.description[attr], "'Description' does not match for {}".format(attr) def test_load_from_description(self, reader): description = reader.get_description() new = mda.auxiliary.core.auxreader(**description) - assert_equal(new, reader, - "AuxReader reloaded from description does not match") + assert new == reader, "AuxReader reloaded from description does not match" def assert_auxstep_equal(A, B): From bc8b1cbb39b89fa105a0e9be5c253c9ee4888972 Mon Sep 17 00:00:00 2001 From: Bansal Utkarsh Date: Sun, 16 Jul 2017 13:22:03 +0530 Subject: [PATCH 2/4] Fix raises --- testsuite/MDAnalysisTests/auxiliary/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testsuite/MDAnalysisTests/auxiliary/base.py b/testsuite/MDAnalysisTests/auxiliary/base.py index f0c076ce381..bd8810bea6d 100644 --- a/testsuite/MDAnalysisTests/auxiliary/base.py +++ b/testsuite/MDAnalysisTests/auxiliary/base.py @@ -32,10 +32,10 @@ from MDAnalysisTests.datafiles import (COORDINATES_XTC, COORDINATES_TOPOLOGY) -@pytest.mark.raises(exception=ValueError) def test_get_bad_auxreader_format_raises_ValueError(): # should raise a ValueError when no AuxReaders with match the specified format - mda.auxiliary.core.get_auxreader_for(format='bad-format') + with pytest.raises(ValueError): + mda.auxiliary.core.get_auxreader_for(format='bad-format') class BaseAuxReference(object): From 3ee718a537b4b524e4a5000cb853dcd69102eaaa Mon Sep 17 00:00:00 2001 From: Bansal Utkarsh Date: Sun, 16 Jul 2017 13:48:48 +0530 Subject: [PATCH 3/4] Fix raises errors --- testsuite/MDAnalysisTests/auxiliary/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testsuite/MDAnalysisTests/auxiliary/base.py b/testsuite/MDAnalysisTests/auxiliary/base.py index bd8810bea6d..17f2a5fc215 100644 --- a/testsuite/MDAnalysisTests/auxiliary/base.py +++ b/testsuite/MDAnalysisTests/auxiliary/base.py @@ -172,7 +172,7 @@ def test_next_past_last_step_raises_StopIteration(self, ref, reader): reader[-1] # if we try to move to next step from here, should raise StopIteration with pytest.raises(StopIteration): - reader.next + reader.next() def test_move_to_invalid_step_raises_IndexError(self, ref, reader): # last step is number n_steps -1 ; if we try move to step number From 3da75398bf4a8f993a0e2aa9bac07c58a72d27b0 Mon Sep 17 00:00:00 2001 From: Bansal Utkarsh Date: Tue, 18 Jul 2017 18:42:25 +0530 Subject: [PATCH 4/4] Fix assert calls --- testsuite/MDAnalysisTests/auxiliary/base.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/testsuite/MDAnalysisTests/auxiliary/base.py b/testsuite/MDAnalysisTests/auxiliary/base.py index 17f2a5fc215..a931fb4c13c 100644 --- a/testsuite/MDAnalysisTests/auxiliary/base.py +++ b/testsuite/MDAnalysisTests/auxiliary/base.py @@ -21,15 +21,12 @@ # from __future__ import absolute_import -from six.moves import range -import pytest -import numpy as np -from numpy.testing import assert_almost_equal - - import MDAnalysis as mda - +import numpy as np +import pytest from MDAnalysisTests.datafiles import (COORDINATES_XTC, COORDINATES_TOPOLOGY) +from numpy.testing import assert_almost_equal, assert_equal +from six.moves import range def test_get_bad_auxreader_format_raises_ValueError(): @@ -235,7 +232,7 @@ def test_data_selector(self, ref): data_selector=ref.data_selector) # data should match reference data for each step for i, val in enumerate(reader): - assert val.data == ref.select_data_ref[i], "data for step {0} does not match".format(i) + assert_equal(val.data, ref.select_data_ref[i], "data for step {0} does not match".format(i)) def test_no_constant_dt(self, ref): ## assume we can select time... @@ -325,8 +322,8 @@ def test_iterate_through_trajectory(self, ref): # trajectory here has same dt, offset; so there's a direct correspondence # between frames and steps for i, ts in enumerate(u.trajectory): - assert ts.aux.test == ref.auxsteps[i].data, "representative value does not match when iterating through "\ - "all trajectory timesteps" + assert_equal(ts.aux.test, ref.auxsteps[i].data, + "representative value does not match when iterating through all trajectory timesteps") u.trajectory.close() def test_iterate_as_auxiliary_from_trajectory(self, ref): @@ -337,8 +334,8 @@ def test_iterate_as_auxiliary_from_trajectory(self, ref): # trahectory here has same dt, offset, so there's a direct correspondence # between frames and steps, and iter_as_aux will run through all frames for i, ts in enumerate(u.trajectory.iter_as_aux('test')): - assert ts.aux.test == ref.auxsteps[i].data, "representative value does not match when iterating through "\ - "all trajectory timesteps" + assert_equal(ts.aux.test, ref.auxsteps[i].data, + "representative value does not match when iterating through all trajectory timesteps") u.trajectory.close() def test_get_description(self, ref, reader):