diff options
author | Travis Oliphant <oliphant@enthought.com> | 2008-01-02 17:04:15 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2008-01-02 17:04:15 +0000 |
commit | 721e160f982453c5de951af09465c7be5c7805d0 (patch) | |
tree | e97fcb1d5fd344a4f7a887f1190343a0becdd049 /numpy/testing/utils.py | |
parent | 1b629358028111f9b08433d0bf906bbd0cdc0f02 (diff) | |
download | numpy-721e160f982453c5de951af09465c7be5c7805d0.tar.gz |
Removed dependency on nose.tools. Ripped nose.tools.raise and placed it in numpy.testing
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r-- | numpy/testing/utils.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index 89fff6c25..4f4c3496c 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -11,7 +11,7 @@ import operator __all__ = ['assert_equal', 'assert_almost_equal','assert_approx_equal', 'assert_array_equal', 'assert_array_less', 'assert_string_equal', 'assert_array_almost_equal', 'jiffies', 'memusage', 'rand', - 'runstring'] + 'runstring', 'raises'] def rand(*args): """Returns an array of random numbers with the given shape. @@ -270,3 +270,37 @@ def assert_string_equal(actual, desired): if not diff_list: return msg = 'Differences in strings:\n%s' % (''.join(diff_list)).rstrip() assert actual==desired, msg + +# Ripped from nose.tools +def raises(*exceptions): + """Test must raise one of expected exceptions to pass. + + Example use:: + + @raises(TypeError, ValueError) + def test_raises_type_error(): + raise TypeError("This test passes") + + @raises(Exception): + def test_that_fails_by_passing(): + pass + + If you want to test many assertions about exceptions in a single test, + you may want to use `assert_raises` instead. + """ + valid = ' or '.join([e.__name__ for e in exceptions]) + def decorate(func): + name = func.__name__ + def newfunc(*arg, **kw): + try: + func(*arg, **kw) + except exceptions: + pass + except: + raise + else: + message = "%s() did not raise %s" % (name, valid) + raise AssertionError(message) + newfunc = make_decorator(func)(newfunc) + return newfunc + return decorate |