pytest¶
py.test hacks to support XFAIL/XPASS
-
sympy.utilities.pytest.raises(expectedException, code=None)[source]¶ Tests that
coderaises the exceptionexpectedException.codemay be a callable, such as a lambda expression or function name.If
codeis not given or None,raiseswill return a context manager for use inwithstatements; the code to execute then comes from the scope of thewith.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
withis 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
withfor each:>>> with raises(ZeroDivisionError): ... n = 1/0 # will execute and raise >>> with raises(ZeroDivisionError): ... n = 9999/0 # will also execute and raise