Skip to content

Commit bf92deb

Browse files
ogriselwebknjaz
andauthored
Fix passenv CI in tox ini and make tests insensitive to the presence of the CI env variable (#13684)
As discussed in #13684. --------- Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]>
1 parent 57c15f7 commit bf92deb

File tree

6 files changed

+20
-2
lines changed

6 files changed

+20
-2
lines changed

changelog/13684.contrib.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make pytest's own testsuite insensitive to the presence of the ``CI`` environment variable -- by :user:`ogrisel`.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ markers = [
428428
"slow",
429429
# experimental mark for all tests using pexpect
430430
"uses_pexpect",
431+
# Disables the `remove_ci_env_var` autouse fixture on a given test that
432+
# actually inspects whether the CI environment variable is set.
433+
"keep_ci_var",
431434
]
432435

433436
[tool.towncrier]

testing/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,15 @@ def mock_timing(monkeypatch: MonkeyPatch):
237237
result = MockTiming()
238238
result.patch(monkeypatch)
239239
return result
240+
241+
242+
@pytest.fixture(autouse=True)
243+
def remove_ci_env_var(monkeypatch: MonkeyPatch, request: pytest.FixtureRequest) -> None:
244+
"""Make the test insensitive if it is running in CI or not.
245+
246+
Use `@pytest.mark.keep_ci_var` in a test to avoid applying this fixture, letting the test
247+
see the real `CI` variable (if present).
248+
"""
249+
has_keep_ci_mark = request.node.get_closest_marker("keep_ci_var") is not None
250+
if not has_keep_ci_mark:
251+
monkeypatch.delenv("CI", raising=False)

testing/python/approx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,10 +1085,10 @@ def test_map_over_nested_lists(self):
10851085
]
10861086

10871087
def test_map_over_mixed_sequence(self):
1088-
assert _recursive_sequence_map(sqrt, [4, (25, 64), [(49)]]) == [
1088+
assert _recursive_sequence_map(sqrt, [4, (25, 64), [49]]) == [
10891089
2,
10901090
(5, 8),
1091-
[(7)],
1091+
[7],
10921092
]
10931093

10941094
def test_map_over_sequence_like(self):

testing/test_faulthandler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def test_disabled():
7171
assert result.ret == 0
7272

7373

74+
@pytest.mark.keep_ci_var
7475
@pytest.mark.parametrize(
7576
"enabled",
7677
[

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ passenv =
5252
PYTEST_ADDOPTS
5353
TERM
5454
SETUPTOOLS_SCM_PRETEND_VERSION_FOR_PYTEST
55+
CI
5556
setenv =
5657
_PYTEST_TOX_DEFAULT_POSARGS={env:_PYTEST_TOX_POSARGS_DOCTESTING:} {env:_PYTEST_TOX_POSARGS_LSOF:} {env:_PYTEST_TOX_POSARGS_XDIST:} {env:_PYTEST_FILES:}
5758

0 commit comments

Comments
 (0)