Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,15 @@ def assert_outcomes(
errors: int = 0,
xpassed: int = 0,
xfailed: int = 0,
warnings: int = 0,
deselected: int = 0,
warnings: Optional[int] = None,
deselected: Optional[int] = 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
from _pytest.pytester_assertions import assert_outcomes

Expand Down
15 changes: 9 additions & 6 deletions src/_pytest/pytester_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# hence cannot be subject to assertion rewriting, which requires a
# module to not be already imported.
from typing import Dict
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import Union
Expand Down Expand Up @@ -42,8 +43,8 @@ def assert_outcomes(
errors: int = 0,
xpassed: int = 0,
xfailed: int = 0,
warnings: int = 0,
deselected: int = 0,
warnings: Optional[int] = None,
deselected: Optional[int] = 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."""
Expand All @@ -56,8 +57,6 @@ def assert_outcomes(
"errors": outcomes.get("errors", 0),
"xpassed": outcomes.get("xpassed", 0),
"xfailed": outcomes.get("xfailed", 0),
"warnings": outcomes.get("warnings", 0),
"deselected": outcomes.get("deselected", 0),
}
expected = {
"passed": passed,
Expand All @@ -66,7 +65,11 @@ def assert_outcomes(
"errors": errors,
"xpassed": xpassed,
"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
4 changes: 4 additions & 0 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ def test_with_warning():
)
result = pytester.runpytest()
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:
Expand All @@ -849,3 +851,5 @@ def test_two():
)
result = pytester.runpytest("-k", "test_one")
result.assert_outcomes(passed=1, deselected=1)
# If deselected is not passed, it is not checked at all.
result.assert_outcomes(passed=1)