Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""py.test hacks to support XFAIL/XPASS"""
except ImportError: USE_PYTEST = False
""" Tests that ``code`` raises the exception ``expectedException``.
``code`` may be a callable, such as a lambda expression or function name.
If ``code`` is not given or None, ``raises`` will return a context manager for use in ``with`` statements; the code to execute then comes from the scope of the ``with``.
``raises()`` does nothing if the callable raises the expected exception, otherwise it raises an AssertionError.
Examples ========
>>> from sympy.utilities.pytest import raises
>>> raises(ZeroDivisionError, lambda: 1/0) >>> raises(ZeroDivisionError, lambda: 1/2) Traceback (most recent call last): ... AssertionError: DID NOT RAISE
>>> with raises(ZeroDivisionError): ... n = 1/0 >>> with raises(ZeroDivisionError): ... n = 1/2 Traceback (most recent call last): ... AssertionError: DID NOT RAISE
Note that you cannot test multiple statements via ``with raises``:
>>> with raises(ZeroDivisionError): ... n = 1/0 # will execute and raise, aborting the ``with`` ... n = 9999/0 # never executed
This is just what ``with`` is supposed to do: abort the contained statement sequence at the first exception and let the context manager deal with the exception.
To test multiple statements, you'll need a separate ``with`` for each:
>>> with raises(ZeroDivisionError): ... n = 1/0 # will execute and raise >>> with raises(ZeroDivisionError): ... n = 9999/0 # will also execute and raise
""" return RaisesContext(expectedException) raise AssertionError("DID NOT RAISE") elif isinstance(code, str): raise TypeError( '\'raises(xxx, "code")\' has been phased out; ' 'change \'raises(xxx, "expression")\' ' 'to \'raises(xxx, lambda: expression)\', ' '\'raises(xxx, "statement")\' ' 'to \'with raises(xxx): statement\'') else: raise TypeError( 'raises() expects a callable for the 2nd argument.')
self.expectedException = expectedException
return None
if exc_type is None: raise AssertionError("DID NOT RAISE") return issubclass(exc_type, self.expectedException)
else: raise Skipped("Timeout") raise XPass(get_function_name(func))
"""Similar to :func:`skip`, but this is a decorator. """
func()
else: XFAIL = py.test.mark.xfail slow = py.test.mark.slow
def SKIP(reason): def skipping(func): @functools.wraps(func) def inner(*args, **kwargs): skip(reason) return inner
return skipping |