Skip to content
1 change: 1 addition & 0 deletions changelog/13684.contrib.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make pytest's own testsuite insensitive to the presence of the ``CI`` environment variable -- by :user:`ogrisel`.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ markers = [
"slow",
# experimental mark for all tests using pexpect
"uses_pexpect",
# Disables the `remove_ci_env_var` autouse fixture on a given test that
# actually inspects whether the CI environment variable is set.
"keep_ci_var",
]

[tool.towncrier]
Expand Down
12 changes: 12 additions & 0 deletions testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,15 @@ def mock_timing(monkeypatch: MonkeyPatch):
result = MockTiming()
result.patch(monkeypatch)
return result


@pytest.fixture(autouse=True)
def remove_ci_env_var(monkeypatch: MonkeyPatch, request: pytest.FixtureRequest) -> None:
"""Make the test insensitive if it is running in CI or not.

Use `@pytest.mark.keep_ci_var` in a test to avoid applying this fixture, letting the test
see the real `CI` variable (if present).
"""
has_keep_ci_mark = request.node.get_closest_marker("keep_ci_var") is not None
if not has_keep_ci_mark:
monkeypatch.delenv("CI", raising=False)
4 changes: 2 additions & 2 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,10 @@ def test_map_over_nested_lists(self):
]

def test_map_over_mixed_sequence(self):
assert _recursive_sequence_map(sqrt, [4, (25, 64), [(49)]]) == [
assert _recursive_sequence_map(sqrt, [4, (25, 64), [49]]) == [
2,
(5, 8),
[(7)],
[7],
]

def test_map_over_sequence_like(self):
Expand Down
1 change: 1 addition & 0 deletions testing/test_faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def test_disabled():
assert result.ret == 0


@pytest.mark.keep_ci_var
@pytest.mark.parametrize(
"enabled",
[
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ passenv =
PYTEST_ADDOPTS
TERM
SETUPTOOLS_SCM_PRETEND_VERSION_FOR_PYTEST
CI
setenv =
_PYTEST_TOX_DEFAULT_POSARGS={env:_PYTEST_TOX_POSARGS_DOCTESTING:} {env:_PYTEST_TOX_POSARGS_LSOF:} {env:_PYTEST_TOX_POSARGS_XDIST:} {env:_PYTEST_FILES:}

Expand Down
Loading