Skip to content

Commit bce1d40

Browse files
authored
tests: harden test_reprcompare_notin, factor out callop (#6764)
* tests: assertion: factor out `callop`, typing * tests: harden test_reprcompare_notin
1 parent e3cf4fc commit bce1d40

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/_pytest/assertion/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
support for presenting detailed information in failing assertions.
33
"""
44
import sys
5+
from typing import Any
6+
from typing import List
57
from typing import Optional
68

79
from _pytest.assertion import rewrite
810
from _pytest.assertion import truncate
911
from _pytest.assertion import util
1012
from _pytest.assertion.rewrite import assertstate_key
1113
from _pytest.compat import TYPE_CHECKING
14+
from _pytest.config import Config
1215
from _pytest.config import hookimpl
1316

1417
if TYPE_CHECKING:
@@ -170,5 +173,7 @@ def pytest_sessionfinish(session):
170173
assertstate.hook.set_session(None)
171174

172175

173-
def pytest_assertrepr_compare(config, op, left, right):
176+
def pytest_assertrepr_compare(
177+
config: Config, op: str, left: Any, right: Any
178+
) -> Optional[List[str]]:
174179
return util.assertrepr_compare(config=config, op=op, left=left, right=right)

testing/test_assertion.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import collections.abc as collections_abc
22
import sys
33
import textwrap
4+
from typing import Any
5+
from typing import List
6+
from typing import Optional
47

58
import attr
69

@@ -310,9 +313,13 @@ def test_check(list):
310313
result.stdout.fnmatch_lines(["*test_hello*FAIL*", "*test_check*PASS*"])
311314

312315

313-
def callequal(left, right, verbose=0):
316+
def callop(op: str, left: Any, right: Any, verbose: int = 0) -> Optional[List[str]]:
314317
config = mock_config(verbose=verbose)
315-
return plugin.pytest_assertrepr_compare(config, "==", left, right)
318+
return plugin.pytest_assertrepr_compare(config, op, left, right)
319+
320+
321+
def callequal(left: Any, right: Any, verbose: int = 0) -> Optional[List[str]]:
322+
return callop("==", left, right, verbose)
316323

317324

318325
class TestAssert_reprcompare:
@@ -1068,10 +1075,13 @@ def test_rewritten():
10681075
assert testdir.runpytest().ret == 0
10691076

10701077

1071-
def test_reprcompare_notin():
1072-
config = mock_config()
1073-
detail = plugin.pytest_assertrepr_compare(config, "not in", "foo", "aaafoobbb")[1:]
1074-
assert detail == ["'foo' is contained here:", " aaafoobbb", "? +++"]
1078+
def test_reprcompare_notin() -> None:
1079+
assert callop("not in", "foo", "aaafoobbb") == [
1080+
"'foo' not in 'aaafoobbb'",
1081+
"'foo' is contained here:",
1082+
" aaafoobbb",
1083+
"? +++",
1084+
]
10751085

10761086

10771087
def test_reprcompare_whitespaces():

0 commit comments

Comments
 (0)