Skip to content

Commit b304499

Browse files
github-actions[bot]nicoddemus
andauthoredJan 27, 2022
[7.0.x] Make 'warnings' and 'deselected' in assert_outcomes optional (#9566)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent f17525d commit b304499

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed
 

‎src/_pytest/pytester.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,15 @@ def assert_outcomes(
596596
errors: int = 0,
597597
xpassed: int = 0,
598598
xfailed: int = 0,
599-
warnings: int = 0,
600-
deselected: int = 0,
599+
warnings: Optional[int] = None,
600+
deselected: Optional[int] = None,
601601
) -> None:
602-
"""Assert that the specified outcomes appear with the respective
603-
numbers (0 means it didn't occur) in the text output from a test run."""
602+
"""
603+
Assert that the specified outcomes appear with the respective
604+
numbers (0 means it didn't occur) in the text output from a test run.
605+
606+
``warnings`` and ``deselected`` are only checked if not None.
607+
"""
604608
__tracebackhide__ = True
605609
from _pytest.pytester_assertions import assert_outcomes
606610

‎src/_pytest/pytester_assertions.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# hence cannot be subject to assertion rewriting, which requires a
55
# module to not be already imported.
66
from typing import Dict
7+
from typing import Optional
78
from typing import Sequence
89
from typing import Tuple
910
from typing import Union
@@ -42,8 +43,8 @@ def assert_outcomes(
4243
errors: int = 0,
4344
xpassed: int = 0,
4445
xfailed: int = 0,
45-
warnings: int = 0,
46-
deselected: int = 0,
46+
warnings: Optional[int] = None,
47+
deselected: Optional[int] = None,
4748
) -> None:
4849
"""Assert that the specified outcomes appear with the respective
4950
numbers (0 means it didn't occur) in the text output from a test run."""
@@ -56,8 +57,6 @@ def assert_outcomes(
5657
"errors": outcomes.get("errors", 0),
5758
"xpassed": outcomes.get("xpassed", 0),
5859
"xfailed": outcomes.get("xfailed", 0),
59-
"warnings": outcomes.get("warnings", 0),
60-
"deselected": outcomes.get("deselected", 0),
6160
}
6261
expected = {
6362
"passed": passed,
@@ -66,7 +65,11 @@ def assert_outcomes(
6665
"errors": errors,
6766
"xpassed": xpassed,
6867
"xfailed": xfailed,
69-
"warnings": warnings,
70-
"deselected": deselected,
7168
}
69+
if warnings is not None:
70+
obtained["warnings"] = outcomes.get("warnings", 0)
71+
expected["warnings"] = warnings
72+
if deselected is not None:
73+
obtained["deselected"] = outcomes.get("deselected", 0)
74+
expected["deselected"] = deselected
7275
assert obtained == expected

‎testing/test_pytester.py

+4
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ def test_with_warning():
835835
)
836836
result = pytester.runpytest()
837837
result.assert_outcomes(passed=1, warnings=1)
838+
# If warnings is not passed, it is not checked at all.
839+
result.assert_outcomes(passed=1)
838840

839841

840842
def test_pytester_outcomes_deselected(pytester: Pytester) -> None:
@@ -849,3 +851,5 @@ def test_two():
849851
)
850852
result = pytester.runpytest("-k", "test_one")
851853
result.assert_outcomes(passed=1, deselected=1)
854+
# If deselected is not passed, it is not checked at all.
855+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)
Please sign in to comment.