Skip to content

Commit b003c44

Browse files
author
Jordan Macdonald
committed
Only enable CI mode is $CI or $BUILD_NUMBER is set to a non-empty string
Previously, if either env variable was defined, CI mode would be activated, even if the variable was set to the empty string. This had some unfortunate consequences: * The easy and obvious method of setting those variables, i.e. `CI="${OTHER_VAR}"`, would not work, because `$CI` would always end up being defined even if `$OTHER_VAR` was undefined. * The easy and obvious method of clearing those variables at runtime, i.e. `CI="" pytest ...`, would not work, because again `$CI` would still be defined even though it was blank. Now, at least one of those variables must be set to a non-empty string in order to enabled CI mode. Closes #13766
1 parent 184f5f1 commit b003c44

File tree

7 files changed

+14
-8
lines changed

7 files changed

+14
-8
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Jon Parise
227227
Jon Sonesen
228228
Jonas Obrist
229229
Jordan Guymon
230+
Jordan Macdonald
230231
Jordan Moldow
231232
Jordan Speicher
232233
Joseph Hunkeler

changelog/13766.improvement.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Previously, PyTest would assume it was running in a CI/CD environment
2+
if either of the environment variables `$CI` or `$BUILD_NUMBER` was defined;
3+
the value of the defined variable didn't matter, including if the variable was set to the empty string.
4+
Now, CI mode is only activated if at least one of those variables is defined and set to a *non-empty* string.

doc/en/explanation/ci.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ adapt some of its behaviours.
1717
How CI is detected
1818
------------------
1919

20-
Pytest knows it is in a CI environment when either one of these environment variables are set,
21-
regardless of their value:
20+
Pytest knows it is in a CI environment
21+
when either one of these environment variables are set to any value other than the empty string:
2222

2323
* `CI`: used by many CI systems.
2424
* `BUILD_NUMBER`: used by Jenkins.

doc/en/reference/reference.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,11 +1165,11 @@ Environment variables that can be used to change pytest's behavior.
11651165

11661166
.. envvar:: CI
11671167

1168-
When set (regardless of value), pytest acknowledges that is running in a CI process. Alternative to ``BUILD_NUMBER`` variable. See also :ref:`ci-pipelines`.
1168+
When set to any value other than the empty string, pytest acknowledges that is running in a CI process. Alternative to ``BUILD_NUMBER`` variable. See also :ref:`ci-pipelines`.
11691169

11701170
.. envvar:: BUILD_NUMBER
11711171

1172-
When set (regardless of value), pytest acknowledges that is running in a CI process. Alternative to CI variable. See also :ref:`ci-pipelines`.
1172+
When set to any value other than the empty string, pytest acknowledges that is running in a CI process. Alternative to CI variable. See also :ref:`ci-pipelines`.
11731173

11741174
.. envvar:: PYTEST_ADDOPTS
11751175

@@ -2408,7 +2408,7 @@ All the command-line flags can be obtained by running ``pytest --help``::
24082408
Plugins that must be present for pytest to run
24092409

24102410
Environment variables:
2411-
CI When set (regardless of value), pytest knows it is running in a CI process and does not truncate summary info
2411+
CI When set to any value other than the empty string, pytest knows it is running in a CI process and does not truncate summary info
24122412
BUILD_NUMBER Equivalent to CI
24132413
PYTEST_ADDOPTS Extra command line options
24142414
PYTEST_PLUGINS Comma-separated plugins to load during startup

src/_pytest/assertion/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,5 +617,6 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> list[str]:
617617

618618
def running_on_ci() -> bool:
619619
"""Check if we're currently running on a CI system."""
620+
# Only enable CI mode if one of these env variables is defined and is a non-empty string
620621
env_vars = ["CI", "BUILD_NUMBER"]
621-
return any(var in os.environ for var in env_vars)
622+
return any(os.environ.get(var) for var in env_vars)

src/_pytest/helpconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def showhelp(config: Config) -> None:
221221
vars = [
222222
(
223223
"CI",
224-
"When set (regardless of value), pytest knows it is running in a "
224+
"When set to any value other than the empty string, pytest knows it is running in a "
225225
"CI process and does not truncate summary info",
226226
),
227227
("BUILD_NUMBER", "Equivalent to CI"),

testing/test_faulthandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_disabled():
7979
pytest.param(
8080
True,
8181
marks=pytest.mark.skipif(
82-
"CI" in os.environ
82+
bool(os.environ.get("CI"))
8383
and sys.platform == "linux"
8484
and sys.version_info >= (3, 14),
8585
reason="sometimes crashes on CI because of truncated outputs (#7022)",

0 commit comments

Comments
 (0)