Skip to content

Commit 42c9fe0

Browse files
authored
Merge pull request #13772 from macdjord/improve_ci_detection
Only enable CI mode is `$CI` or `$BUILD_NUMBER` is set to a non-empty string
2 parents 9498a82 + 304743c commit 42c9fe0

File tree

8 files changed

+27
-8
lines changed

8 files changed

+27
-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.breaking.rst

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

doc/en/explanation/ci.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ 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 when either one of these environment variables are set to a non-empty value:
2221

2322
* `CI`: used by many CI systems.
2423
* `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 a non-empty value, pytest acknowledges that is running in a CI process. 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 a non-empty value, pytest acknowledges that is running in a CI process. Alternative to :envvar:`CI`. 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 a non-empty value, 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/compat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,6 @@ def __call__(self) -> bool:
308308

309309
def running_on_ci() -> bool:
310310
"""Check if we're currently running on a CI system."""
311+
# Only enable CI mode if one of these env variables is defined and non-empty.
311312
env_vars = ["CI", "BUILD_NUMBER"]
312-
return any(var in os.environ for var in env_vars)
313+
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 a non-empty value, 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_assertion.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ def test_full_diff():
567567
result = pytester.runpytest()
568568
result.stdout.fnmatch_lines(["E Full diff:"])
569569

570+
# Setting CI to empty string is same as having it undefined
571+
monkeypatch.setenv("CI", "")
572+
result = pytester.runpytest()
573+
result.stdout.fnmatch_lines(["E Use -v to get more diff"])
574+
570575
monkeypatch.delenv("CI", raising=False)
571576
result = pytester.runpytest()
572577
result.stdout.fnmatch_lines(["E Use -v to get more diff"])
@@ -1465,6 +1470,17 @@ def test_many_lines():
14651470
result = pytester.runpytest("-vv")
14661471
result.stdout.fnmatch_lines(["* 6*"])
14671472

1473+
# Setting CI to empty string is same as having it undefined
1474+
monkeypatch.setenv("CI", "")
1475+
result = pytester.runpytest()
1476+
result.stdout.fnmatch_lines(
1477+
[
1478+
"*+ 1*",
1479+
"*+ 3*",
1480+
f"*truncated ({expected_truncated_lines} lines hidden)*use*-vv*",
1481+
]
1482+
)
1483+
14681484
monkeypatch.setenv("CI", "1")
14691485
result = pytester.runpytest()
14701486
result.stdout.fnmatch_lines(["* 6*"])

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)