Skip to content

Commit 85e0f67

Browse files
authored
Reset color-related envvars for testing (#11638)
Reset color-related environment variables in a fixture to prevent them from affecting test results. Otherwise, some of the tests fail e.g. if NO_COLOR is set in the calling environment.
1 parent e0d5754 commit 85e0f67

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

changelog/11638.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the selftests to pass correctly if ``FORCE_COLOR``, ``NO_COLOR`` or ``PY_COLORS`` is set in the calling environment.

testing/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ def set_column_width(monkeypatch: pytest.MonkeyPatch) -> None:
3131
monkeypatch.setenv("COLUMNS", "80")
3232

3333

34+
@pytest.fixture(autouse=True)
35+
def reset_colors(monkeypatch: pytest.MonkeyPatch) -> None:
36+
"""
37+
Reset all color-related variables to prevent them from affecting internal pytest output
38+
in tests that depend on it.
39+
"""
40+
monkeypatch.delenv("PY_COLORS", raising=False)
41+
monkeypatch.delenv("NO_COLOR", raising=False)
42+
monkeypatch.delenv("FORCE_COLOR", raising=False)
43+
44+
3445
@pytest.hookimpl(wrapper=True, tryfirst=True)
3546
def pytest_collection_modifyitems(items) -> Generator[None, None, None]:
3647
"""Prefer faster tests.

0 commit comments

Comments
 (0)