From 721e160f982453c5de951af09465c7be5c7805d0 Mon Sep 17 00:00:00 2001 From: Travis Oliphant Date: Wed, 2 Jan 2008 17:04:15 +0000 Subject: Removed dependency on nose.tools. Ripped nose.tools.raise and placed it in numpy.testing --- numpy/testing/utils.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'numpy/testing') 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 -- cgit v1.2.1