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

Function test_clean_up

testing/test_pythonpath.py:83–110  ·  view source on GitHub ↗

Test that the pythonpath plugin cleans up after itself.

(pytester: Pytester)

Source from the content-addressed store, hash-verified

81
82
83def test_clean_up(pytester: Pytester) -> None:
84 """Test that the pythonpath plugin cleans up after itself."""
85 # This is tough to test behaviorly because the cleanup really runs last.
86 # So the test make several implementation assumptions:
87 # - Cleanup is done in pytest_unconfigure().
88 # - Not a hookwrapper.
89 # So we can add a hookwrapper ourselves to test what it does.
90 pytester.makefile(".ini", pytest="[pytest]\npythonpath=I_SHALL_BE_REMOVED\n")
91 pytester.makepyfile(test_foo="""def test_foo(): pass""")
92
93 before: Optional[List[str]] = None
94 after: Optional[List[str]] = None
95
96 class Plugin:
97 @pytest.hookimpl(hookwrapper=True, tryfirst=True)
98 def pytest_unconfigure(self) -> Generator[None, None, None]:
99 nonlocal before, after
100 before = sys.path.copy()
101 yield
102 after = sys.path.copy()
103
104 result = pytester.runpytest_inprocess(plugins=[Plugin()])
105 assert result.ret == 0
106
107 assert before is not None
108 assert after is not None
109 assert any("I_SHALL_BE_REMOVED" in entry for entry in before)
110 assert not any("I_SHALL_BE_REMOVED" in entry for entry in after)

Callers

nothing calls this directly

Calls 4

PluginClass · 0.70
makefileMethod · 0.45
makepyfileMethod · 0.45
runpytest_inprocessMethod · 0.45

Tested by

no test coverage detected