diff --git a/.travis.yml b/.travis.yml index a2c0bce0c76..1fdbab25f06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,9 +23,9 @@ env: - PYTHON_VERSION=2.7 - COVERALLS=false - NOSE_FLAGS="--processes=2 --process-timeout=400 --no-open-files --with-timer --timer-top-n 50" - - NOSE_TEST_LIST="analysis core" + - NOSE_TEST_LIST="analysis" - PYTEST_FLAGS="--disable-pytest-warnings -n 2" - - PYTEST_LIST="testsuite/MDAnalysisTests/lib testsuite/MDAnalysisTests/formats testsuite/MDAnalysisTests/coordinates testsuite/MDAnalysisTests/utils testsuite/MDAnalysisTests/topology testsuite/MDAnalysisTests/auxiliary" + - PYTEST_LIST="testsuite/MDAnalysisTests/lib testsuite/MDAnalysisTests/formats testsuite/MDAnalysisTests/coordinates testsuite/MDAnalysisTests/utils testsuite/MDAnalysisTests/topology testsuite/MDAnalysisTests/auxiliary testsuite/MDAnalysisTests/core" - NOSE_COVERAGE_FILE="nose_coverage" - PYTEST_COVERAGE_FILE="pytest_coverage" - MAIN_CMD="pytest ${PYTEST_LIST} ${PYTEST_FLAGS}; python ./testsuite/MDAnalysisTests/mda_nosetests ${NOSE_TEST_LIST} ${NOSE_FLAGS}" diff --git a/testsuite/MDAnalysisTests/core/test_atom.py b/testsuite/MDAnalysisTests/core/test_atom.py index b7ca4b7f59a..339b82338fe 100644 --- a/testsuite/MDAnalysisTests/core/test_atom.py +++ b/testsuite/MDAnalysisTests/core/test_atom.py @@ -21,6 +21,9 @@ # from __future__ import absolute_import + +from unittest import TestCase + import numpy as np from numpy.testing import ( dec, @@ -29,6 +32,7 @@ assert_equal, assert_raises, ) +import pytest import MDAnalysis as mda from MDAnalysis import NoDataError @@ -40,11 +44,11 @@ from MDAnalysisTests import parser_not_found -class TestAtom(object): +class TestAtom(TestCase): # Legacy tests from before 363 """Tests of Atom.""" - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): """Set up the standard AdK system in implicit solvent.""" @@ -127,7 +131,7 @@ def test_undefined_occupancy(self): self.universe.atoms[0].occupancy -class TestAtomNoForceNoVel(object): +class TestAtomNoForceNoVel(TestCase): def setUp(self): self.u = mda.Universe(XYZ_mini) self.a = self.u.atoms[0] diff --git a/testsuite/MDAnalysisTests/core/test_atomgroup.py b/testsuite/MDAnalysisTests/core/test_atomgroup.py index 8feb7ba3324..05660ff7df4 100644 --- a/testsuite/MDAnalysisTests/core/test_atomgroup.py +++ b/testsuite/MDAnalysisTests/core/test_atomgroup.py @@ -25,6 +25,8 @@ import itertools import os from os import path +from unittest import TestCase + import numpy as np import warnings @@ -56,24 +58,23 @@ ) from MDAnalysisTests import parser_not_found, tempdir, make_Universe +import pytest + # I want to catch all warnings in the tests. If this is not set at the start it # could cause test that check for warnings to fail. warnings.simplefilter('always') class TestDeprecationWarnings(object): - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_AtomGroupUniverse_usage_warning(): + @pytest.mark.skipif(parser_not_found('DCD'), reason='DCD parser not available. Are you using python 3?') + def test_AtomGroupUniverse_usage_warning(self): with warnings.catch_warnings(record=True) as warn: warnings.simplefilter('always') mda.core.AtomGroup.Universe(PSF, DCD) assert_equal(len(warn), 1) - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_old_AtomGroup_init_warns(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_old_AtomGroup_init_warns(self): u = make_Universe(('names',)) at_list = list(u.atoms[:10]) with warnings.catch_warnings(record=True) as warn: @@ -81,10 +82,9 @@ def test_old_AtomGroup_init_warns(): ag = mda.core.groups.AtomGroup(at_list) assert_equal(len(warn), 1) - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_old_AtomGroup_init_works(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_old_AtomGroup_init_works(self): u = make_Universe(('names',)) at_list = list(u.atoms[:10]) ag = mda.core.groups.AtomGroup(at_list) @@ -93,10 +93,9 @@ def test_old_AtomGroup_init_works(): assert_(len(ag) == 10) assert_equal(ag.names, u.atoms[:10].names) - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_old_ResidueGroup_init_warns(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_old_ResidueGroup_init_warns(self): u = make_Universe(('resnames',)) res_list = list(u.residues[:10]) with warnings.catch_warnings(record=True) as warn: @@ -104,10 +103,9 @@ def test_old_ResidueGroup_init_warns(): rg = mda.core.groups.ResidueGroup(res_list) assert_equal(len(warn), 1) - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_old_ResidueGroup_init_works(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_old_ResidueGroup_init_works(self): u = make_Universe(('resnames',)) res_list = list(u.residues[:10]) rg = mda.core.groups.ResidueGroup(res_list) @@ -116,10 +114,9 @@ def test_old_ResidueGroup_init_works(): assert_(len(rg) == 10) assert_equal(rg.resnames, u.residues[:10].resnames) - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_old_SegmentGroup_init_warns(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_old_SegmentGroup_init_warns(self): u = make_Universe(('segids',)) seg_list = list(u.segments[:3]) with warnings.catch_warnings(record=True) as warn: @@ -127,10 +124,9 @@ def test_old_SegmentGroup_init_warns(): sg = mda.core.groups.SegmentGroup(seg_list) assert_equal(len(warn), 1) - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_old_SegmentGroup_init_works(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_old_SegmentGroup_init_works(self): u = make_Universe(('segids',)) seg_list = list(u.segments[:3]) sg = mda.core.groups.SegmentGroup(seg_list) @@ -142,46 +138,45 @@ def test_old_SegmentGroup_init_works(): class TestAtomGroupToTopology(object): """Test the conversion of AtomGroup to TopologyObjects""" - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def setUp(self): - self.u = mda.Universe(PSF, DCD) - - def tearDown(self): - del self.u + @pytest.mark.skipif(parser_not_found('DCD'),'DCD parser not available. Are you using python 3?') + @pytest.fixture() + def u(self): + return mda.Universe(PSF, DCD) - def test_bond(self): - ag = self.u.atoms[:2] + def test_bond(self, u): + ag = u.atoms[:2] bond = ag.bond assert_(isinstance(bond, Bond)) - def test_angle(self): - ag = self.u.atoms[:3] + def test_angle(self, u): + ag = u.atoms[:3] angle = ag.angle assert_(isinstance(angle, Angle)) - def test_dihedral(self): - ag = self.u.atoms[:4] + def test_dihedral(self, u): + ag = u.atoms[:4] dih = ag.dihedral assert_(isinstance(dih, Dihedral)) - def test_improper(self): - ag = self.u.atoms[:4] + def test_improper(self, u): + ag = u.atoms[:4] imp = ag.improper assert_(isinstance(imp, ImproperDihedral)) - def _check_VE(self, btype): - ag = self.u.atoms[:10] + @pytest.mark.parametrize('btype,', [ + 'bond', + 'angle', + 'dihedral', + 'improper' + ]) + def test_VE(self, btype, u): + ag = u.atoms[:10] assert_raises(ValueError, getattr, ag, btype) - def test_VEs(self): - for btype in ('bond', 'angle', 'dihedral', 'improper'): - yield self._check_VE, btype - -class TestAtomGroupWriting(object): - @dec.skipif(parser_not_found('DCD'), +class TestAtomGroupWriting(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.u = mda.Universe(PSF, DCD) @@ -217,12 +212,15 @@ def test_bogus_kwarg_pdb(self): with assert_raises(TypeError): self.u.atoms.write('dummy.pdb', bogus="what?") -class _WriteAtoms(object): +class _WriteAtoms(TestCase): """Set up the standard AdK system in implicit solvent.""" + + __test__ = False + ext = None # override to test various output writers precision = 3 - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.universe = mda.Universe(PSF, DCD) @@ -313,11 +311,17 @@ def test_write_Universe(self): class TestWritePDB(_WriteAtoms): + + __test__ = True + ext = "pdb" precision = 3 class TestWriteGRO(_WriteAtoms): + + __test__ = True + ext = "gro" precision = 2 @@ -328,8 +332,8 @@ def test_flag_convert_length(self): " in the testing suite.)") -class TestAtomGroupTransformations(object): - @dec.skipif(parser_not_found('DCD'), +class TestAtomGroupTransformations(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.u = mda.Universe(PSF, DCD) @@ -455,7 +459,8 @@ def test_transform_translation_and_rotation(self): np.sin(angle) + 1, 1]) -class TestCenter(object): + +class TestCenter(TestCase): def setUp(self): self.u = make_Universe(trajectory=True) self.ag = self.u.atoms[10:30] @@ -487,8 +492,8 @@ def test_center_wrong_shape(self): assert_raises(TypeError, self.ag.center, weights) -class TestSplit(object): - @dec.skipif(parser_not_found('DCD'), +class TestSplit(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.universe = mda.Universe(PSF, DCD) @@ -535,8 +540,8 @@ def test_split_VE(self): assert_raises(ValueError, ag.split, 'something') -class TestWrap(object): - @dec.skipif(parser_not_found('TRZ'), +class TestWrap(TestCase): + @pytest.mark.skipif(parser_not_found('TRZ'), 'TRZ parser not available. Are you using python 3?') def setUp(self): self.u = mda.Universe(TRZ_psf, TRZ) @@ -638,7 +643,7 @@ def _change_atom_check_ag(self, att, atts, vals, ag): assert_equal(vals, other, err_msg="Change to Atoms not reflected in AtomGroup for property: {0}".format(att)) - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def test_attributes(self): u = make_Universe(('names', 'resids', 'segids', 'types', 'altLocs', @@ -727,8 +732,8 @@ def test_adding_empty_ags(self): assert_(len(u.atoms[:3] + u.atoms[[]]) == 3) -class TestDihedralSelections(object): - @dec.skipif(parser_not_found('DCD'), +class TestDihedralSelections(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.universe = mda.Universe(PSF, DCD) @@ -799,8 +804,8 @@ def test_dihedral_chi1(self): assert_almost_equal(sel.dihedral.value(), -58.428127, self.dih_prec) -class TestPBCFlag(object): - @dec.skipif(parser_not_found('TRZ'), +class TestPBCFlag(TestCase): + @pytest.mark.skipif(parser_not_found('TRZ'), 'TRZ parser not available. Are you using python 3?') def setUp(self): self.prec = 3 @@ -910,8 +915,8 @@ def test_override_flag(self): mda.core.flags['use_pbc'] = False -@dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') +@pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_instantselection_termini(): """Test that instant selections work, even for residues that are also termini (Issue 70)""" universe = mda.Universe(PSF, DCD) @@ -919,13 +924,13 @@ def test_instantselection_termini(): del universe -class TestAtomGroup(object): +class TestAtomGroup(TestCase): """Tests of AtomGroup; selections are tested separately. These are from before the big topology rework (aka #363) but are still valid. There is likely lots of duplication between here and other tests. """ - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): """Set up the standard AdK system in implicit solvent.""" @@ -1286,10 +1291,10 @@ def test_atom_order(self): sorted(self.universe.atoms.indices)) -class TestAtomGroupTimestep(object): +class TestAtomGroupTimestep(TestCase): """Tests the AtomGroup.ts attribute (partial timestep)""" - @dec.skipif(parser_not_found('TRZ'), + @pytest.mark.skipif(parser_not_found('TRZ'), 'TRZ parser not available. Are you using python 3?') def setUp(self): self.universe = mda.Universe(TRZ_psf, TRZ) diff --git a/testsuite/MDAnalysisTests/core/test_atomselections.py b/testsuite/MDAnalysisTests/core/test_atomselections.py index 33d35f4041e..f854c244960 100644 --- a/testsuite/MDAnalysisTests/core/test_atomselections.py +++ b/testsuite/MDAnalysisTests/core/test_atomselections.py @@ -22,6 +22,7 @@ from __future__ import division, absolute_import from six.moves import range +from unittest import TestCase import itertools import numpy as np @@ -56,9 +57,11 @@ ) from MDAnalysisTests import parser_not_found, make_Universe +import pytest -class TestSelectionsCHARMM(object): - @dec.skipif(parser_not_found('DCD'), + +class TestSelectionsCHARMM(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): """Set up the standard AdK system in implicit solvent. @@ -317,7 +320,7 @@ def test_global(self): assert_array_equal(ag2.indices, ag1.indices) -class TestSelectionsAMBER(object): +class TestSelectionsAMBER(TestCase): def setUp(self): """Set up AMBER system""" self.universe = MDAnalysis.Universe(PRMpbc, TRJpbc_bz2) @@ -340,7 +343,7 @@ def test_type(self): assert_equal(sel.names, ['HH31', 'HH32', 'HH33', 'HB1', 'HB2', 'HB3']) -class TestSelectionsNAMD(object): +class TestSelectionsNAMD(TestCase): def setUp(self): """Set up NAMD system""" self.universe = MDAnalysis.Universe(PSF_NAMD, PDB_NAMD) @@ -370,7 +373,7 @@ def test_type(self): assert_array_equal(sel.names, ['HN', 'HN', 'HN', 'HH', 'HN']) -class TestSelectionsGRO(object): +class TestSelectionsGRO(TestCase): def setUp(self): """Set up GRO system (implicit types, charges, masses, ...)""" self.universe = MDAnalysis.Universe(GRO) @@ -415,7 +418,7 @@ def test_cyzone(self): assert_equal(len(sel), 1556) -class TestSelectionsXTC(object): +class TestSelectionsXTC(TestCase): def setUp(self): self.universe = MDAnalysis.Universe(TPR,XTC) @@ -432,7 +435,7 @@ def test_same_fragment(self): "Found a different set of atoms when using the 'same fragment as' construct vs. the .fragment prperty") -class TestSelectionsNucleicAcids(object): +class TestSelectionsNucleicAcids(TestCase): def setUp(self): self.universe = MDAnalysis.Universe(NUCL) @@ -478,6 +481,9 @@ class BaseDistanceSelection(object): Cylindrical methods don't use KDTree """ + + __test__ = False + methods = [('kdtree', False), ('distmat', True), ('distmat', False)] @@ -497,16 +503,21 @@ def choosemeth(sel, meth, periodic): return sel - def _check_around(self, meth, periodic): - sel = Parser.parse('around 5.0 resid 1', self.u.atoms) + @pytest.mark.parametrize('meth, periodic', [ + ('kdtree', False), + ('distmat', True), + ('distmat', False) + ]) + def test_around(self,u, meth, periodic): + sel = Parser.parse('around 5.0 resid 1', u.atoms) sel = self.choosemeth(sel, meth, periodic) - result = sel.apply(self.u.atoms) + result = sel.apply(u.atoms) - r1 = self.u.select_atoms('resid 1') + r1 = u.select_atoms('resid 1') cog = r1.center_of_geometry().reshape(1, 3) - box = self.u.dimensions if periodic else None - d = distance_array(self.u.atoms.positions, r1.positions, + box = u.dimensions if periodic else None + d = distance_array(u.atoms.positions, r1.positions, box=box) ref = set(np.where(d < 5.0)[0]) @@ -514,84 +525,88 @@ def _check_around(self, meth, periodic): ref.difference_update(set(r1.indices)) assert_(ref == set(result.indices)) - def test_around(self): - for meth, periodic in self.methods: - yield self._check_around, meth, periodic - - def _check_spherical_layer(self, meth, periodic): - sel = Parser.parse('sphlayer 2.4 6.0 resid 1' , self.u.atoms) + @pytest.mark.parametrize('meth, periodic', [ + ('kdtree', False), + ('distmat', True), + ('distmat', False) + ]) + def test_spherical_layer(self,u, meth, periodic): + sel = Parser.parse('sphlayer 2.4 6.0 resid 1' , u.atoms) sel = self.choosemeth(sel, meth, periodic) - result = sel.apply(self.u.atoms) + result = sel.apply(u.atoms) - r1 = self.u.select_atoms('resid 1') + r1 = u.select_atoms('resid 1') cog = r1.center_of_geometry().reshape(1, 3) - box = self.u.dimensions if periodic else None - d = distance_array(self.u.atoms.positions, cog, box=box) + box = u.dimensions if periodic else None + d = distance_array(u.atoms.positions, cog, box=box) ref = set(np.where((d > 2.4) & (d < 6.0))[0]) assert_(ref == set(result.indices)) - def test_spherical_layer(self): - for meth, periodic in self.methods: - yield self._check_spherical_layer, meth, periodic - - def _check_spherical_zone(self, meth, periodic): - sel = Parser.parse('sphzone 5.0 resid 1', self.u.atoms) + @pytest.mark.parametrize('meth, periodic', [ + ('kdtree', False), + ('distmat', True), + ('distmat', False) + ]) + def test_spherical_zone(self, u, meth, periodic): + sel = Parser.parse('sphzone 5.0 resid 1', u.atoms) sel = self.choosemeth(sel, meth, periodic) - result = sel.apply(self.u.atoms) + result = sel.apply(u.atoms) - r1 = self.u.select_atoms('resid 1') + r1 = u.select_atoms('resid 1') cog = r1.center_of_geometry().reshape(1, 3) - box = self.u.dimensions if periodic else None - d = distance_array(self.u.atoms.positions, cog, box=box) + box = u.dimensions if periodic else None + d = distance_array(u.atoms.positions, cog, box=box) ref = set(np.where(d < 5.0)[0]) assert_(ref == set(result.indices)) - def test_spherical_zone(self): - for meth, periodic in self.methods: - yield self._check_spherical_zone, meth, periodic - def _check_point(self, meth, periodic): - sel = Parser.parse('point 5.0 5.0 5.0 3.0', self.u.atoms) + @pytest.mark.parametrize('meth, periodic', [ + ('kdtree', False), + ('distmat', True), + ('distmat', False) + ]) + def test_point(self,u, meth, periodic): + sel = Parser.parse('point 5.0 5.0 5.0 3.0', u.atoms) sel = self.choosemeth(sel, meth, periodic) - result = sel.apply(self.u.atoms) + result = sel.apply(u.atoms) - box = self.u.dimensions if periodic else None + box = u.dimensions if periodic else None d = distance_array(np.array([[5.0, 5.0, 5.0]], dtype=np.float32), - self.u.atoms.positions, + u.atoms.positions, box=box) ref = set(np.where(d < 3.0)[1]) assert_(ref == set(result.indices)) - def test_point(self): - for meth, periodic in self.methods: - yield self._check_point, meth, periodic - class TestOrthogonalDistanceSelections(BaseDistanceSelection): - @dec.skipif(parser_not_found('TRZ'), - 'TRZ parser not available. Are you using python 3?') - def setUp(self): - self.u = mda.Universe(TRZ_psf, TRZ) - def tearDown(self): - del self.u + __test__ = True + + @pytest.mark.skipif(parser_not_found('TRZ'), 'TRZ parser not available. Are you using python 3?') + @pytest.fixture() + def u(self): + return mda.Universe(TRZ_psf, TRZ) - def _check_cyzone(self, meth, periodic): - sel = Parser.parse('cyzone 5 4 -4 resid 2', self.u.atoms) + @pytest.mark.parametrize('meth, periodic', [ + ('distmat', True), + ('distmat', False) + ]) + def test_cyzone(self, u, meth, periodic): + sel = Parser.parse('cyzone 5 4 -4 resid 2', u.atoms) sel.periodic = periodic - result = sel.apply(self.u.atoms) + result = sel.apply(u.atoms) - other = self.u.select_atoms('resid 2') + other = u.select_atoms('resid 2') pos = other.center_of_geometry() - vecs = self.u.atoms.positions - pos + vecs = u.atoms.positions - pos if periodic: - box = self.u.dimensions[:3] + box = u.dimensions[:3] vecs -= box * np.rint(vecs / box) mask = (vecs[:,2] > -4) & (vecs[:,2] < 4) @@ -599,23 +614,20 @@ def _check_cyzone(self, meth, periodic): radii = vecs[:,0] ** 2 + vecs[:, 1] ** 2 mask &= radii < 5**2 - ref = set(self.u.atoms[mask].indices) + ref = set(u.atoms[mask].indices) assert_(ref == set(result.indices)) - def test_cyzone(self): - for meth, periodic in self.methods[1:]: - yield self._check_cyzone, meth, periodic - class TestTriclinicDistanceSelections(BaseDistanceSelection): - def setUp(self): - self.u = mda.Universe(GRO) - def tearDown(self): - del self.u + __test__ = True + + @pytest.fixture() + def u(self): + return mda.Universe(GRO) -class TestTriclinicSelections(object): +class TestTriclinicSelections(TestCase): """Non-KDTree based selections This system has triclinic geometry so won't use KDTree based selections @@ -785,8 +797,8 @@ def test_props(self): yield self._check_flip, prop, ag, op -class TestBondedSelection(object): - @dec.skipif(parser_not_found('DCD'), +class TestBondedSelection(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.u = mda.Universe(PSF, DCD) @@ -799,8 +811,7 @@ def test_bonded_1(self): assert_(len(ag) == 3) - @staticmethod - def test_nobonds_warns(): + def test_nobonds_warns(self): u = make_Universe(('names',)) # empty bond topology attr @@ -812,41 +823,35 @@ def test_nobonds_warns(): class TestSelectionErrors(object): - def setUp(self): - self.u = make_Universe(('names', 'masses', - 'resids', 'resnames', 'resnums')) - - def tearDown(self): - del self.u - - def selection_fail(self, selstr): - assert_raises(SelectionError, self.u.select_atoms, + @staticmethod + @pytest.fixture() + def universe(): + return make_Universe(('names', 'masses', 'resids', 'resnames', 'resnums')) + + @pytest.mark.parametrize('selstr', [ + 'name and H', # string selection + 'name )', + 'resid abcd', # resid arg parsing selection + 'resnum 7a7', # rangeselection arg parsing + 'resid 1-', + 'prop chicken == tasty', + 'prop chicken <= 7.4', + 'prop mass ^^ 12.0', + 'same this as resid 1', # same selection + 'same resid resname mass 5.0', # same / expect + 'name H and', # check all tokens used + 'naem H', # unkonwn (misplet) opertaor + 'resid and name C', # rangesel not finding vals + 'resnum ', + 'bynum or protein', + 'prop mass < 4.0 hello', # unused token + 'prop mass > 10. and group this', # missing group + 'prop mass > 10. and fullgroup this', # missing fullgroup + ]) + def test_selection_fail(self, selstr, universe): + assert_raises(SelectionError, universe.select_atoms, selstr) - def test_expected_errors(self): - for selstr in [ - 'name and H', # string selection - 'name )', - 'resid abcd', # resid arg parsing selection - 'resnum 7a7', # rangeselection arg parsing - 'resid 1-', - 'prop chicken == tasty', - 'prop chicken <= 7.4', - 'prop mass ^^ 12.0', - 'same this as resid 1', # same selection - 'same resid resname mass 5.0', # same / expect - 'name H and', # check all tokens used - 'naem H', # unkonwn (misplet) opertaor - 'resid and name C', # rangesel not finding vals - 'resnum ', - 'bynum or protein', - 'prop mass < 4.0 hello', # unused token - 'prop mass > 10. and group this', # missing group - 'prop mass > 10. and fullgroup this', # missing fullgroup - ]: - yield self.selection_fail, selstr - - def test_segid_and_resid(): u = make_Universe(('segids', 'resids')) @@ -858,53 +863,42 @@ def test_segid_and_resid(): class TestImplicitOr(object): - def setUp(self): - self.u = make_Universe(('names', 'types', - 'resids', 'resnums', - 'resnames', 'segids')) - - def tearDown(self): - del self.u + @staticmethod + @pytest.fixture() + def universe(): + return make_Universe(('names', 'types','resids', 'resnums', 'resnames', 'segids')) - def _check_sels(self, ref, sel): - ref = self.u.select_atoms(ref) - sel = self.u.select_atoms(sel) + def _check_sels(self, ref, sel, universe): + ref = universe.select_atoms(ref) + sel = universe.select_atoms(sel) assert_array_equal(ref.indices, sel.indices) - def test_string_selections(self): - for ref, sel in ( - ('name NameABA or name NameACA or name NameADA', - 'name NameABA NameACA NameADA'), - ('type TypeE or type TypeD or type TypeB', - 'type TypeE TypeD TypeB'), - ('resname RsC or resname RsY', 'resname RsC RsY'), - ('name NameAB* or name NameACC', 'name NameAB* NameACC'), - ('(name NameABC or name NameABB) and (resname RsD or resname RsF)', - 'name NameABC NameABB and resname RsD RsF'), - ('segid SegA or segid SegC', 'segid SegA SegC'), - ): - yield self._check_sels, ref, sel - - def test_range_selections(self): - # All these selections just use numeric types, - # So loop over what type of selections, - # And apply the same numeric constraints to all - for seltype in ['resid', 'resnum', 'bynum']: - for ref, sel in ( - ('{typ} 1 or {typ} 2', '{typ} 1 2'), - ('{typ} 1:10 or {typ} 22', '{typ} 1:10 22'), - ('{typ} 1:10 or {typ} 20:30', '{typ} 1:10 20:30'), - ('{typ} 1-5 or {typ} 7', '{typ} 1-5 7'), - ('{typ} 1-5 or {typ} 7:10 or {typ} 12', - '{typ} 1-5 7:10 12'), - ('{typ} 1 or {typ} 3 or {typ} 5:10', '{typ} 1 3 5:10'), - ): - yield (self._check_sels, - ref.format(typ=seltype), - sel.format(typ=seltype)) - -class TestICodeSelection(object): + @pytest.mark.parametrize('ref, sel',[ + ('name NameABA or name NameACA or name NameADA', 'name NameABA NameACA NameADA'), + ('type TypeE or type TypeD or type TypeB', 'type TypeE TypeD TypeB'), + ('resname RsC or resname RsY', 'resname RsC RsY'), + ('name NameAB* or name NameACC', 'name NameAB* NameACC'), + ('segid SegA or segid SegC', 'segid SegA SegC'), + ('(name NameABC or name NameABB) and (resname RsD or resname RsF)', 'name NameABC NameABB and resname RsD RsF'), + ]) + def test_string_selections(self, ref, sel, universe): + self._check_sels(ref, sel, universe) + + @pytest.mark.parametrize("seltype", ['resid', 'resnum', 'bynum']) + @pytest.mark.parametrize('ref, sel', [ + ('{typ} 1 or {typ} 2', '{typ} 1 2'), + ('{typ} 1:10 or {typ} 22', '{typ} 1:10 22'), + ('{typ} 1:10 or {typ} 20:30', '{typ} 1:10 20:30'), + ('{typ} 1-5 or {typ} 7', '{typ} 1-5 7'), + ('{typ} 1-5 or {typ} 7:10 or {typ} 12', '{typ} 1-5 7:10 12'), + ('{typ} 1 or {typ} 3 or {typ} 5:10', '{typ} 1 3 5:10'), + ]) + def test_range_selections(self, seltype, ref, sel, universe): + self._check_sels(ref.format(typ=seltype), sel.format(typ=seltype), universe) + + +class TestICodeSelection(TestCase): def setUp(self): self.u = mda.Universe(PDB_icodes) diff --git a/testsuite/MDAnalysisTests/core/test_group_traj_access.py b/testsuite/MDAnalysisTests/core/test_group_traj_access.py index 363defbb785..bbf61181e9c 100644 --- a/testsuite/MDAnalysisTests/core/test_group_traj_access.py +++ b/testsuite/MDAnalysisTests/core/test_group_traj_access.py @@ -21,6 +21,8 @@ # from __future__ import division, absolute_import +from unittest import TestCase + import numpy as np from numpy.testing import ( assert_, @@ -274,7 +276,7 @@ def test_all(self): yield self._check_atom_force_setting, u, force -class TestAtom_ForceVelocity(object): +class TestAtom_ForceVelocity(TestCase): def setUp(self): self.u = MDAnalysis.Universe(PDB_xvf, TRR_xvf) self.a = self.u.atoms[0] @@ -328,7 +330,7 @@ def test_for_iteration(self): assert_array_equal(val, ref) -class TestGROVelocities(object): +class TestGROVelocities(TestCase): def setUp(self): #reference velocities for the full 6-atom test case: self.reference_velocities = np.array( @@ -363,7 +365,7 @@ def testParse_velocities(self): err_msg="problem reading .gro file velocities") -class TestTRRForces(object): +class TestTRRForces(TestCase): def setUp(self): self.universe = MDAnalysis.Universe(PDB_xvf, TRR_xvf) # extracted protein forces with g_traj into cobrotoxin_protein_forces.xvg.bz2 @@ -396,7 +398,7 @@ def setUp(self): self.reference_mean_protein_force = self.reference_mean_protein_force_native -class TestAtomGroupVelocities(object): +class TestAtomGroupVelocities(TestCase): """Tests of velocity-related functions in AtomGroup""" def setUp(self): self.universe = MDAnalysis.Universe(GRO, TRR) @@ -430,7 +432,7 @@ def test_set_velocities(self): err_msg="messages were not set to new value") -class TestAtomGroupForces(object): +class TestAtomGroupForces(TestCase): """Tests of velocity-related functions in AtomGroup""" def setUp(self): self.universe = MDAnalysis.Universe(COORDINATES_XYZ, COORDINATES_TRR) diff --git a/testsuite/MDAnalysisTests/core/test_groups.py b/testsuite/MDAnalysisTests/core/test_groups.py index e962b045fb7..7b38277aa67 100644 --- a/testsuite/MDAnalysisTests/core/test_groups.py +++ b/testsuite/MDAnalysisTests/core/test_groups.py @@ -20,9 +20,11 @@ # J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 # from __future__ import absolute_import + from six.moves import range import itertools +from unittest import TestCase import numpy as np from numpy.testing import ( dec, @@ -32,6 +34,7 @@ assert_raises, assert_warns, ) +import pytest import operator import six @@ -44,7 +47,7 @@ from MDAnalysis.topology.base import change_squash -class TestGroupProperties(object): +class TestGroupProperties(TestCase): """ Test attributes of all groups """ @@ -308,7 +311,7 @@ def _check_contains_wronglevel(self, group, group2): assert_(not group[2] in group2) -class TestGroupLevelTransition(object): +class TestGroupLevelTransition(TestCase): """Test moving between different hierarchy levels AtomGroup @@ -527,8 +530,7 @@ def test_comparions(self): class TestMetaclassMagic(object): # tests for the weird voodoo we do with metaclasses - @staticmethod - def test_new_class(): + def test_new_class(self): u = make_Universe(trajectory=True) # should be able to subclass AtomGroup as normal @@ -544,7 +546,7 @@ class NewGroup(groups.AtomGroup): assert_array_equal(ng.positions, ag.positions) -class TestGroupBy(object): +class TestGroupBy(TestCase): # tests for the method 'groupby' def setUp(self): self.u = make_Universe(('types', 'charges', 'resids')) @@ -579,8 +581,8 @@ def test_groupby_int(self): -class TestReprs(object): - @dec.skipif(parser_not_found('DCD'), +class TestReprs(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.u = mda.Universe(PSF, DCD) @@ -983,13 +985,12 @@ def _hash_not_equal(a, b): class TestAtomGroup(object): - @staticmethod - def test_PDB_atom_repr(): + def test_PDB_atom_repr(self): u = make_Universe(extras=('altLocs', 'names', 'types', 'resnames', 'resids', 'segids')) assert_equal("", u.atoms[0].__repr__()) -class TestInstantSelectorDeprecationWarnings(object): +class TestInstantSelectorDeprecationWarnings(TestCase): def setUp(self): self.u = make_Universe(("resids", "resnames", "segids", "names")) diff --git a/testsuite/MDAnalysisTests/core/test_index_dtype.py b/testsuite/MDAnalysisTests/core/test_index_dtype.py index 752b78bc86f..b632b48a540 100644 --- a/testsuite/MDAnalysisTests/core/test_index_dtype.py +++ b/testsuite/MDAnalysisTests/core/test_index_dtype.py @@ -28,6 +28,8 @@ """ from __future__ import absolute_import +from unittest import TestCase + import numpy as np from numpy.testing import ( assert_, @@ -35,7 +37,7 @@ from MDAnalysisTests import make_Universe -class TestIndexDtype(object): +class TestIndexDtype(TestCase): def setUp(self): self.u = make_Universe() diff --git a/testsuite/MDAnalysisTests/core/test_residue.py b/testsuite/MDAnalysisTests/core/test_residue.py index 6473567e675..b7173fa4e16 100644 --- a/testsuite/MDAnalysisTests/core/test_residue.py +++ b/testsuite/MDAnalysisTests/core/test_residue.py @@ -20,11 +20,15 @@ # J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 # from __future__ import absolute_import + +from unittest import TestCase + from numpy.testing import ( dec, assert_, assert_equal, ) +import pytest import MDAnalysis as mda @@ -32,9 +36,9 @@ from MDAnalysisTests.datafiles import PSF, DCD -class TestResidue(object): +class TestResidue(TestCase): # Legacy tests from before 363 - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.universe = mda.Universe(PSF, DCD) diff --git a/testsuite/MDAnalysisTests/core/test_residuegroup.py b/testsuite/MDAnalysisTests/core/test_residuegroup.py index d4325d9ca1c..826f6d74091 100644 --- a/testsuite/MDAnalysisTests/core/test_residuegroup.py +++ b/testsuite/MDAnalysisTests/core/test_residuegroup.py @@ -28,7 +28,8 @@ assert_equal, assert_raises, ) -from unittest import skip +import pytest +from unittest import skip, TestCase import MDAnalysis as mda @@ -36,7 +37,7 @@ from MDAnalysisTests import parser_not_found -class TestSequence(object): +class TestSequence(TestCase): # all tests are done with the AdK system (PSF and DCD) sequence: # http://www.uniprot.org/uniprot/P69441.fasta # >sp|P69441|KAD_ECOLI Adenylate kinase OS=Escherichia coli (strain K12) GN=adk PE=1 SV=1 @@ -47,7 +48,7 @@ class TestSequence(object): "YYSKEAEAGNTKYAKVDGTKPVAEVRADLEKILG" ) - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.u = mda.Universe(PSF, DCD) @@ -96,9 +97,9 @@ def test_format_TE(self): assert_raises(TypeError, self.u.residues.sequence, format='chicken') -class TestResidueGroup(object): +class TestResidueGroup(TestCase): # Legacy tests from before 363 - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): """Set up the standard AdK system in implicit solvent.""" diff --git a/testsuite/MDAnalysisTests/core/test_segment.py b/testsuite/MDAnalysisTests/core/test_segment.py index 86ac2fb997e..46b39388acc 100644 --- a/testsuite/MDAnalysisTests/core/test_segment.py +++ b/testsuite/MDAnalysisTests/core/test_segment.py @@ -21,12 +21,15 @@ # from __future__ import absolute_import +from unittest import TestCase + from numpy.testing import ( dec, assert_, assert_equal, ) from nose.plugins.attrib import attr +import pytest import MDAnalysis as mda @@ -34,7 +37,7 @@ from MDAnalysis.tests.datafiles import PSF, DCD -class TestSegment(object): +class TestSegment(TestCase): def setUp(self): self.universe = make_Universe(('segids',)) self.sB = self.universe.segments[1] @@ -63,8 +66,8 @@ def test_atom_order(self): sorted(self.universe.segments[0].atoms.indices)) @attr("issue") -@dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') +@pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_generated_residueselection(): """Test that a generated residue group always returns a ResidueGroup (Issue 47) unless there is a single residue (Issue 363 change)""" diff --git a/testsuite/MDAnalysisTests/core/test_segmentgroup.py b/testsuite/MDAnalysisTests/core/test_segmentgroup.py index 33b75f338bf..0cdcd3355e3 100644 --- a/testsuite/MDAnalysisTests/core/test_segmentgroup.py +++ b/testsuite/MDAnalysisTests/core/test_segmentgroup.py @@ -26,7 +26,8 @@ assert_, assert_equal, ) -from unittest import skip +import pytest +from unittest import TestCase import MDAnalysis as mda @@ -34,9 +35,9 @@ from MDAnalysisTests import parser_not_found -class TestSegmentGroup(object): +class TestSegmentGroup(TestCase): # Legacy tests from before 363 - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): """Set up the standard AdK system in implicit solvent.""" diff --git a/testsuite/MDAnalysisTests/core/test_topology.py b/testsuite/MDAnalysisTests/core/test_topology.py index 9f4a07e9f70..35e8153393e 100644 --- a/testsuite/MDAnalysisTests/core/test_topology.py +++ b/testsuite/MDAnalysisTests/core/test_topology.py @@ -4,9 +4,10 @@ Should work with both a single or an array of indices """ from __future__ import absolute_import + from six.moves import zip import itertools - +from unittest import TestCase from numpy.testing import ( assert_, assert_equal, @@ -28,7 +29,7 @@ import MDAnalysis -class TestTransTable(object): +class TestTransTable(TestCase): def setUp(self): # Reference data self.Ridx = np.array([0, 0, 2, 2, 1, 1, 3, 3, 1, 2]) @@ -155,7 +156,7 @@ def test_move_residue_simple(self): assert_equal(len(tt.segments2residues_1d(1)), 1) -class TestLevelMoves(object): +class TestLevelMoves(TestCase): """Tests for moving atoms/residues between residues/segments @@ -448,7 +449,7 @@ def test_move_residuegroup_list_TE(self): setattr, self.u.residues[:3], 'segments', [1, 2, 3]) -class TestDownshiftArrays(object): +class TestDownshiftArrays(TestCase): def setUp(self): # test for square and ragged shapes # square shapes sometimes simplify to 2d array @@ -648,7 +649,7 @@ def test_add_Segment_with_attr(self): assert_(new_seg.segid == 'New') -class TestTopologyGuessed(object): +class TestTopologyGuessed(TestCase): def setUp(self): names = self.names = ta.Atomnames(np.array(['A', 'B', 'C'], dtype=object)) types = self.types = ta.Atomtypes(np.array(['X', 'Y', 'Z'], dtype=object), @@ -684,23 +685,20 @@ def test_read(self): class TestTopologyCreation(object): - @staticmethod - def test_make_topology_no_attrs(): + def test_make_topology_no_attrs(self): # should still make attrs list when attrs=None top = Topology() assert_(hasattr(top, 'attrs')) assert_(isinstance(top.attrs, list)) - @staticmethod - def test_resindex_VE(): + def test_resindex_VE(self): # wrong sized atom to residue array AR = np.arange(10) assert_raises(ValueError, Topology, n_atoms=5, atom_resindex=AR) - @staticmethod - def test_segindex_VE(): + def test_segindex_VE(self): # wrong sized residue to segment array AR = np.arange(5) RS = np.arange(10) diff --git a/testsuite/MDAnalysisTests/core/test_topologyattrs.py b/testsuite/MDAnalysisTests/core/test_topologyattrs.py index c5687194627..2a4156fca8d 100644 --- a/testsuite/MDAnalysisTests/core/test_topologyattrs.py +++ b/testsuite/MDAnalysisTests/core/test_topologyattrs.py @@ -23,6 +23,9 @@ """ from __future__ import division, absolute_import + +from unittest import TestCase + import numpy as np from numpy.testing import ( @@ -33,6 +36,7 @@ assert_raises, ) from nose.tools import raises +import pytest from MDAnalysisTests.plugins.knownfailure import knownfailure from MDAnalysisTests.datafiles import PSF, DCD from MDAnalysisTests import parser_not_found, make_Universe @@ -60,7 +64,7 @@ def ix(self): return self._ix -class TopologyAttrMixin(object): +class TopologyAttrMixin(TestCase): """Mixin to test the common elements to all TopologyAttrs. 10 atoms @@ -68,6 +72,9 @@ class TopologyAttrMixin(object): 2 segments """ + + __test__ = False + # Reference data Ridx = np.array([0, 0, 2, 2, 1, 1, 3, 3, 1, 2]) Sidx = np.array([0, 1, 1, 0]) @@ -90,11 +97,13 @@ class TestAtomAttr(TopologyAttrMixin): """Test atom-level TopologyAttrs. """ + + __test__ = True + values = np.array([7, 3, 69, 9993, 84, 194, 263, 501, 109, 5873]) attrclass = tpattrs.AtomAttr - @staticmethod - def test_set_atom_VE(): + def test_set_atom_VE(self): u = make_Universe(('names',)) at = u.atoms[0] @@ -148,11 +157,14 @@ def test_get_segments(self): class TestAtomids(TestAtomAttr): + + __test__ = True + attrclass = tpattrs.Atomids -class TestIndicesClasses(object): - @dec.skipif(parser_not_found('DCD'), +class TestIndicesClasses(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.u = mda.Universe(PSF, DCD) @@ -174,12 +186,18 @@ def test_cant_set_segment_indices(self): class TestAtomnames(TestAtomAttr): + + __test__ = True + values = np.array(['O', 'C', 'CA', 'N', 'CB', 'CG', 'CD', 'NA', 'CL', 'OW'], dtype=np.object) attrclass = tpattrs.Atomnames class AggregationMixin(TestAtomAttr): + + __test__ = False + def test_get_residues(self): assert_array_equal(self.attr.get_residues(DummyGroup([2, 1])), np.array([self.values[[2, 3, 9]].sum(), @@ -195,10 +213,16 @@ def test_get_segment(self): class TestMasses(AggregationMixin): + + __test__ = True + attrclass = tpattrs.Masses class TestCharges(AggregationMixin): + + __test__ = True + values = np.array([+2, -1, 0, -1, +1, +2, 0, 0, 0, -1]) attrclass = tpattrs.Charges @@ -207,11 +231,13 @@ class TestResidueAttr(TopologyAttrMixin): """Test residue-level TopologyAttrs. """ + + __test__ = True + values = np.array([15.2, 395.6, 0.1, 9.8]) attrclass = tpattrs.ResidueAttr - @staticmethod - def test_set_residue_VE(): + def test_set_residue_VE(self): u = make_Universe(('resnames',)) res = u.residues[0] @@ -253,6 +279,9 @@ def test_get_segments(self): class TestResids(TestResidueAttr): + + __test__ = True + values = np.array([10, 11, 18, 20]) attrclass = tpattrs.Resids @@ -279,6 +308,9 @@ def test_set_residues(self): class TestResnames(TestResidueAttr): + + __test__ = True + values = np.array(['VAL', 'LYS', 'VAL', 'POPG'], dtype=np.object) attrclass = tpattrs.Resnames @@ -331,11 +363,13 @@ class TestSegmentAttr(TopologyAttrMixin): """Test segment-level TopologyAttrs. """ + + __test__ = True + values = np.array([-0.19, 500]) attrclass = tpattrs.SegmentAttr - @staticmethod - def test_set_segment_VE(): + def test_set_segment_VE(self): u = make_Universe(('segids',)) seg = u.segments[0] @@ -372,8 +406,8 @@ def test_set_segments_VE(self): assert_raises(ValueError, self.attr.set_segments, dg, np.array([4, 5, 6, 7])) -class TestAttr(object): - @dec.skipif(parser_not_found('DCD'), +class TestAttr(TestCase): + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.universe = mda.Universe(PSF, DCD) diff --git a/testsuite/MDAnalysisTests/core/test_topologyobjects.py b/testsuite/MDAnalysisTests/core/test_topologyobjects.py index 9efbb575945..7d5486a296c 100644 --- a/testsuite/MDAnalysisTests/core/test_topologyobjects.py +++ b/testsuite/MDAnalysisTests/core/test_topologyobjects.py @@ -21,6 +21,8 @@ # from __future__ import absolute_import +from unittest import TestCase + import numpy as np from numpy.testing import ( dec, @@ -30,6 +32,7 @@ assert_, assert_raises, ) +import pytest import MDAnalysis as mda from MDAnalysis.lib.distances import calc_bonds, calc_angles, calc_dihedrals @@ -44,7 +47,7 @@ from MDAnalysisTests import parser_not_found -class TestTopologyObjects(object): +class TestTopologyObjects(TestCase): """Test the base TopologyObject funtionality init @@ -55,7 +58,7 @@ class TestTopologyObjects(object): len """ - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.precision = 3 # rather lenient but see #271 @@ -166,7 +169,7 @@ def test_improper(self): assert_almost_equal(imp.value(), -3.8370631, self.precision) -class TestTopologyGroup(object): +class TestTopologyGroup(TestCase): """Tests TopologyDict and TopologyGroup classes with psf input""" def setUp(self): @@ -554,13 +557,13 @@ def test_atom4(self): assert_(atom == bond[3]) -class TestTopologyGroup_Cython(object): +class TestTopologyGroup_Cython(TestCase): """ Check that the shortcut to all cython functions: - work (return proper values) - catch errors """ - @dec.skipif(parser_not_found('DCD'), + @pytest.mark.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') def setUp(self): self.u = mda.Universe(PSF, DCD) diff --git a/testsuite/MDAnalysisTests/core/test_universe.py b/testsuite/MDAnalysisTests/core/test_universe.py index 6e17d5bc9ef..907f8e80d9c 100644 --- a/testsuite/MDAnalysisTests/core/test_universe.py +++ b/testsuite/MDAnalysisTests/core/test_universe.py @@ -20,9 +20,13 @@ # J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 # from __future__ import absolute_import, print_function + + from six.moves import cPickle import os +from unittest import TestCase + try: from cStringIO import StringIO except: @@ -39,6 +43,7 @@ assert_raises, ) from nose.plugins.attrib import attr +import pytest from MDAnalysisTests import make_Universe from MDAnalysisTests.datafiles import ( @@ -79,49 +84,41 @@ def parse(self): class TestUniverseCreation(object): # tests concerning Universe creation and errors encountered - @staticmethod - def test_load(): + def test_load(self): # Universe(top, trj) u = mda.Universe(PSF, PDB_small) assert_equal(len(u.atoms), 3341, "Loading universe failed somehow") - @staticmethod - def test_load_topology_stringio(): + def test_load_topology_stringio(self): u = mda.Universe(StringIO(CHOL_GRO), format='GRO') assert_equal(len(u.atoms), 8, "Loading universe from StringIO failed somehow") assert_equal(u.trajectory.ts.positions[0], np.array([65.580002, 29.360001, 40.050003], dtype=np.float32)) - @staticmethod - def test_load_trajectory_stringio(): + def test_load_trajectory_stringio(self): u = mda.Universe(StringIO(CHOL_GRO), StringIO(CHOL_GRO), format='GRO', topology_format='GRO') assert_equal(len(u.atoms), 8, "Loading universe from StringIO failed somehow") - @staticmethod - def test_make_universe_no_args(): + def test_make_universe_no_args(self): # universe creation without args should work u = mda.Universe() assert_(isinstance(u, mda.Universe)) assert_(u.atoms == None) - @staticmethod - def test_make_universe_stringio_no_format(): + def test_make_universe_stringio_no_format(self): # Loading from StringIO without format arg should raise TypeError assert_raises(TypeError, mda.Universe, StringIO(CHOL_GRO)) - @staticmethod - def test_Universe_no_trajectory_AE(): + def test_Universe_no_trajectory_AE(self): # querying trajectory without a trajectory loaded (only topology) u = make_Universe() assert_raises(AttributeError, getattr, u, 'trajectory') - @staticmethod - def test_Universe_topology_unrecognizedformat_VE(): + def test_Universe_topology_unrecognizedformat_VE(self): assert_raises(ValueError, mda.Universe, 'some.weird.not.pdb.but.converted.xtc') - @staticmethod - def test_Universe_topology_unrecognizedformat_VE_msg(): + def test_Universe_topology_unrecognizedformat_VE_msg(self): try: mda.Universe('some.weird.not.pdb.but.converted.xtc') except ValueError as e: @@ -129,13 +126,11 @@ def test_Universe_topology_unrecognizedformat_VE_msg(): else: raise AssertionError - @staticmethod - def test_Universe_topology_IE(): + def test_Universe_topology_IE(self): assert_raises(IOError, mda.Universe, 'thisfile', topology_format=IOErrorParser) - @staticmethod - def test_Universe_topology_IE_msg(): + def test_Universe_topology_IE_msg(self): # should get the original error, as well as Universe error try: mda.Universe('thisfile', topology_format=IOErrorParser) @@ -145,8 +140,7 @@ def test_Universe_topology_IE_msg(): else: raise AssertionError - @staticmethod - def test_Universe_filename_IE_msg(): + def test_Universe_filename_IE_msg(self): # check for non existent file try: mda.Universe('thisfile.xml') @@ -155,8 +149,7 @@ def test_Universe_filename_IE_msg(): else: raise AssertionError - @staticmethod - def test_Universe_invalidfile_IE_msg(): + def test_Universe_invalidfile_IE_msg(self): # check for invalid file (something with the wrong content) temp_dir = TempDir() with open(os.path.join(temp_dir.name, 'invalid.file.tpr'), 'w') as temp_file: @@ -170,8 +163,7 @@ def test_Universe_invalidfile_IE_msg(): finally: temp_dir.dissolve() - @staticmethod - def test_Universe_invalidpermissionfile_IE_msg(): + def test_Universe_invalidpermissionfile_IE_msg(self): # check for file with invalid permissions (eg. no read access) temp_dir = TempDir() temp_file = os.path.join(temp_dir.name, 'permission.denied.tpr') @@ -187,15 +179,13 @@ def test_Universe_invalidpermissionfile_IE_msg(): finally: temp_dir.dissolve() - @staticmethod - def test_load_new_VE(): + def test_load_new_VE(self): u = mda.Universe() assert_raises(TypeError, u.load_new, 'thisfile', format='soup') - @staticmethod - def test_universe_kwargs(): + def test_universe_kwargs(self): u = mda.Universe(PSF, PDB_small, fake_kwarg=True) assert_equal(len(u.atoms), 3341, "Loading universe failed somehow") @@ -208,8 +198,7 @@ def test_universe_kwargs(): assert_(u2.kwargs['fake_kwarg'] is True) assert_equal(u.kwargs, u2.kwargs) - @staticmethod - def test_universe_topology_class_with_coords(): + def test_universe_topology_class_with_coords(self): u = mda.Universe(PSF, PDB_small) u2 = mda.Universe(u._topology, PDB_small) assert_(isinstance(u2.trajectory, type(u.trajectory))) @@ -219,8 +208,8 @@ def test_universe_topology_class_with_coords(): class TestUniverse(object): # older tests, still useful - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_load_bad_topology(self): # tests that Universe builds produce the right error message def bad_load(): @@ -229,15 +218,15 @@ def bad_load(): assert_raises(ValueError, bad_load) @attr('issue') - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_load_new(self): u = mda.Universe(PSF, DCD) u.load_new(PDB_small) assert_equal(len(u.trajectory), 1, "Failed to load_new(PDB)") - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_load_new_TypeError(self): u = mda.Universe(PSF, DCD) @@ -253,8 +242,8 @@ def test_load_structure(self): assert_equal(len(u.atoms), 3341, "Loading universe failed somehow") assert_almost_equal(u.atoms.positions, ref.atoms.positions) - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_load_multiple_list(self): # Universe(top, [trj, trj, ...]) ref = mda.Universe(PSF, DCD) @@ -262,8 +251,8 @@ def test_load_multiple_list(self): assert_equal(len(u.atoms), 3341, "Loading universe failed somehow") assert_equal(u.trajectory.n_frames, 2 * ref.trajectory.n_frames) - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_load_multiple_args(self): # Universe(top, trj, trj, ...) ref = mda.Universe(PSF, DCD) @@ -271,14 +260,14 @@ def test_load_multiple_args(self): assert_equal(len(u.atoms), 3341, "Loading universe failed somehow") assert_equal(u.trajectory.n_frames, 2 * ref.trajectory.n_frames) - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_pickle_raises_NotImplementedError(self): u = mda.Universe(PSF, DCD) assert_raises(NotImplementedError, cPickle.dumps, u, protocol=cPickle.HIGHEST_PROTOCOL) - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') def test_set_dimensions(self): u = mda.Universe(PSF, DCD) box = np.array([10, 11, 12, 90, 90, 90]) @@ -301,7 +290,7 @@ def test_chainid_quick_select(): assert_(len(u.D.atoms) == 7) -class TestGuessBonds(object): +class TestGuessBonds(TestCase): """Test the AtomGroup methed guess_bonds This needs to be done both from Universe creation (via kwarg) and AtomGroup @@ -395,54 +384,48 @@ def test_atomgroup_guess_bonds_with_vdwradii(self): class TestInMemoryUniverse(object): - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_reader_w_timeseries(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_reader_w_timeseries(self): universe = mda.Universe(PSF, DCD, in_memory=True) assert_equal(universe.trajectory.timeseries(universe.atoms).shape, (3341, 98, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - def test_reader_wo_timeseries(): + def test_reader_wo_timeseries(self): universe = mda.Universe(GRO, TRR, in_memory=True) assert_equal(universe.trajectory.timeseries(universe.atoms).shape, (47681, 10, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_reader_w_timeseries_frame_interval(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_reader_w_timeseries_frame_interval(self): universe = mda.Universe(PSF, DCD, in_memory=True, in_memory_step=10) assert_equal(universe.trajectory.timeseries(universe.atoms).shape, (3341, 10, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - def test_reader_wo_timeseries_frame_interval(): + def test_reader_wo_timeseries_frame_interval(self): universe = mda.Universe(GRO, TRR, in_memory=True, in_memory_step=3) assert_equal(universe.trajectory.timeseries(universe.atoms).shape, (47681, 4, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_existing_universe(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_existing_universe(self): universe = mda.Universe(PDB_small, DCD) universe.transfer_to_memory() assert_equal(universe.trajectory.timeseries(universe.atoms).shape, (3341, 98, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_frame_interval_convention(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_frame_interval_convention(self): universe1 = mda.Universe(PSF, DCD) array1 = universe1.trajectory.timeseries(skip=10) universe2 = mda.Universe(PSF, DCD, in_memory=True, @@ -451,10 +434,9 @@ def test_frame_interval_convention(): assert_equal(array1, array2, err_msg="Unexpected differences between arrays.") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_slicing_with_start_stop(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_slicing_with_start_stop(self): universe = MDAnalysis.Universe(PDB_small, DCD) # Skip only the last frame universe.transfer_to_memory(start=10, stop=20) @@ -462,10 +444,9 @@ def test_slicing_with_start_stop(): (3341, 10, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_slicing_without_start(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_slicing_without_start(self): universe = MDAnalysis.Universe(PDB_small, DCD) # Skip only the last frame universe.transfer_to_memory(stop=10) @@ -473,10 +454,9 @@ def test_slicing_without_start(): (3341, 10, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_slicing_without_stop(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_slicing_without_stop(self): universe = MDAnalysis.Universe(PDB_small, DCD) # Skip only the last frame universe.transfer_to_memory(start=10) @@ -485,10 +465,9 @@ def test_slicing_without_stop(): (3341, 88, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_slicing_step_without_start_stop(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_slicing_step_without_start_stop(self): universe = MDAnalysis.Universe(PDB_small, DCD) # Skip only the last frame universe.transfer_to_memory(step=2) @@ -497,10 +476,9 @@ def test_slicing_step_without_start_stop(): (3341, 49, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_slicing_step_with_start_stop(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_slicing_step_with_start_stop(self): universe = MDAnalysis.Universe(PDB_small, DCD) # Skip only the last frame universe.transfer_to_memory(start=10, stop=30, step=2) @@ -509,10 +487,9 @@ def test_slicing_step_with_start_stop(): (3341, 10, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_slicing_step_dt(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_slicing_step_dt(self): universe = MDAnalysis.Universe(PDB_small, DCD) times = [ts.time for ts in universe.trajectory] universe.transfer_to_memory(step=2) @@ -521,10 +498,9 @@ def test_slicing_step_dt(): err_msg="Unexpected in-memory timestep: " + "dt not updated with step information") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_slicing_negative_start(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_slicing_negative_start(self): universe = MDAnalysis.Universe(PDB_small, DCD) # Skip only the last frame universe.transfer_to_memory(start=-10) @@ -533,10 +509,9 @@ def test_slicing_negative_start(): (3341, 10, 3), err_msg="Unexpected shape of trajectory timeseries") - @staticmethod - @dec.skipif(parser_not_found('DCD'), - 'DCD parser not available. Are you using python 3?') - def test_slicing_negative_stop(): + @pytest.mark.skipif(parser_not_found('DCD'), + reason='DCD parser not available. Are you using python 3?') + def test_slicing_negative_stop(self): universe = MDAnalysis.Universe(PDB_small, DCD) # Skip only the last frame universe.transfer_to_memory(stop=-20) @@ -550,8 +525,8 @@ class TestCustomReaders(object): """ Can pass a reader as kwarg on Universe creation """ - @dec.skipif(parser_not_found('TRZ'), - 'TRZ parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('TRZ'), + reason='TRZ parser not available. Are you using python 3?') def test_custom_reader(self): # check that reader passing works u = mda.Universe(TRZ_psf, TRZ, format=MDAnalysis.coordinates.TRZ.TRZReader) @@ -572,15 +547,15 @@ def test_custom_reader_singleframe_2(self): topology_format=T, format=R) assert_equal(len(u.atoms), 6) - @dec.skipif(parser_not_found('TRZ'), - 'TRZ parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('TRZ'), + reason='TRZ parser not available. Are you using python 3?') def test_custom_parser(self): # topology reader passing works u = mda.Universe(TRZ_psf, TRZ, topology_format=MDAnalysis.topology.PSFParser.PSFParser) assert_equal(len(u.atoms), 8184) - @dec.skipif(parser_not_found('TRZ'), - 'TRZ parser not available. Are you using python 3?') + @pytest.mark.skipif(parser_not_found('TRZ'), + reason='TRZ parser not available. Are you using python 3?') def test_custom_both(self): # use custom for both u = mda.Universe(TRZ_psf, TRZ, format=MDAnalysis.coordinates.TRZ.TRZReader, diff --git a/testsuite/MDAnalysisTests/core/test_updating_atomgroup.py b/testsuite/MDAnalysisTests/core/test_updating_atomgroup.py index c9eec3f6aae..a4cc06af059 100644 --- a/testsuite/MDAnalysisTests/core/test_updating_atomgroup.py +++ b/testsuite/MDAnalysisTests/core/test_updating_atomgroup.py @@ -21,6 +21,8 @@ # from __future__ import absolute_import +from unittest import TestCase + import numpy as np from numpy.testing import ( assert_, @@ -37,7 +39,8 @@ import MDAnalysis import MDAnalysis as mda -class TestUpdatingSelection(object): + +class TestUpdatingSelection(TestCase): def setUp(self): self.u = mda.Universe(GRO, XTC) self.ag = self.u.select_atoms( @@ -102,7 +105,7 @@ def test_kwarg_check(self): assert_raises(TypeError, self.u.select_atoms, "group updating", {"updating":True}) -class TestUpdatingSelectionNotraj(object): +class TestUpdatingSelectionNotraj(TestCase): def setUp(self): self.u = mda.Universe(PSF) self.ag = self.u.select_atoms("name N*") @@ -164,7 +167,7 @@ def _read_frame(self, frame): return self._read_next_frame -class TestUAGCallCount(object): +class TestUAGCallCount(TestCase): # make sure updates are only called when required! # # these tests check that potentially expensive selection operations are only @@ -201,7 +204,7 @@ def test_updated_when_next(self): assert_(mock_update.call_count == 1) -class TestDynamicUAG(object): +class TestDynamicUAG(TestCase): def setUp(self): self.u = u = make_Universe(('names',)) u.trajectory = UAGReader(125)