tests: harden test_reprcompare_notin, factor out callop (#6764)

* tests: assertion: factor out `callop`, typing

* tests: harden test_reprcompare_notin
This commit is contained in:
Daniel Hahler 2020-03-08 01:23:19 +01:00 committed by GitHub
parent e3cf4fc258
commit bce1d40fb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View File

@ -2,6 +2,8 @@
support for presenting detailed information in failing assertions. support for presenting detailed information in failing assertions.
""" """
import sys import sys
from typing import Any
from typing import List
from typing import Optional from typing import Optional
from _pytest.assertion import rewrite from _pytest.assertion import rewrite
@ -9,6 +11,7 @@ from _pytest.assertion import truncate
from _pytest.assertion import util from _pytest.assertion import util
from _pytest.assertion.rewrite import assertstate_key from _pytest.assertion.rewrite import assertstate_key
from _pytest.compat import TYPE_CHECKING from _pytest.compat import TYPE_CHECKING
from _pytest.config import Config
from _pytest.config import hookimpl from _pytest.config import hookimpl
if TYPE_CHECKING: if TYPE_CHECKING:
@ -170,5 +173,7 @@ def pytest_sessionfinish(session):
assertstate.hook.set_session(None) assertstate.hook.set_session(None)
def pytest_assertrepr_compare(config, op, left, right): def pytest_assertrepr_compare(
config: Config, op: str, left: Any, right: Any
) -> Optional[List[str]]:
return util.assertrepr_compare(config=config, op=op, left=left, right=right) return util.assertrepr_compare(config=config, op=op, left=left, right=right)

View File

@ -1,6 +1,9 @@
import collections.abc as collections_abc import collections.abc as collections_abc
import sys import sys
import textwrap import textwrap
from typing import Any
from typing import List
from typing import Optional
import attr import attr
@ -310,9 +313,13 @@ class TestBinReprIntegration:
result.stdout.fnmatch_lines(["*test_hello*FAIL*", "*test_check*PASS*"]) result.stdout.fnmatch_lines(["*test_hello*FAIL*", "*test_check*PASS*"])
def callequal(left, right, verbose=0): def callop(op: str, left: Any, right: Any, verbose: int = 0) -> Optional[List[str]]:
config = mock_config(verbose=verbose) config = mock_config(verbose=verbose)
return plugin.pytest_assertrepr_compare(config, "==", left, right) return plugin.pytest_assertrepr_compare(config, op, left, right)
def callequal(left: Any, right: Any, verbose: int = 0) -> Optional[List[str]]:
return callop("==", left, right, verbose)
class TestAssert_reprcompare: class TestAssert_reprcompare:
@ -1068,10 +1075,13 @@ def test_rewritten(testdir):
assert testdir.runpytest().ret == 0 assert testdir.runpytest().ret == 0
def test_reprcompare_notin(): def test_reprcompare_notin() -> None:
config = mock_config() assert callop("not in", "foo", "aaafoobbb") == [
detail = plugin.pytest_assertrepr_compare(config, "not in", "foo", "aaafoobbb")[1:] "'foo' not in 'aaafoobbb'",
assert detail == ["'foo' is contained here:", " aaafoobbb", "? +++"] "'foo' is contained here:",
" aaafoobbb",
"? +++",
]
def test_reprcompare_whitespaces(): def test_reprcompare_whitespaces():