Skip to content

Commit

Permalink
Port core module to pytest (#1443)
Browse files Browse the repository at this point in the history
* Fix test_group_traj_access.py
* Move core to PYTEST_LIST
* Fix test_atom.py
* Fix test_atomgroup.py
* Fix test_atomselections.py
* Fix test_groups.py
* Fix test_index_dtype.py
* Fix test_residue.py
* Fix test_residuegroup.py
* Fix test_segment.py
* Fix test_segmentgroup.py
* Fix test_topology.py
* Fix test_topologyattrs.py
* Fix test_topologyobjects.py
* Fix test_universe.py
* Fix test_updating_atomgroup.py
* Use @pytest.mark.skipif in place of @dec.skipif
  • Loading branch information
utkbansal authored and kain88-de committed Jul 2, 2017
1 parent dbdaf60 commit b97f2ab
Show file tree
Hide file tree
Showing 16 changed files with 418 additions and 388 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
10 changes: 7 additions & 3 deletions testsuite/MDAnalysisTests/core/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#

from __future__ import absolute_import

from unittest import TestCase

import numpy as np
from numpy.testing import (
dec,
Expand All @@ -29,6 +32,7 @@
assert_equal,
assert_raises,
)
import pytest

import MDAnalysis as mda
from MDAnalysis import NoDataError
Expand All @@ -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."""
Expand Down Expand Up @@ -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]
Expand Down
147 changes: 76 additions & 71 deletions testsuite/MDAnalysisTests/core/test_atomgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import itertools
import os
from os import path
from unittest import TestCase

import numpy as np
import warnings

Expand Down Expand Up @@ -56,35 +58,33 @@
)
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:
warnings.simplefilter('always')
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)
Expand All @@ -93,21 +93,19 @@ 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:
warnings.simplefilter('always')
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)
Expand All @@ -116,21 +114,19 @@ 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:
warnings.simplefilter('always')
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -910,22 +915,22 @@ 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)
assert_equal(universe.residues[20].CA.name, 'CA', "CA of MET21 is not selected correctly")
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."""
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit b97f2ab

Please sign in to comment.