Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port core module to pytest #1443

Merged
merged 26 commits into from Jul 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
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
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
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?')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you double check that marking fixtures like this works? Ie does this mark then get transferred onto tests which use this fixture?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my local system, I only have the minimal dependencies, all the test methods are skipped.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You run on python3? These tests will always run on Python 2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's odd. I'm python 2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dcd is available on python 2 as a parser. Those tests should run for you. But since we soon have a python3 DCD parser I'm not to worried if the tests are skipped but they should be run on phython2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work on Python 2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@utkbansal do these tests work under python 2 now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they work now.

@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