diff --git a/AUTHORS b/AUTHORS index 52363a177bb..07bf59c0582 100644 --- a/AUTHORS +++ b/AUTHORS @@ -227,6 +227,7 @@ Jon Parise Jon Sonesen Jonas Obrist Jordan Guymon +Jordan Macdonald Jordan Moldow Jordan Speicher Joseph Hunkeler diff --git a/changelog/13766.breaking.rst b/changelog/13766.breaking.rst new file mode 100644 index 00000000000..d7bd5a9a0ee --- /dev/null +++ b/changelog/13766.breaking.rst @@ -0,0 +1,2 @@ +Previously, pytest would assume it was running in a CI/CD environment if either of the environment variables `$CI` or `$BUILD_NUMBER` was defined; +now, CI mode is only activated if at least one of those variables is defined and set to a *non-empty* value. diff --git a/doc/en/explanation/ci.rst b/doc/en/explanation/ci.rst index 45fe658d14f..6f6734f395b 100644 --- a/doc/en/explanation/ci.rst +++ b/doc/en/explanation/ci.rst @@ -17,8 +17,7 @@ adapt some of its behaviours. How CI is detected ------------------ -Pytest knows it is in a CI environment when either one of these environment variables are set, -regardless of their value: +Pytest knows it is in a CI environment when either one of these environment variables are set to a non-empty value: * `CI`: used by many CI systems. * `BUILD_NUMBER`: used by Jenkins. diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index 7ec1b110baf..443530a2006 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -1165,11 +1165,11 @@ Environment variables that can be used to change pytest's behavior. .. envvar:: CI -When set (regardless of value), pytest acknowledges that is running in a CI process. Alternative to ``BUILD_NUMBER`` variable. See also :ref:`ci-pipelines`. +When set to a non-empty value, pytest acknowledges that is running in a CI process. See also :ref:`ci-pipelines`. .. envvar:: BUILD_NUMBER -When set (regardless of value), pytest acknowledges that is running in a CI process. Alternative to CI variable. See also :ref:`ci-pipelines`. +When set to a non-empty value, pytest acknowledges that is running in a CI process. Alternative to :envvar:`CI`. See also :ref:`ci-pipelines`. .. envvar:: PYTEST_ADDOPTS @@ -2408,7 +2408,7 @@ All the command-line flags can be obtained by running ``pytest --help``:: Plugins that must be present for pytest to run Environment variables: - CI When set (regardless of value), pytest knows it is running in a CI process and does not truncate summary info + CI When set to a non-empty value, pytest knows it is running in a CI process and does not truncate summary info BUILD_NUMBER Equivalent to CI PYTEST_ADDOPTS Extra command line options PYTEST_PLUGINS Comma-separated plugins to load during startup diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index c00000c794d..2f5a4c863f9 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -308,5 +308,6 @@ def __call__(self) -> bool: def running_on_ci() -> bool: """Check if we're currently running on a CI system.""" + # Only enable CI mode if one of these env variables is defined and non-empty. env_vars = ["CI", "BUILD_NUMBER"] - return any(var in os.environ for var in env_vars) + return any(os.environ.get(var) for var in env_vars) diff --git a/src/_pytest/helpconfig.py b/src/_pytest/helpconfig.py index a531c75d28b..b2f6e5dd0ca 100644 --- a/src/_pytest/helpconfig.py +++ b/src/_pytest/helpconfig.py @@ -221,7 +221,7 @@ def showhelp(config: Config) -> None: vars = [ ( "CI", - "When set (regardless of value), pytest knows it is running in a " + "When set to a non-empty value, pytest knows it is running in a " "CI process and does not truncate summary info", ), ("BUILD_NUMBER", "Equivalent to CI"), diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 2c2830eb929..5179b13b0e9 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -567,6 +567,11 @@ def test_full_diff(): result = pytester.runpytest() result.stdout.fnmatch_lines(["E Full diff:"]) + # Setting CI to empty string is same as having it undefined + monkeypatch.setenv("CI", "") + result = pytester.runpytest() + result.stdout.fnmatch_lines(["E Use -v to get more diff"]) + monkeypatch.delenv("CI", raising=False) result = pytester.runpytest() result.stdout.fnmatch_lines(["E Use -v to get more diff"]) @@ -1465,6 +1470,17 @@ def test_many_lines(): result = pytester.runpytest("-vv") result.stdout.fnmatch_lines(["* 6*"]) + # Setting CI to empty string is same as having it undefined + monkeypatch.setenv("CI", "") + result = pytester.runpytest() + result.stdout.fnmatch_lines( + [ + "*+ 1*", + "*+ 3*", + f"*truncated ({expected_truncated_lines} lines hidden)*use*-vv*", + ] + ) + monkeypatch.setenv("CI", "1") result = pytester.runpytest() result.stdout.fnmatch_lines(["* 6*"]) diff --git a/testing/test_faulthandler.py b/testing/test_faulthandler.py index b308e89adbd..2362f9e1eaf 100644 --- a/testing/test_faulthandler.py +++ b/testing/test_faulthandler.py @@ -79,7 +79,7 @@ def test_disabled(): pytest.param( True, marks=pytest.mark.skipif( - "CI" in os.environ + bool(os.environ.get("CI")) and sys.platform == "linux" and sys.version_info >= (3, 14), reason="sometimes crashes on CI because of truncated outputs (#7022)",