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 analysis module to pytest #1450

Merged
merged 20 commits into from Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from 18 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"
- NOSE_TEST_LIST=""
- 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 testsuite/MDAnalysisTests/core"
- PYTEST_LIST="testsuite/MDAnalysisTests/lib testsuite/MDAnalysisTests/formats testsuite/MDAnalysisTests/coordinates testsuite/MDAnalysisTests/utils testsuite/MDAnalysisTests/topology testsuite/MDAnalysisTests/auxiliary testsuite/MDAnalysisTests/core testsuite/MDAnalysisTests/analysis"
Copy link
Member

Choose a reason for hiding this comment

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

also remove nose from package/setup.py and testsuite/setup.py — yay, major step forward in your project: getting rid of nose.

Copy link
Member

Choose a reason for hiding this comment

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

And remove testsuite/mda_nosetests.py.

Copy link
Member

Choose a reason for hiding this comment

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

@mnmelo @jbarnoud @kain88-de @richardjgowers : should the nose-specific plugins also be removed at this time? We have to recreate the plugins for pytest or replace with native pytest plugins anyway,

Copy link
Member

Choose a reason for hiding this comment

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

@orbeckst these changes should be in a separate PR. The plugins can pretty much all go. There is the openfile plugin but otherwise pytest comes with better alternatives

- 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}"
Copy link
Member

Choose a reason for hiding this comment

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

remove use of mda_nosetests

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @orbeckst. With NOSE_TEST_LIST being empty, the call to mda_nosetests becomes buggy and should be removed. Removing the dependencies to nose is out of scope, though.

Expand Down
6 changes: 3 additions & 3 deletions testsuite/MDAnalysisTests/analysis/test_align.py
Expand Up @@ -43,8 +43,8 @@
warnings.simplefilter('always')


class TestRotationMatrix(object):
def __init__(self):
class TestRotationMatrix(TestCase):
def setUp(self):
Copy link
Member

Choose a reason for hiding this comment

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

@utkbansal I assume the goal of this PR is to make it run under pytest and then you will do a second round adapting it to pytest style? Is this correct? (Would be fine with me.)

Copy link
Member

Choose a reason for hiding this comment

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

yes that idea is correct

