diff --git a/changelog/9502.improvement.rst b/changelog/9502.improvement.rst index 7174d7dbf..2eaf6a727 100644 --- a/changelog/9502.improvement.rst +++ b/changelog/9502.improvement.rst @@ -1 +1 @@ -Added an environment variable which helps to check if a code is running from within a pytest run +Added :envvar:`PYTEST_VERSION` environment variable which is defined at the start of the pytest session and undefined afterwards. It contains the value of ``pytest.__version__``, and among other things can be used to easily check if code is running from within a pytest run. diff --git a/testing/test_runner.py b/testing/test_runner.py index ed5a0f80e..c4fb85a7e 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -1096,7 +1096,7 @@ def test_outcome_exception_bad_msg() -> None: assert str(excinfo.value) == expected -def test_pytest_version(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: +def test_pytest_version_env_var(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: pytester.makepyfile( """ import pytest @@ -1108,6 +1108,5 @@ def test_pytest_version(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: """ ) result = pytester.runpytest_inprocess() - # assert result.ret == 0 assert result.ret == ExitCode.OK assert "PYTEST_VERSION" not in os.environ