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

TST: Added scipy import warning unit test to test_distances.py. #1141

Merged
merged 6 commits into from
Jan 1, 2017
20 changes: 20 additions & 0 deletions testsuite/MDAnalysisTests/analysis/test_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from numpy.testing import TestCase, assert_equal, dec
import numpy as np
import warnings


class TestContactMatrix(TestCase):
Expand Down Expand Up @@ -186,3 +187,22 @@ def test_between_simple_case_indices_only(self):
self.ag2,
self.distance).indices)
assert_equal(actual, self.expected)

class TestImportWarnings(TestCase):

def test_no_exception_scipy_module_level(self):
Copy link
Member

Choose a reason for hiding this comment

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

I think we can wrap this with the PR I put up, so it always issue a warning. Then split the else: branch of this if into a separate test that only runs when the scipy module is present.

# a module level ImportError should never be raised, even if
# scipy is absent ; only silent success or a warning
# see unit testing for warnings:
# http://stackoverflow.com/a/3892301

if module_not_found('scipy'):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
import MDAnalysis.analysis.distances
assert issubclass(w[-1].category, ImportWarning)
else: # no warning if scipy is present
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
import MDAnalysis.analysis.distances
assert w == []
Copy link
Member

Choose a reason for hiding this comment

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

I do like checking that we don't warn when not necessary though!