Skip to content

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

@plannigan

Description

@plannigan
Contributor

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: pytesterrelated to the pytester builtin plugintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @Zac-HD@Oreldm@plannigan

      Issue actions

        Pytester.syspathinsert() has no effect when using runpytest_subprocess() · Issue #10651 · pytest-dev/pytest