Make 'warnings' and 'deselected' in assert_outcomes optional (#9475)

Fix #9471
This commit is contained in:
Bruno Oliveira 2022-01-27 08:18:36 -03:00 committed by GitHub
parent 396a7def75
commit f064942f2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 10 deletions

View File

@ -596,11 +596,15 @@ class RunResult:
errors: int = 0, errors: int = 0,
xpassed: int = 0, xpassed: int = 0,
xfailed: int = 0, xfailed: int = 0,
warnings: int = 0, warnings: Optional[int] = None,
deselected: int = 0, deselected: Optional[int] = None,
) -> None: ) -> None:
"""Assert that the specified outcomes appear with the respective """
numbers (0 means it didn't occur) in the text output from a test run.""" Assert that the specified outcomes appear with the respective
numbers (0 means it didn't occur) in the text output from a test run.
``warnings`` and ``deselected`` are only checked if not None.
"""
__tracebackhide__ = True __tracebackhide__ = True
from _pytest.pytester_assertions import assert_outcomes from _pytest.pytester_assertions import assert_outcomes

View File

@ -4,6 +4,7 @@
# hence cannot be subject to assertion rewriting, which requires a # hence cannot be subject to assertion rewriting, which requires a
# module to not be already imported. # module to not be already imported.
from typing import Dict from typing import Dict
from typing import Optional
from typing import Sequence from typing import Sequence
from typing import Tuple from typing import Tuple
from typing import Union from typing import Union
@ -42,8 +43,8 @@ def assert_outcomes(
errors: int = 0, errors: int = 0,
xpassed: int = 0, xpassed: int = 0,
xfailed: int = 0, xfailed: int = 0,
warnings: int = 0, warnings: Optional[int] = None,
deselected: int = 0, deselected: Optional[int] = None,
) -> None: ) -> None:
"""Assert that the specified outcomes appear with the respective """Assert that the specified outcomes appear with the respective
numbers (0 means it didn't occur) in the text output from a test run.""" numbers (0 means it didn't occur) in the text output from a test run."""
@ -56,8 +57,6 @@ def assert_outcomes(
"errors": outcomes.get("errors", 0), "errors": outcomes.get("errors", 0),
"xpassed": outcomes.get("xpassed", 0), "xpassed": outcomes.get("xpassed", 0),
"xfailed": outcomes.get("xfailed", 0), "xfailed": outcomes.get("xfailed", 0),
"warnings": outcomes.get("warnings", 0),
"deselected": outcomes.get("deselected", 0),
} }
expected = { expected = {
"passed": passed, "passed": passed,
@ -66,7 +65,11 @@ def assert_outcomes(
"errors": errors, "errors": errors,
"xpassed": xpassed, "xpassed": xpassed,
"xfailed": xfailed, "xfailed": xfailed,
"warnings": warnings,
"deselected": deselected,
} }
if warnings is not None:
obtained["warnings"] = outcomes.get("warnings", 0)
expected["warnings"] = warnings
if deselected is not None:
obtained["deselected"] = outcomes.get("deselected", 0)
expected["deselected"] = deselected
assert obtained == expected assert obtained == expected

View File

@ -835,6 +835,8 @@ def test_pytester_assert_outcomes_warnings(pytester: Pytester) -> None:
) )
result = pytester.runpytest() result = pytester.runpytest()
result.assert_outcomes(passed=1, warnings=1) result.assert_outcomes(passed=1, warnings=1)
# If warnings is not passed, it is not checked at all.
result.assert_outcomes(passed=1)
def test_pytester_outcomes_deselected(pytester: Pytester) -> None: def test_pytester_outcomes_deselected(pytester: Pytester) -> None:
@ -849,3 +851,5 @@ def test_pytester_outcomes_deselected(pytester: Pytester) -> None:
) )
result = pytester.runpytest("-k", "test_one") result = pytester.runpytest("-k", "test_one")
result.assert_outcomes(passed=1, deselected=1) result.assert_outcomes(passed=1, deselected=1)
# If deselected is not passed, it is not checked at all.
result.assert_outcomes(passed=1)