Create a test file which calls a function in a module which generates warnings.
(pytester: Pytester, request: FixtureRequest)
| 13 | |
| 14 | @pytest.fixture |
| 15 | def pyfile_with_warnings(pytester: Pytester, request: FixtureRequest) -> str: |
| 16 | """Create a test file which calls a function in a module which generates warnings.""" |
| 17 | pytester.syspathinsert() |
| 18 | test_name = request.function.__name__ |
| 19 | module_name = test_name.lstrip("test_") + "_module" |
| 20 | test_file = pytester.makepyfile( |
| 21 | """ |
| 22 | import {module_name} |
| 23 | def test_func(): |
| 24 | assert {module_name}.foo() == 1 |
| 25 | """.format( |
| 26 | module_name=module_name |
| 27 | ), |
| 28 | **{ |
| 29 | module_name: """ |
| 30 | import warnings |
| 31 | def foo(): |
| 32 | warnings.warn(UserWarning("user warning")) |
| 33 | warnings.warn(RuntimeWarning("runtime warning")) |
| 34 | return 1 |
| 35 | """, |
| 36 | }, |
| 37 | ) |
| 38 | return str(test_file) |
| 39 | |
| 40 | |
| 41 | @pytest.mark.filterwarnings("default::UserWarning", "default::RuntimeWarning") |
nothing calls this directly
no test coverage detected