Skip to content

Commit

Permalink
[7.0.x] Make 'warnings' and 'deselected' in assert_outcomes optional (#…
Browse files Browse the repository at this point in the history
…9566)

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
  • Loading branch information
github-actions[bot] and nicoddemus committed Jan 27, 2022
1 parent f17525d commit b304499
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/_pytest/pytester.py
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
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
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)

0 comments on commit b304499

Please sign in to comment.