pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Pytester.syspathinsert() has no effect when using runpytest_subprocess()

Open plannigan opened this issue 3 years ago • 1 comments

Pytester.syspathinsert() uses monkeypatch to temporarily update sys.path for the length of the test. However, runpytest_subprocess() starts a new process to without any knowledge of requested changes to sys.path.

Using pytest 7.2.0, the first test case will pass, but the second will fail.

from pytest import Pytester

SOME_DIR = "foobar"


def test_syspathinsert__in_process__path_exists(pytester: Pytester):
    pytester.syspathinsert(SOME_DIR)
    pytester.makepyfile(
        f"""
        import sys

        def test_foo():
            assert "{SOME_DIR}" in sys.path
        """
    )

    result = pytester.runpytest_inprocess()

    result.assert_outcomes(passed=1)


def test_syspathinsert__sub_process__path_exists(pytester: Pytester):
    pytester.syspathinsert(SOME_DIR)
    pytester.makepyfile(
        f"""
        import sys

        def test_foo():
            assert "{SOME_DIR}" in sys.path
        """
    )

    result = pytester.runpytest_subprocess(timeout=1)

    result.assert_outcomes(passed=1)

plannigan avatar Jan 11 '23 15:01 plannigan

Created a PR : https://github.com/pytest-dev/pytest/pull/12812

Oreldm avatar Sep 13 '24 08:09 Oreldm