From 5a1d6695a4436736b6ada354028e110bd80c6c11 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 27 Jan 2022 08:18:36 -0300 Subject: [PATCH] [7.0.x] Make 'warnings' and 'deselected' in assert_outcomes optional --- src/_pytest/pytester.py | 12 ++++++++---- src/_pytest/pytester_assertions.py | 15 +++++++++------ testing/test_pytester.py | 4 ++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 84547535d5f..363a3727447 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -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 diff --git a/src/_pytest/pytester_assertions.py b/src/_pytest/pytester_assertions.py index 6a5aabece47..657e4db5fc3 100644 --- a/src/_pytest/pytester_assertions.py +++ b/src/_pytest/pytester_assertions.py @@ -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 @@ -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.""" @@ -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, @@ -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 diff --git a/testing/test_pytester.py b/testing/test_pytester.py index bc6e52aba0e..9f7cf42b77c 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -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: @@ -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)