脓包#
py.测试支持XFAIL/XPASS的技巧
- sympy.testing.pytest.raises(expectedException, code=None)[源代码]#
测试
code
引发异常expectedException
.code
可以是可调用的,例如lambda表达式或函数名。如果
code
不给或不给,raises
将返回上下文管理器以在中使用with
语句;然后要执行的代码来自with
.raises()
如果callable引发预期的异常,则不执行任何操作,否则将引发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
请注意,您不能通过
with raises
:>>> with raises(ZeroDivisionError): ... n = 1/0 # will execute and raise, aborting the ``with`` ... n = 9999/0 # never executed
这就是什么
with
假定管理器在第一个异常中处理异常:让异常在第一个异常中终止。要测试多个语句,您需要一个单独的
with
每种:>>> with raises(ZeroDivisionError): ... n = 1/0 # will execute and raise >>> with raises(ZeroDivisionError): ... n = 9999/0 # will also execute and raise
- sympy.testing.pytest.skip_under_pyodide(message)[源代码]#
Decorator to skip a test if running under pyodide.
- sympy.testing.pytest.warns(warningcls, *, match='', test_stacklevel=True)[源代码]#
就像发出警告一样,但也会发出警告。
>>> 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_stacklevel
makes it check that thestacklevel
parameter towarn()
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 theactive_deprecations_target
is a real target in theactive-deprecations.md
file.
- sympy.testing.pytest.warns_deprecated_sympy()[源代码]#
缩写为
warns(SymPyDeprecationWarning)
这是推荐的测试方法
SymPyDeprecationWarning
为SymPy中已弃用的功能发出。要测试其他警告,请使用warns
. 要在不声明发出警告的情况下抑制警告,请使用ignore_warnings
.备注
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
warnings
filters (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 theSymPyDeprecationWarning
from being emitted again recursively. In rare cases it is impossible to have a consistentstacklevel
for deprecation warnings because different ways of calling a function will produce different call stacks.. In those cases, usewarns(SymPyDeprecationWarning)
instead.