diff options
author | Nathaniel J. Smith <njs@pobox.com> | 2012-09-20 22:02:18 +0100 |
---|---|---|
committer | Nathaniel J. Smith <njs@pobox.com> | 2012-09-20 22:02:18 +0100 |
commit | 47086158cb00a151b67c442ae759ce230ec0de34 (patch) | |
tree | 275dc7e5b2a2d9fa3b17a119417c34f3f48f19a4 /numpy/testing/utils.py | |
parent | 9597b1fdef7c46b0d1b1485fb680099ec7115f76 (diff) | |
download | numpy-47086158cb00a151b67c442ae759ce230ec0de34.tar.gz |
ENH: More capable test functions for warnings
1) New function assert_no_warnings
2) Make assert_warns and assert_no_warnings pass through the
function's return value on success, so that it can be checked as
well.
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r-- | numpy/testing/utils.py | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index ffce2eefc..16ed0f803 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -16,7 +16,8 @@ __all__ = ['assert_equal', 'assert_almost_equal','assert_approx_equal', 'decorate_methods', 'jiffies', 'memusage', 'print_assert_equal', 'raises', 'rand', 'rundocs', 'runstring', 'verbose', 'measure', 'assert_', 'assert_array_almost_equal_nulp', - 'assert_array_max_ulp', 'assert_warns', 'assert_allclose'] + 'assert_array_max_ulp', 'assert_warns', 'assert_no_warnings', + 'assert_allclose'] verbose = 0 @@ -1464,7 +1465,7 @@ def assert_warns(warning_class, func, *args, **kw): Returns ------- - None + The value returned by `func`. """ @@ -1474,7 +1475,7 @@ def assert_warns(warning_class, func, *args, **kw): l = ctx.__enter__() warnings.simplefilter('always') try: - func(*args, **kw) + result = func(*args, **kw) if not len(l) > 0: raise AssertionError("No warning raised when calling %s" % func.__name__) @@ -1483,3 +1484,36 @@ def assert_warns(warning_class, func, *args, **kw): "%s( is %s)" % (func.__name__, warning_class, l[0])) finally: ctx.__exit__() + return result + +def assert_no_warnings(func, *args, **kw): + """ + Fail if the given callable produces any warnings. + + Parameters + ---------- + func : callable + The callable to test. + \\*args : Arguments + Arguments passed to `func`. + \\*\\*kwargs : Kwargs + Keyword arguments passed to `func`. + + Returns + ------- + The value returned by `func`. + + """ + # XXX: once we may depend on python >= 2.6, this can be replaced by the + # warnings module context manager. + ctx = WarningManager(record=True) + l = ctx.__enter__() + warnings.simplefilter('always') + try: + result = func(*args, **kw) + if len(l) > 0: + raise AssertionError("Got warnings when calling %s: %s" + % (func.__name__, l)) + finally: + ctx.__exit__() + return result |