summaryrefslogtreecommitdiff
path: root/numpy/testing
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/testing')
-rw-r--r--numpy/testing/decorators.py13
-rw-r--r--numpy/testing/nosetester.py43
-rw-r--r--numpy/testing/tests/test_decorators.py14
-rw-r--r--numpy/testing/tests/test_utils.py12
-rw-r--r--numpy/testing/utils.py24
5 files changed, 56 insertions, 50 deletions
diff --git a/numpy/testing/decorators.py b/numpy/testing/decorators.py
index 6cde298e1..17400c0d5 100644
--- a/numpy/testing/decorators.py
+++ b/numpy/testing/decorators.py
@@ -15,10 +15,10 @@ function name, setup and teardown functions and so on - see
"""
from __future__ import division, absolute_import, print_function
-import warnings
import collections
-from .utils import SkipTest
+from .utils import SkipTest, assert_warns
+
def slow(t):
"""
@@ -251,15 +251,8 @@ def deprecated(conditional=True):
def _deprecated_imp(*args, **kwargs):
# Poor man's replacement for the with statement
- with warnings.catch_warnings(record=True) as l:
- warnings.simplefilter('always')
+ with assert_warns(DeprecationWarning):
f(*args, **kwargs)
- if not len(l) > 0:
- raise AssertionError("No warning raised when calling %s"
- % f.__name__)
- if not l[0].category is DeprecationWarning:
- raise AssertionError("First warning for %s is not a "
- "DeprecationWarning( is %s)" % (f.__name__, l[0]))
if isinstance(conditional, collections.Callable):
cond = conditional()
diff --git a/numpy/testing/nosetester.py b/numpy/testing/nosetester.py
index 6fff240eb..c30c0eecd 100644
--- a/numpy/testing/nosetester.py
+++ b/numpy/testing/nosetester.py
@@ -135,9 +135,9 @@ class NoseTester(object):
which `NoseTester` is initialized.
raise_warnings : None, str or sequence of warnings, optional
This specifies which warnings to configure as 'raise' instead
- of 'warn' during the test execution. Valid strings are:
+ of being shown once during the test execution. Valid strings are:
- - "develop" : equals ``(DeprecationWarning, RuntimeWarning)``
+ - "develop" : equals ``(Warning,)``
- "release" : equals ``()``, don't raise on any warnings.
Default is "release".
@@ -297,8 +297,7 @@ class NoseTester(object):
return argv, plugins
def test(self, label='fast', verbose=1, extra_argv=None,
- doctests=False, coverage=False,
- raise_warnings=None):
+ doctests=False, coverage=False, raise_warnings=None):
"""
Run tests for module using nose.
@@ -324,13 +323,15 @@ class NoseTester(object):
If True, report coverage of NumPy code. Default is False.
(This requires the `coverage module:
<http://nedbatchelder.com/code/modules/coverage.html>`_).
- raise_warnings : str or sequence of warnings, optional
+ raise_warnings : None, str or sequence of warnings, optional
This specifies which warnings to configure as 'raise' instead
- of 'warn' during the test execution. Valid strings are:
+ of being shown once during the test execution. Valid strings are:
- - "develop" : equals ``(DeprecationWarning, RuntimeWarning)``
+ - "develop" : equals ``(Warning,)``
- "release" : equals ``()``, don't raise on any warnings.
+ The default is to use the class initialization value.
+
Returns
-------
result : object
@@ -379,12 +380,12 @@ class NoseTester(object):
if raise_warnings is None:
raise_warnings = self.raise_warnings
- _warn_opts = dict(develop=(DeprecationWarning, RuntimeWarning),
+ _warn_opts = dict(develop=(Warning,),
release=())
if isinstance(raise_warnings, basestring):
raise_warnings = _warn_opts[raise_warnings]
- with warnings.catch_warnings():
+ with suppress_warnings("location") as sup:
# Reset the warning filters to the default state,
# so that running the tests is more repeatable.
warnings.resetwarnings()
@@ -395,18 +396,27 @@ class NoseTester(object):
for warningtype in raise_warnings:
warnings.filterwarnings('error', category=warningtype)
# Filter out annoying import messages.
- warnings.filterwarnings('ignore', message='Not importing directory')
- warnings.filterwarnings("ignore", message="numpy.dtype size changed")
- warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
- warnings.filterwarnings("ignore", category=np.ModuleDeprecationWarning)
- warnings.filterwarnings("ignore", category=FutureWarning)
+ sup.filter(message='Not importing directory')
+ sup.filter(message="numpy.dtype size changed")
+ sup.filter(message="numpy.ufunc size changed")
+ sup.filter(category=np.ModuleDeprecationWarning)
# Filter out boolean '-' deprecation messages. This allows
# older versions of scipy to test without a flood of messages.
- warnings.filterwarnings("ignore", message=".*boolean negative.*")
- warnings.filterwarnings("ignore", message=".*boolean subtract.*")
+ sup.filter(message=".*boolean negative.*")
+ sup.filter(message=".*boolean subtract.*")
+ # Filter out distutils cpu warnings (could be localized to
+ # distutils tests). ASV has problems with top level import,
+ # so fetch module for suppression here.
+ with warnings.catch_warnings():
+ warnings.simplefilter("always")
+ from ..distutils import cpuinfo
+ sup.filter(category=UserWarning, module=cpuinfo)
+
# Filter out some deprecation warnings inside nose 1.3.7 when run
# on python 3.5b2. See
# https://github.com/nose-devs/nose/issues/929
+ # Note: it is hard to filter based on module for sup (lineno could
+ # be implemented).
warnings.filterwarnings("ignore", message=".*getargspec.*",
category=DeprecationWarning,
module="nose\.")
@@ -415,6 +425,7 @@ class NoseTester(object):
argv, plugins = self.prepare_test_args(
label, verbose, extra_argv, doctests, coverage)
+
t = NumpyTestProgram(argv=argv, exit=False, plugins=plugins)
return t.result
diff --git a/numpy/testing/tests/test_decorators.py b/numpy/testing/tests/test_decorators.py
index 7dbb5a828..721c0ef7e 100644
--- a/numpy/testing/tests/test_decorators.py
+++ b/numpy/testing/tests/test_decorators.py
@@ -1,8 +1,10 @@
from __future__ import division, absolute_import, print_function
+import warnings
+
from numpy.testing import (dec, assert_, assert_raises, run_module_suite,
SkipTest, KnownFailureException)
-import nose
+
def test_slow():
@dec.slow
@@ -172,10 +174,12 @@ def test_deprecated():
assert_raises(AssertionError, non_deprecated_func)
# should be silent
deprecated_func()
- # fails if deprecated decorator just disables test. See #1453.
- assert_raises(ValueError, deprecated_func2)
- # first warnings is not a DeprecationWarning
- assert_raises(AssertionError, deprecated_func3)
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always") # do not propagate unrelated warnings
+ # fails if deprecated decorator just disables test. See #1453.
+ assert_raises(ValueError, deprecated_func2)
+ # warning is not a DeprecationWarning
+ assert_raises(AssertionError, deprecated_func3)
if __name__ == '__main__':
diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py
index c0f609883..c191aea5b 100644
--- a/numpy/testing/tests/test_utils.py
+++ b/numpy/testing/tests/test_utils.py
@@ -503,23 +503,21 @@ class TestWarns(unittest.TestCase):
warnings.warn("yo", DeprecationWarning)
failed = False
- filters = sys.modules['warnings'].filters[:]
- try:
+ with warnings.catch_warnings():
+ warnings.simplefilter("error", DeprecationWarning)
try:
- # Should raise an AssertionError
+ # Should raise a DeprecationWarning
assert_warns(UserWarning, f)
failed = True
- except AssertionError:
+ except DeprecationWarning:
pass
- finally:
- sys.modules['warnings'].filters = filters
if failed:
raise AssertionError("wrong warning caught by assert_warn")
class TestAssertAllclose(unittest.TestCase):
-
+
def test_simple(self):
x = 1e-3
y = 1e-9
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index 9c432f3dc..683a4f0a6 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -396,8 +396,12 @@ def assert_equal(actual,desired,err_msg='',verbose=True):
pass
# Explicitly use __eq__ for comparison, ticket #2552
- if not (desired == actual):
- raise AssertionError(msg)
+ with suppress_warnings() as sup:
+ # TODO: Better handling will to needed when change happens!
+ sup.filter(DeprecationWarning, ".*NAT ==")
+ sup.filter(FutureWarning, ".*NAT ==")
+ if not (desired == actual):
+ raise AssertionError(msg)
def print_assert_equal(test_string, actual, desired):
@@ -687,8 +691,9 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
# pass (or maybe eventually catch the errors and return False, I
# dunno, that's a little trickier and we can figure that out when the
# time comes).
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
+ with suppress_warnings() as sup:
+ sup.filter(DeprecationWarning, ".*==")
+ sup.filter(FutureWarning, ".*==")
return comparison(*args, **kwargs)
def isnumber(x):
@@ -1658,16 +1663,12 @@ class WarningManager(object):
@contextlib.contextmanager
def _assert_warns_context(warning_class, name=None):
__tracebackhide__ = True # Hide traceback for py.test
- with warnings.catch_warnings(record=True) as l:
- warnings.simplefilter('always')
+ with suppress_warnings() as sup:
+ l = sup.record(warning_class)
yield
if not len(l) > 0:
name_str = " when calling %s" % name if name is not None else ""
raise AssertionError("No warning raised" + name_str)
- if not l[0].category is warning_class:
- name_str = "%s " % name if name is not None else ""
- raise AssertionError("First warning %sis not a %s (is %s)"
- % (name_str, warning_class, l[0]))
def assert_warns(warning_class, *args, **kwargs):
@@ -1676,8 +1677,7 @@ def assert_warns(warning_class, *args, **kwargs):
A warning of class warning_class should be thrown by the callable when
invoked with arguments args and keyword arguments kwargs.
- If a different type of warning is thrown, it will not be caught, and the
- test case will be deemed to have suffered an error.
+ If a different type of warning is thrown, it will not be caught.
If called with all arguments other than the warning class omitted, may be
used as a context manager: