From 47086158cb00a151b67c442ae759ce230ec0de34 Mon Sep 17 00:00:00 2001 From: "Nathaniel J. Smith" Date: Thu, 20 Sep 2012 22:02:18 +0100 Subject: 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. --- numpy/testing/utils.py | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'numpy/testing/utils.py') 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 -- cgit v1.2.1