From c3e8ec89898d2b245a06c95fd4160fb56f5458af Mon Sep 17 00:00:00 2001 From: dheerajck Date: Sat, 23 Mar 2024 21:07:03 +0530 Subject: [PATCH 1/9] Added environment variable PYTEST_VERSION which specifies pytest version, this is set when pytest is running and unset just before pytest is stopped --- src/_pytest/config/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 7ff27643f10..3140e1c0fea 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -50,6 +50,7 @@ from .exceptions import PrintHelp as PrintHelp from .exceptions import UsageError as UsageError from .findpaths import determine_setup +from _pytest import __version__ import _pytest._code from _pytest._code import ExceptionInfo from _pytest._code import filter_traceback @@ -152,6 +153,7 @@ def main( :returns: An exit code. """ try: + os.environ["PYTEST_VERSION"] = __version__ try: config = _prepareconfig(args, plugins) except ConftestImportFailure as e: @@ -186,6 +188,8 @@ def main( for msg in e.args: tw.line(f"ERROR: {msg}\n", red=True) return ExitCode.USAGE_ERROR + finally: + os.environ.pop("PYTEST_VERSION", None) def console_main() -> int: From 6bb894a5215efe9435c1b31fe1bc4491b34b2378 Mon Sep 17 00:00:00 2001 From: dheerajck Date: Sat, 23 Mar 2024 21:27:56 +0530 Subject: [PATCH 2/9] Added ut to make sure PYTEST_VERSION is added and removed from env properly --- testing/test_runner.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/testing/test_runner.py b/testing/test_runner.py index 8cc496f7064..ed5a0f80e2c 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -1094,3 +1094,20 @@ def func() -> None: with pytest.raises(TypeError) as excinfo: OutcomeException(func) # type: ignore assert str(excinfo.value) == expected + + +def test_pytest_version(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + pytester.makepyfile( + """ + import pytest + import os + + + def test(): + assert os.environ.get("PYTEST_VERSION") == pytest.__version__ + """ + ) + result = pytester.runpytest_inprocess() + # assert result.ret == 0 + assert result.ret == ExitCode.OK + assert "PYTEST_VERSION" not in os.environ From e2947e7106fa0a0a76bfe315e8d56b6f52eacea4 Mon Sep 17 00:00:00 2001 From: dheerajck Date: Sat, 6 Apr 2024 12:25:22 +0530 Subject: [PATCH 3/9] Added author --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 53f7a8c2a16..3d1ecf98604 100644 --- a/AUTHORS +++ b/AUTHORS @@ -118,6 +118,7 @@ Daw-Ran Liou Debi Mishra Denis Kirisov Denivy Braiam Rück +Dheeraj C K Dhiren Serai Diego Russo Dmitry Dygalo From dff74a9bf41012f3ca82e4e140b890ab20feb305 Mon Sep 17 00:00:00 2001 From: dheerajck Date: Sat, 6 Apr 2024 12:31:14 +0530 Subject: [PATCH 4/9] Added changelog --- changelog/9502.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/9502.improvement.rst diff --git a/changelog/9502.improvement.rst b/changelog/9502.improvement.rst new file mode 100644 index 00000000000..7174d7dbf90 --- /dev/null +++ b/changelog/9502.improvement.rst @@ -0,0 +1 @@ +Added an environment variable which helps to check if a code is running from within a pytest run From 237ea1ac239229dbcb6bbc80ce887919fc621bd1 Mon Sep 17 00:00:00 2001 From: dj <112573278+dheerajck@users.noreply.github.com> Date: Sun, 7 Apr 2024 18:24:35 +0530 Subject: [PATCH 5/9] Apply suggestions from code review Co-authored-by: Bruno Oliveira --- changelog/9502.improvement.rst | 2 +- testing/test_runner.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/changelog/9502.improvement.rst b/changelog/9502.improvement.rst index 7174d7dbf90..2eaf6a72747 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 ed5a0f80e2c..c4fb85a7e9f 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -1096,7 +1096,7 @@ def func() -> 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(): """ ) result = pytester.runpytest_inprocess() - # assert result.ret == 0 assert result.ret == ExitCode.OK assert "PYTEST_VERSION" not in os.environ From d6d59fed4192eeb481930da1b780110a7e1f8c5a Mon Sep 17 00:00:00 2001 From: dheerajck Date: Sun, 7 Apr 2024 19:28:38 +0530 Subject: [PATCH 6/9] Added details about PYTEST_VERSION to doc/en/reference/reference.rst --- doc/en/reference/reference.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index 21890fbf63e..30ba4301d9d 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -1117,6 +1117,11 @@ When set (regardless of value), pytest acknowledges that is running in a CI proc This contains a command-line (parsed by the py:mod:`shlex` module) that will be **prepended** to the command line given by the user, see :ref:`adding default options` for more information. +.. envvar:: PYTEST_VERSION + +This environment variable is defined at the start of the pytest session and is undefined afterwards. +It contains the value of ``pytest.__version__``, and among other things can be used to easily check if a code is running from within a pytest run. + .. envvar:: PYTEST_CURRENT_TEST This is not meant to be set by users, but is set by pytest internally with the name of the current test so other From 2f8e436213954a0f24839251b727bba65025a2c8 Mon Sep 17 00:00:00 2001 From: dj <112573278+dheerajck@users.noreply.github.com> Date: Tue, 9 Apr 2024 19:49:11 +0530 Subject: [PATCH 7/9] Cleaning up, if there was a value set for PYTEST_VERSION before pytest run by the calling process, we set it back after pytest run Co-authored-by: Bruno Oliveira --- src/_pytest/config/__init__.py | 6 +++++- testing/test_runner.py | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 3140e1c0fea..6a8ac84ce2e 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -153,6 +153,7 @@ def main( :returns: An exit code. """ try: + old_pytest_version = os.environ.get("PYTEST_VERSION") os.environ["PYTEST_VERSION"] = __version__ try: config = _prepareconfig(args, plugins) @@ -189,7 +190,10 @@ def main( tw.line(f"ERROR: {msg}\n", red=True) return ExitCode.USAGE_ERROR finally: - os.environ.pop("PYTEST_VERSION", None) + if old_pytest_version is None: + os.environ.pop("PYTEST_VERSION", None) + else: + os.environ["PYTEST_VERSION"] = old_pytest_version def console_main() -> int: diff --git a/testing/test_runner.py b/testing/test_runner.py index c4fb85a7e9f..d3ddc5d9acb 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -1097,6 +1097,7 @@ def func() -> None: def test_pytest_version_env_var(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: + os.environ["PYTEST_VERSION"] = "old version" pytester.makepyfile( """ import pytest @@ -1109,4 +1110,4 @@ def test(): ) result = pytester.runpytest_inprocess() assert result.ret == ExitCode.OK - assert "PYTEST_VERSION" not in os.environ + assert os.environ["PYTEST_VERSION"] == "old version" From 95f70839c7e6f99c1697b86c6defdeba54581dc4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 9 Apr 2024 14:21:32 +0000 Subject: [PATCH 8/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/config/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 6a8ac84ce2e..849a87a6b3b 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -193,7 +193,7 @@ def main( if old_pytest_version is None: os.environ.pop("PYTEST_VERSION", None) else: - os.environ["PYTEST_VERSION"] = old_pytest_version + os.environ["PYTEST_VERSION"] = old_pytest_version def console_main() -> int: From c50f156754978a36968ea02de407d058d1d140da Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 18 Apr 2024 07:31:01 -0300 Subject: [PATCH 9/9] Update src/_pytest/config/__init__.py --- src/_pytest/config/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 849a87a6b3b..306b14cce28 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -152,8 +152,8 @@ def main( :returns: An exit code. """ + old_pytest_version = os.environ.get("PYTEST_VERSION") try: - old_pytest_version = os.environ.get("PYTEST_VERSION") os.environ["PYTEST_VERSION"] = __version__ try: config = _prepareconfig(args, plugins)