脓包#
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_stacklevelmakes it check that thestacklevelparameter 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_targetis a real target in theactive-deprecations.mdfile.
- 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
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 theSymPyDeprecationWarningfrom being emitted again recursively. In rare cases it is impossible to have a consistentstacklevelfor deprecation warnings because different ways of calling a function will produce different call stacks.. In those cases, usewarns(SymPyDeprecationWarning)instead.