-
Notifications
You must be signed in to change notification settings - Fork 663
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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): | ||
@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)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assert_array_equal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also change for similar cases in this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See discussion on #1444. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kain88-de my understanding is that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(): | ||
|
@@ -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): | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]: | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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