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

Pytest Style analysis/test_base.py #1547

Merged
merged 2 commits into from
Jul 29, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 53 additions & 57 deletions testsuite/MDAnalysisTests/analysis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
from __future__ import division, absolute_import

import pytest
from six.moves import range
from unittest import TestCase

import numpy as np

from numpy.testing import (
assert_, dec, raises, assert_raises, assert_equal, assert_array_equal
)
from numpy.testing import assert_equal, assert_array_equal

import MDAnalysis as mda
from MDAnalysis.analysis import base
Expand Down Expand Up @@ -61,51 +60,49 @@ def _single_frame(self):
pass


class TestAnalysisBase(TestCase):
def setUp(self):
# has 98 frames
self.u = mda.Universe(PSF, DCD)

def tearDown(self):
del self.u
class TestAnalysisBase(object):
Copy link
Member

Choose a reason for hiding this comment

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

no need to have this as a class actually

@staticmethod
@pytest.fixture()
def u():
return mda.Universe(PSF, DCD)

def test_default(self):
an = FrameAnalysis(self.u.trajectory).run()
assert_equal(an.n_frames, len(self.u.trajectory))
assert_equal(an.frames, list(range(len(self.u.trajectory))))
def test_default(self, u):
an = FrameAnalysis(u.trajectory).run()
assert an.n_frames == len(u.trajectory)
assert_equal(an.frames, list(range(len(u.trajectory))))
Copy link
Member

Choose a reason for hiding this comment

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

assert_array_equal

Copy link
Member

Choose a reason for hiding this comment

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

please also change for similar cases in this file

Copy link
Contributor

Choose a reason for hiding this comment

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

See discussion on #1444.

Copy link
Member Author

Choose a reason for hiding this comment

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

@kain88-de my understanding is that assert_equal is preferred over assert_array_equal as discussed in 1444?

Copy link
Contributor

Choose a reason for hiding this comment

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

@utkbansal It is my understanding too.


def test_start(self):
an = FrameAnalysis(self.u.trajectory, start=20).run()
assert_equal(an.n_frames, len(self.u.trajectory) - 20)
assert_equal(an.frames, list(range(20, len(self.u.trajectory))))
def test_start(self, u):
an = FrameAnalysis(u.trajectory, start=20).run()
assert an.n_frames == len(u.trajectory) - 20
assert_equal(an.frames, list(range(20, len(u.trajectory))))

def test_stop(self):
an = FrameAnalysis(self.u.trajectory, stop=20).run()
assert_equal(an.n_frames, 20)
def test_stop(self, u):
an = FrameAnalysis(u.trajectory, stop=20).run()
assert an.n_frames == 20
assert_equal(an.frames, list(range(20)))

def test_step(self):
an = FrameAnalysis(self.u.trajectory, step=20).run()
assert_equal(an.n_frames, 5)
def test_step(self, u):
an = FrameAnalysis(u.trajectory, step=20).run()
assert an.n_frames == 5
assert_equal(an.frames, list(range(98))[::20])

def test_verbose(self):
a = FrameAnalysis(self.u.trajectory, verbose=True)
assert_(a._verbose)
assert_(not a._quiet)
def test_verbose(self, u):
a = FrameAnalysis(u.trajectory, verbose=True)
assert a._verbose
assert not a._quiet

@raises(NotImplementedError)
def test_incomplete_defined_analysis(self):
IncompleteAnalysis(self.u.trajectory).run()
def test_incomplete_defined_analysis(self, u):
with pytest.raises(NotImplementedError):
IncompleteAnalysis(u.trajectory).run()

def test_old_api(self):
OldAPIAnalysis(self.u.trajectory).run()
def test_old_api(self, u):
OldAPIAnalysis(u.trajectory).run()

def test_start_stop_step_conversion(self):
an = FrameAnalysis(self.u.trajectory)
assert_equal(an.start, 0)
assert_equal(an.stop, self.u.trajectory.n_frames)
assert_equal(an.step, 1)
def test_start_stop_step_conversion(self, u):
an = FrameAnalysis(u.trajectory)
assert an.start == 0
assert an.stop == u.trajectory.n_frames
assert an.step == 1


def test_filter_baseanalysis_kwargs():
Expand All @@ -117,19 +114,20 @@ def good_f(mobile, ref):

kwargs = {'step': 3, 'foo': None}

assert_raises(ValueError, base._filter_baseanalysis_kwargs, bad_f, kwargs)
with pytest.raises(ValueError):
base._filter_baseanalysis_kwargs(bad_f, kwargs)

base_kwargs, kwargs = base._filter_baseanalysis_kwargs(good_f, kwargs)

assert_equal(1, len(kwargs))
assert_equal(kwargs['foo'], None)
assert 1 == len(kwargs)
assert kwargs['foo'] == None

assert_equal(5, len(base_kwargs))
assert_equal(base_kwargs['start'], None)
assert_equal(base_kwargs['step'], 3)
assert_equal(base_kwargs['stop'], None)
assert_equal(base_kwargs['quiet'], None)
assert_equal(base_kwargs['verbose'], None)
assert 5 == len(base_kwargs)
assert base_kwargs['start'] is None
assert base_kwargs['step'] == 3
assert base_kwargs['stop'] is None
assert base_kwargs['quiet'] is None
assert base_kwargs['verbose'] is None


def simple_function(mobile):
Expand All @@ -139,12 +137,9 @@ def simple_function(mobile):
def test_AnalysisFromFunction():
u = mda.Universe(PSF, DCD)
step = 2
ana1 = base.AnalysisFromFunction(simple_function, mobile=u.atoms,
step=step).run()
ana2 = base.AnalysisFromFunction(simple_function, u.atoms,
step=step).run()
ana3 = base.AnalysisFromFunction(simple_function, u.trajectory, u.atoms,
step=step).run()
ana1 = base.AnalysisFromFunction(simple_function, mobile=u.atoms, step=step).run()
Copy link
Member

Choose a reason for hiding this comment

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

please don't change this formatting. I'm sure this was done to conform to the 80 char limit of pep8

ana2 = base.AnalysisFromFunction(simple_function, u.atoms, step=step).run()
ana3 = base.AnalysisFromFunction(simple_function, u.trajectory, u.atoms, step=step).run()

results = []
for ts in u.trajectory[::step]:
Expand All @@ -157,8 +152,8 @@ def test_AnalysisFromFunction():

def test_analysis_class():
ana_class = base.analysis_class(simple_function)
assert_(issubclass(ana_class, base.AnalysisBase))
assert_(issubclass(ana_class, base.AnalysisFromFunction))
assert issubclass(ana_class, base.AnalysisBase)
assert issubclass(ana_class, base.AnalysisFromFunction)

u = mda.Universe(PSF, DCD)
step = 2
Expand All @@ -170,8 +165,9 @@ def test_analysis_class():
results = np.asarray(results)

assert_array_equal(results, ana.results)
with pytest.raises(ValueError):
ana_class(2)

assert_raises(ValueError, ana_class, 2)

def test_analysis_class_decorator():
# Issue #1511
Expand Down