Skip to content

Commit c34d9b5

Browse files
committed
Factorize monkeypatch.delenv('CI', raising=False) into remove_ci_env_var fixture
1 parent 1efb477 commit c34d9b5

File tree

4 files changed

+18
-29
lines changed

4 files changed

+18
-29
lines changed

testing/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,9 @@ def mock_timing(monkeypatch: MonkeyPatch):
237237
result = MockTiming()
238238
result.patch(monkeypatch)
239239
return result
240+
241+
242+
@pytest.fixture
243+
def remove_ci_env_var(monkeypatch: MonkeyPatch) -> None:
244+
"""Make the test insensitive if it is running in CI or not."""
245+
monkeypatch.delenv("CI", raising=False)

testing/python/approx.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from _pytest.python_api import _recursive_sequence_map
1515
import pytest
1616
from pytest import approx
17-
from pytest import MonkeyPatch
1817

1918

2019
inf, nan = float("inf"), float("nan")
@@ -285,9 +284,8 @@ def test_error_messages_invalid_args(self, assert_approx_raises_regex):
285284
)
286285

287286
def test_error_messages_with_different_verbosity(
288-
self, assert_approx_raises_regex, monkeypatch: MonkeyPatch
287+
self, assert_approx_raises_regex, remove_ci_env_var
289288
):
290-
monkeypatch.delenv("CI")
291289
np = pytest.importorskip("numpy")
292290
for v in [0, 1, 2]:
293291
# Verbosity level doesn't affect the error message for scalars
@@ -1089,10 +1087,10 @@ def test_map_over_nested_lists(self):
10891087
]
10901088

10911089
def test_map_over_mixed_sequence(self):
1092-
assert _recursive_sequence_map(sqrt, [4, (25, 64), [(49)]]) == [
1090+
assert _recursive_sequence_map(sqrt, [4, (25, 64), [49]]) == [
10931091
2,
10941092
(5, 8),
1095-
[(7)],
1093+
[7],
10961094
]
10971095

10981096
def test_map_over_sequence_like(self):

testing/test_assertion.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,12 @@ def test(check_first):
111111
result.stdout.fnmatch_lines([expected])
112112

113113
def test_rewrite_assertions_pytester_plugin(
114-
self, pytester: Pytester, monkeypatch: MonkeyPatch
114+
self, pytester: Pytester, remove_ci_env_var
115115
) -> None:
116116
"""
117117
Assertions in the pytester plugin must also benefit from assertion
118118
rewriting (#1920).
119119
"""
120-
monkeypatch.delenv("CI")
121120
pytester.makepyfile(
122121
"""
123122
pytest_plugins = ['pytester']
@@ -455,9 +454,8 @@ def test_multiline_text_diff(self) -> None:
455454
assert "- eggs" in diff
456455
assert "+ spam" in diff
457456

458-
def test_bytes_diff_normal(self, monkeypatch: MonkeyPatch) -> None:
457+
def test_bytes_diff_normal(self, remove_ci_env_var) -> None:
459458
"""Check special handling for bytes diff (#5260)"""
460-
monkeypatch.delenv("CI")
461459
diff = callequal(b"spam", b"eggs")
462460

463461
assert diff == [
@@ -534,24 +532,20 @@ def test_list(self) -> None:
534532
),
535533
],
536534
)
537-
def test_iterable_full_diff(
538-
self, left, right, expected, monkeypatch: MonkeyPatch
539-
) -> None:
535+
def test_iterable_full_diff(self, left, right, expected, remove_ci_env_var) -> None:
540536
"""Test the full diff assertion failure explanation.
541537
542538
When verbose is False, then just a -v notice to get the diff is rendered,
543539
when verbose is True, then ndiff of the pprint is returned.
544540
"""
545-
monkeypatch.delenv("CI")
546541
expl = callequal(left, right, verbose=0)
547542
assert expl is not None
548543
assert expl[-1] == "Use -v to get more diff"
549544
verbose_expl = callequal(left, right, verbose=1)
550545
assert verbose_expl is not None
551546
assert "\n".join(verbose_expl).endswith(textwrap.dedent(expected).strip())
552547

553-
def test_iterable_quiet(self, monkeypatch: MonkeyPatch) -> None:
554-
monkeypatch.delenv("CI")
548+
def test_iterable_quiet(self, remove_ci_env_var) -> None:
555549
expl = callequal([1, 2], [10, 2], verbose=-1)
556550
assert expl == [
557551
"[1, 2] == [10, 2]",
@@ -721,8 +715,7 @@ def test_dict(self) -> None:
721715
assert expl is not None
722716
assert len(expl) > 1
723717

724-
def test_dict_omitting(self, monkeypatch: MonkeyPatch) -> None:
725-
monkeypatch.delenv("CI")
718+
def test_dict_omitting(self, remove_ci_env_var) -> None:
726719
lines = callequal({"a": 0, "b": 1}, {"a": 1, "b": 1})
727720
assert lines is not None
728721
assert lines[2].startswith("Omitting 1 identical item")
@@ -990,10 +983,7 @@ def test_dataclasses(self, pytester: Pytester) -> None:
990983
consecutive=True,
991984
)
992985

993-
def test_recursive_dataclasses(
994-
self, pytester: Pytester, monkeypatch: MonkeyPatch
995-
) -> None:
996-
monkeypatch.delenv("CI")
986+
def test_recursive_dataclasses(self, pytester: Pytester, remove_ci_env_var) -> None:
997987
p = pytester.copy_example("dataclasses/test_compare_recursive_dataclasses.py")
998988
result = pytester.runpytest(p)
999989
result.assert_outcomes(failed=1, passed=0)
@@ -1225,9 +1215,7 @@ def __eq__(self, other): # pragma: no cover
12251215

12261216

12271217
class TestAssert_reprcompare_namedtuple:
1228-
def test_namedtuple(self, monkeypatch: MonkeyPatch) -> None:
1229-
monkeypatch.delenv("CI")
1230-
1218+
def test_namedtuple(self, remove_ci_env_var) -> None:
12311219
class NT(NamedTuple):
12321220
a: Any
12331221
b: Any
@@ -1250,9 +1238,7 @@ class NT(NamedTuple):
12501238
"Use -v to get more diff",
12511239
]
12521240

1253-
def test_comparing_two_different_namedtuple(self, monkeypatch: MonkeyPatch) -> None:
1254-
monkeypatch.delenv("CI")
1255-
1241+
def test_comparing_two_different_namedtuple(self, remove_ci_env_var) -> None:
12561242
class NT1(NamedTuple):
12571243
a: Any
12581244
b: Any

testing/test_terminal.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,8 +2535,7 @@ class X:
25352535
assert reason == message
25362536

25372537

2538-
def test_line_with_reprcrash(monkeypatch: MonkeyPatch) -> None:
2539-
monkeypatch.delenv("CI")
2538+
def test_line_with_reprcrash(monkeypatch: MonkeyPatch, remove_ci_env_var) -> None:
25402539
mocked_verbose_word = "FAILED"
25412540

25422541
mocked_pos = "some::nodeid"

0 commit comments

Comments
 (0)