pytest

py.test hacks to support XFAIL/XPASS

sympy.utilities.pytest.SKIP(reason)[source]

Similar to skip(), but this is a decorator.

sympy.utilities.pytest.ignore_warnings(warningcls)[source]

Context manager to suppress warnings during tests.

This function is useful for suppressing warnings during tests. The warns function should be used to assert that a warning is raised. The ignore_warnings function is useful in situation when the warning is not guaranteed to be raised (e.g. on importing a module) or if the warning comes from third-party code.

When the warning is coming (reliably) from SymPy the warns function should be preferred to ignore_warnings.

>>> from sympy.utilities.pytest import ignore_warnings
>>> import warnings

Here’s a warning:

>>> with warnings.catch_warnings():  # reset warnings in doctest
...     warnings.simplefilter('error')
...     warnings.warn('deprecated', UserWarning)
Traceback (most recent call last):
  ...
UserWarning: deprecated

Let’s suppress it with ignore_warnings:

>>> with warnings.catch_warnings():  # reset warnings in doctest
...     warnings.simplefilter('error')
...     with ignore_warnings(UserWarning):
...         warnings.warn('deprecated', UserWarning)

(No warning emitted)

sympy.utilities.pytest.raises(expectedException, code=None)[source]

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):
...
Failed: DID NOT RAISE
>>> with raises(ZeroDivisionError):
...     n = 1/0
>>> with raises(ZeroDivisionError):
...     n = 1/2
Traceback (most recent call last):
...
Failed: 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
sympy.utilities.pytest.warns(warningcls, **kwargs)[source]

Like raises but tests that warnings are emitted.

>>> from sympy.utilities.pytest import warns
>>> import warnings
>>> with warns(UserWarning):
...     warnings.warn('deprecated', UserWarning)
>>> with warns(UserWarning):
...     pass
Traceback (most recent call last):
...
Failed: DID NOT WARN. No warnings of type UserWarning        was emitted. The list of emitted warnings is: [].
sympy.utilities.pytest.warns_deprecated_sympy()[source]

Shorthand for warns(SymPyDeprecationWarning)

This is the recommended way to test that SymPyDeprecationWarning is emitted for deprecated features in SymPy. To test for other warnings use warns. To suppress warnings without asserting that they are emitted use ignore_warnings.

>>> from sympy.utilities.pytest import warns_deprecated_sympy
>>> from sympy.utilities.exceptions import SymPyDeprecationWarning
>>> import warnings
>>> with warns_deprecated_sympy():
...     SymPyDeprecationWarning("Don't use", feature="old thing",
...         deprecated_since_version="1.0", issue=123).warn()
>>> with warns_deprecated_sympy():
...     pass
Traceback (most recent call last):
...
Failed: DID NOT WARN. No warnings of type     SymPyDeprecationWarning was emitted. The list of emitted warnings is: [].