_pytest.recwarn module¶
recording warnings during test function execution.
-
for ... in
recwarn()[source]¶ Return a
WarningsRecorderinstance that records all warnings emitted by test functions.See http://docs.python.org/library/warnings.html for information on warning categories.
-
deprecated_call(func=None, *args, **kwargs)[source]¶ context manager that can be used to ensure a block of code triggers a
DeprecationWarningorPendingDeprecationWarning:>>> import warnings >>> def api_call_v2(): ... warnings.warn('use v3 of this api', DeprecationWarning) ... return 200 >>> with deprecated_call(): ... assert api_call_v2() == 200
deprecated_callcan also be used by passing a function and*argsand*kwargs, in which case it will ensure callingfunc(*args, **kwargs)produces one of the warnings types above.
-
warns(expected_warning: Optional[Union[Type[Warning], Tuple[Type[Warning], …]]], *args: Any, match: Optional[Union[str, Pattern]] = None, **kwargs: Any) → Union[WarningsChecker, Any][source]¶ Assert that code raises a particular class of warning.
Specifically, the parameter
expected_warningcan be a warning class or sequence of warning classes, and the inside thewithblock must issue a warning of that class or classes.This helper produces a list of
warnings.WarningMessageobjects, one for each warning raised.This function can be used as a context manager, or any of the other ways
pytest.raisescan be used:>>> with warns(RuntimeWarning): ... warnings.warn("my warning", RuntimeWarning)
In the context manager form you may use the keyword argument
matchto assert that the exception matches a text or regex:>>> with warns(UserWarning, match='must be 0 or None'): ... warnings.warn("value must be 0 or None", UserWarning) >>> with warns(UserWarning, match=r'must be \d+$'): ... warnings.warn("value must be 42", UserWarning) >>> with warns(UserWarning, match=r'must be \d+$'): ... warnings.warn("this is not here", UserWarning) Traceback (most recent call last): ... _pytest.outcomes.Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted...
-
class
WarningsRecorder[source]¶ Bases:
warnings.catch_warningsA context manager to record raised warnings.
Adapted from
warnings.catch_warnings.-
list¶ The list of recorded warnings.
-