pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.92k stars 2.66k forks source link

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

Open plannigan opened 1 year ago

plannigan commented 1 year ago

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)
Oreldm commented 2 weeks ago

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