September 2017
Intermediate to advanced
222 pages
5h 1m
English
The recwarn builtin fixture is used to examine warnings generated by code under test. In Python, you can add warnings that work a lot like assertions, but are used for things that don’t need to stop execution. For example, suppose we want to stop supporting a function that we wish we had never put into a package but was released for others to use. We can put a warning in the code and leave it there for a release or two:
| | import warnings |
| | import pytest |
| | |
| | |
| | def lame_function(): |
| | warnings.warn("Please stop using this", DeprecationWarning) |
| | # rest of function |
We can make sure the warning is getting issued correctly with a test:
| | def test_lame_function(recwarn): |
| | lame_function() ... |
Read now
Unlock full access