self.a = np.array([[0.1, 0.2, 0.3],
[1.1, 1.1, 1.1]])
self.b = np.array([[0.1, 0.1, 0.1],
Expand Down Expand Up @@ -297,7 +297,7 @@ def test_alignto_partial_universe():



class TestAlignmentProcessing(object):
class TestAlignmentProcessing(TestCase):
def setUp(self):
self.seq = FASTA
self.tempdir = tempdir.TempDir()
Expand Down
3 changes: 2 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_base.py
Expand Up @@ -21,6 +21,7 @@
#
from __future__ import division, absolute_import
from six.moves import range
from unittest import TestCase

import numpy as np

Expand Down Expand Up @@ -60,7 +61,7 @@ def _single_frame(self):
pass


class TestAnalysisBase(object):
class TestAnalysisBase(TestCase):
@dec.skipif(parser_not_found('DCD'),
'DCD parser not available. Are you using python 3?')
def setUp(self):
Expand Down
6 changes: 4 additions & 2 deletions testsuite/MDAnalysisTests/analysis/test_contacts.py
Expand Up @@ -21,6 +21,8 @@
#
from __future__ import print_function, division, absolute_import

from unittest import TestCase

import MDAnalysis as mda
from MDAnalysis.analysis import contacts
from MDAnalysis.analysis.distances import distance_array
Expand Down Expand Up @@ -151,11 +153,11 @@ def soft_cut(ref, u, selA, selB, radius=4.5, beta=5.0, lambda_constant=1.8):
return np.asarray(results)


class TestContacts(object):
class TestContacts(TestCase):
@dec.skipif(
parser_not_found('DCD'),
'DCD parser not available. Are you using python 3?')
def __init__(self):
def setUp(self):
self.universe = mda.Universe(PSF, DCD)
self.trajectory = self.universe.trajectory

Expand Down
2 changes: 1 addition & 1 deletion testsuite/MDAnalysisTests/analysis/test_density.py
Expand Up @@ -196,7 +196,7 @@ def test_presence_griddata(self):
an ImportError if gridData is available.''')


class TestNotWithin(object):
class TestNotWithin(TestCase):
# tests notwithin_coordinates_factory
# only checks that KDTree and distance_array give same results
def setUp(self):
Expand Down
3 changes: 2 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_diffusionmap.py
Expand Up @@ -20,6 +20,7 @@
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
from __future__ import print_function, absolute_import
from unittest import TestCase
import numpy as np
import MDAnalysis
import MDAnalysis.analysis.diffusionmap as diffusionmap
Expand All @@ -30,7 +31,7 @@
from MDAnalysisTests.datafiles import PDB, XTC


class TestDiffusionmap(object):
class TestDiffusionmap(TestCase):
def setUp(self):
self.u = MDAnalysis.Universe(PDB, XTC)
self.dist = diffusionmap.DistanceMatrix(self.u, select='backbone')
Expand Down
19 changes: 11 additions & 8 deletions testsuite/MDAnalysisTests/analysis/test_encore.py
Expand Up @@ -30,6 +30,7 @@
import sys
import warnings

import pytest
from numpy.testing import (TestCase, dec, assert_equal, assert_almost_equal,
assert_warns)

Expand Down Expand Up @@ -826,20 +827,22 @@ def test_get_distance_matrix(self):
dm = confdistmatrix.get_distance_matrix(u)

class TestEncoreImportWarnings(object):
def setUp(self):
# clear cache of encore module
@block_import('sklearn')
def _check_sklearn_import_warns(self, package, recwarn):
for mod in list(sys.modules): # list as we're changing as we iterate
if 'encore' in mod:
sys.modules.pop(mod, None)

@block_import('sklearn')
def _check_sklearn_import_warns(self, package):
warnings.simplefilter('always')
assert_warns(ImportWarning, importlib.import_module, package)
# assert_warns(ImportWarning, importlib.import_module, package)
importlib.import_module(package)
assert recwarn.pop(ImportWarning)

def test_import_warnings(self):
def test_import_warnings(self, recwarn):
for mod in list(sys.modules): # list as we're changing as we iterate
if 'encore' in mod:
sys.modules.pop(mod, None)
for pkg in (
'MDAnalysis.analysis.encore.dimensionality_reduction.DimensionalityReductionMethod',
'MDAnalysis.analysis.encore.clustering.ClusteringMethod',
):
yield self._check_sklearn_import_warns, pkg
self._check_sklearn_import_warns(pkg, recwarn)
Copy link
Contributor

Choose a reason for hiding this comment

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

This changes the behaviour of the test. Instead of having 2 tests that can fail individually, there is only one test that has two consecutive asserts. It is fine for now, but please add a comment as it may be forgotten when you will move to proper pytest idioms.

Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is still missing here.

Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is bellow. I missed it.

3 changes: 2 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_gnm.py
Expand Up @@ -24,6 +24,7 @@
import MDAnalysis
import MDAnalysis.analysis.gnm

from unittest import TestCase
from numpy.testing import (assert_equal, assert_almost_equal)
import numpy as np

Expand All @@ -32,7 +33,7 @@
from MDAnalysisTests.datafiles import GRO, XTC
from MDAnalysisTests import tempdir

class TestGNM(object):
class TestGNM(TestCase):
def setUp(self):
self.tmpdir = tempdir.TempDir()
self.universe = MDAnalysis.Universe(GRO, XTC)
Expand Down
6 changes: 4 additions & 2 deletions testsuite/MDAnalysisTests/analysis/test_hbonds.py
Expand Up @@ -21,6 +21,8 @@
#
from __future__ import print_function, absolute_import

from unittest import TestCase

import MDAnalysis
import MDAnalysis.analysis.hbonds
from MDAnalysis import SelectionError, SelectionWarning
Expand All @@ -46,7 +48,7 @@ def guess_types(names):
return Atomtypes(np.array([guess_atom_type(name) for name in names], dtype=object))


class TestHydrogenBondAnalysis(object):
class TestHydrogenBondAnalysis(TestCase):
def setUp(self):
self.universe = u = MDAnalysis.Universe(PDB_helix)
self.kwargs = {
Expand Down Expand Up @@ -239,7 +241,7 @@ def run_HBA_dynamic_selections(*args):
self._tearDown()


class TestHydrogenBondAnalysisTIP3P(object):
class TestHydrogenBondAnalysisTIP3P(TestCase):
@dec.skipif(parser_not_found('DCD'),
Copy link
Member

Choose a reason for hiding this comment

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

remove DCD skipif

Copy link
Member

Choose a reason for hiding this comment

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

separate PR, as @kain88-de says

'DCD parser not available. Are you using python 3?')
def setUp(self):
Expand Down
Expand Up @@ -22,6 +22,8 @@
from __future__ import division, absolute_import
import six
from six.moves import zip, range

from unittest import TestCase
from MDAnalysisTests.datafiles import TRZ, TRZ_psf, PRM, TRJ
from MDAnalysisTests import module_not_found, tempdir
from numpy.testing import assert_, assert_array_almost_equal, assert_raises, assert_, dec
Expand All @@ -32,7 +34,7 @@
from MDAnalysis.analysis.hbonds import HydrogenBondAutoCorrel as HBAC


class TestHydrogenBondAutocorrel(object):
class TestHydrogenBondAutocorrel(TestCase):
def setUp(self):
u = self.u = mda.Universe(TRZ_psf, TRZ)
self.H = u.atoms.select_atoms('name Hn')
Expand Down
4 changes: 3 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_nuclinfo.py
Expand Up @@ -21,6 +21,8 @@
#
from __future__ import absolute_import

from unittest import TestCase

from numpy.testing import (
assert_,
assert_almost_equal,
Expand All @@ -33,7 +35,7 @@
from MDAnalysisTests.datafiles import NUCL


class TestNuclinfo(object):
class TestNuclinfo(TestCase):
def setUp(self):
self.u = mda.Universe(NUCL)

Expand Down
5 changes: 4 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_pca.py
Expand Up @@ -20,6 +20,9 @@
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
from __future__ import print_function, absolute_import

from unittest import TestCase

import numpy as np
import MDAnalysis
import MDAnalysis.analysis.pca as pca
Expand All @@ -32,7 +35,7 @@
from MDAnalysisTests import module_not_found


class TestPCA(object):
class TestPCA(TestCase):
""" Test the PCA class """
def setUp(self):
self.u = MDAnalysis.Universe(PSF, DCD)
Expand Down
6 changes: 4 additions & 2 deletions testsuite/MDAnalysisTests/analysis/test_persistencelength.py
Expand Up @@ -21,6 +21,8 @@
#
from __future__ import print_function, division, absolute_import

from unittest import TestCase

import MDAnalysis
from MDAnalysis.analysis import polymer
from MDAnalysis.exceptions import NoDataError
Expand All @@ -39,7 +41,7 @@
from MDAnalysisTests import module_not_found


class TestPersistenceLength(object):
class TestPersistenceLength(TestCase):
def setUp(self):
self.u = MDAnalysis.Universe(Plength)

Expand Down Expand Up @@ -89,7 +91,7 @@ def test_raise_NoDataError(self):
p = self._make_p()
assert_raises(NoDataError, p.perform_fit)

class TestFitExponential(object):
class TestFitExponential(TestCase):
def setUp(self):
self.x = np.linspace(0, 250, 251)
self.a_ref = 20.0
Expand Down
8 changes: 8 additions & 0 deletions testsuite/MDAnalysisTests/analysis/test_psa.py
Expand Up @@ -185,6 +185,8 @@ class _BaseHausdorffDistance(TestCase):
for various Hausdorff distance
calculation properties.'''

__test__ = False

def setUp(self):
self.random_angles = np.random.random((100,)) * np.pi * 2
self.random_columns = np.column_stack((self.random_angles,
Expand Down Expand Up @@ -233,6 +235,8 @@ class TestHausdorffSymmetric(_BaseHausdorffDistance):
'''Tests for conventional and symmetric (undirected)
Hausdorff distance between point sets in 3D.'''

__test__ = True

def setUp(self):
super(TestHausdorffSymmetric, self).setUp()
self.h = PSA.hausdorff
Expand All @@ -243,6 +247,8 @@ class TestWeightedAvgHausdorffSymmetric(_BaseHausdorffDistance):
'''Tests for weighted average and symmetric (undirected)
Hausdorff distance between point sets in 3D.'''

__test__ = True

def setUp(self):
super(TestWeightedAvgHausdorffSymmetric, self).setUp()
self.h = PSA.hausdorff_wavg
Expand All @@ -265,6 +271,8 @@ class TestAvgHausdorffSymmetric(_BaseHausdorffDistance):
'''Tests for unweighted average and symmetric (undirected)
Hausdorff distance between point sets in 3D.'''

__test__ = True

def setUp(self):
super(TestAvgHausdorffSymmetric, self).setUp()
self.h = PSA.hausdorff_avg
Expand Down
4 changes: 3 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_rdf.py
Expand Up @@ -22,6 +22,8 @@
from __future__ import absolute_import
from six.moves import zip

from unittest import TestCase

from numpy.testing import assert_

import MDAnalysis as mda
Expand All @@ -30,7 +32,7 @@
from MDAnalysisTests.datafiles import two_water_gro


class TestInterRDF(object):
class TestInterRDF(TestCase):
def setUp(self):
self.u = mda.Universe(two_water_gro)

Expand Down