MCPcopy Create free account
hub / github.com/pytest-dev/pytest / pyfile_with_warnings

Function pyfile_with_warnings

testing/test_warnings.py:15–38  ·  view source on GitHub ↗

Create a test file which calls a function in a module which generates warnings.

(pytester: Pytester, request: FixtureRequest)

Source from the content-addressed store, hash-verified

13
14@pytest.fixture
15def 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")

Callers

nothing calls this directly

Calls 3

syspathinsertMethod · 0.45
makepyfileMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected