py.test hacks to support XFAIL/XPASS
skip(), but this is a decorator.
Dummy decorator for marking tests that fail when cache is disabled
- sympy.testing.pytest.raises(expectedException, code=None)#
coderaises the exception
codemay be a callable, such as a lambda expression or function name.
codeis not given or None,
raiseswill return a context manager for use in
withstatements; the code to execute then comes from the scope of the
raises()does nothing if the callable raises the expected exception, otherwise it raises an AssertionError.
>>> from sympy.testing.pytest import raises
>>> raises(ZeroDivisionError, lambda: 1/0) <ExceptionInfo ZeroDivisionError(...)> >>> 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(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
>>> with raises(ZeroDivisionError): ... n = 1/0 # will execute and raise >>> with raises(ZeroDivisionError): ... n = 9999/0 # will also execute and raise
- sympy.testing.pytest.warns(warningcls, *, match='', test_stacklevel=True)#
Like raises but tests that warnings are emitted.
>>> from sympy.testing.pytest import warns >>> import warnings
>>> with warns(UserWarning): ... warnings.warn('deprecated', UserWarning, stacklevel=2)
>>> 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: .
test_stacklevelmakes it check that the
warn()is set so that the warning shows the user line of code (the code under the warns() context manager). Set this to False if this is ambiguous or if the context manager does not test the direct user code that emits the warning.
If the warning is a
SymPyDeprecationWarning, this additionally tests that the
active_deprecations_targetis a real target in the
This is the recommended way to test that
SymPyDeprecationWarningis emitted for deprecated features in SymPy. To test for other warnings use
warns. To suppress warnings without asserting that they are emitted use
warns_deprecated_sympy()is only intended for internal use in the SymPy test suite to test that a deprecation warning triggers properly. All other code in the SymPy codebase, including documentation examples, should not use deprecated behavior.
If you are a user of SymPy and you want to disable SymPyDeprecationWarnings, use
warningsfilters (see Silencing SymPy Deprecation Warnings).
>>> from sympy.testing.pytest import warns_deprecated_sympy >>> from sympy.utilities.exceptions import sympy_deprecation_warning >>> with warns_deprecated_sympy(): ... sympy_deprecation_warning("Don't use", ... deprecated_since_version="1.0", ... active_deprecations_target="active-deprecations")
>>> 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: .
Sometimes the stacklevel test will fail because the same warning is emitted multiple times. In this case, you can use
sympy.utilities.exceptions.ignore_warnings()in the code to prevent the
SymPyDeprecationWarningfrom being emitted again recursively. In rare cases it is impossible to have a consistent
stacklevelfor deprecation warnings because different ways of calling a function will produce different call stacks.. In those cases, use