def test_clean_up(pytester: Pytester) -> None:
    """Test that the plugin cleans up after itself."""
    # This is tough to test behaviorally because the cleanup really runs last.
    # So the test make several implementation assumptions:
    # - Cleanup is done in pytest_unconfigure().
    # - Not a hook wrapper.
    # So we can add a hook wrapper ourselves to test what it does.
    pytester.makefile(".ini", pytest="[pytest]\npythonpath=I_SHALL_BE_REMOVED\n")
    pytester.makepyfile(test_foo="""def test_foo(): pass""")

    before: list[str] | None = None
    after: list[str] | None = None

    class Plugin:
        @pytest.hookimpl(tryfirst=True)
        def pytest_unconfigure(self) -> None:
            nonlocal before
            before = sys.path.copy()

    result = pytester.runpytest_inprocess(plugins=[Plugin()])
    after = sys.path.copy()
    assert result.ret == 0

    assert before is not None
    assert after is not None
    assert any("I_SHALL_BE_REMOVED" in entry for entry in before)
    assert not any("I_SHALL_BE_REMOVED" in entry for entry in after)
Test that the plugin cleans up after itself.
testing/test_python_path.py