From d3c3baed354db28ffbed0c5d4b76cd54c7e81d59 Mon Sep 17 00:00:00 2001 From: symonk Date: Wed, 28 Jul 2021 22:08:39 +0100 Subject: [PATCH 1/3] expose `warnings=` to pytester `assert_outcomes()` --- src/_pytest/pytester.py | 2 ++ src/_pytest/pytester_assertions.py | 3 +++ testing/test_pytester.py | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index af73b63920b..c8258d4b694 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -588,6 +588,7 @@ def assert_outcomes( errors: int = 0, xpassed: int = 0, xfailed: int = 0, + warnings: int = 0, ) -> 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.""" @@ -603,6 +604,7 @@ def assert_outcomes( errors=errors, xpassed=xpassed, xfailed=xfailed, + warnings=warnings, ) diff --git a/src/_pytest/pytester_assertions.py b/src/_pytest/pytester_assertions.py index 630c1d3331c..45aa41c5a94 100644 --- a/src/_pytest/pytester_assertions.py +++ b/src/_pytest/pytester_assertions.py @@ -42,6 +42,7 @@ def assert_outcomes( errors: int = 0, xpassed: int = 0, xfailed: int = 0, + warnings: int = 0, ) -> 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.""" @@ -54,6 +55,7 @@ def assert_outcomes( "errors": outcomes.get("errors", 0), "xpassed": outcomes.get("xpassed", 0), "xfailed": outcomes.get("xfailed", 0), + "warnings": outcomes.get("warnings", 0), } expected = { "passed": passed, @@ -62,5 +64,6 @@ def assert_outcomes( "errors": errors, "xpassed": xpassed, "xfailed": xfailed, + "warnings": warnings, } assert obtained == expected diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 7b16c69c23d..23809d114a1 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -847,3 +847,14 @@ def test_testdir_makefile_ext_empty_string_makes_file(testdir) -> None: """For backwards compat #8192""" p1 = testdir.makefile("", "") assert "test_testdir_makefile" in str(p1) + + +def test_pytester_assert_outcomes_warnings(pytester: Pytester) -> None: + p = pytester.makepyfile( + """ + def test_with_warning(): + pass + """ + ) + result = pytester.runpytest(p, "--strict") + result.assert_outcomes(passed=1, warnings=1) From 094d013a23edd731caf62dbcf4022f4b4aaa4f3a Mon Sep 17 00:00:00 2001 From: symonk Date: Wed, 28 Jul 2021 22:18:42 +0100 Subject: [PATCH 2/3] fix test fallout from adding warnings= to assert_outcomes() --- testing/test_nose.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_nose.py b/testing/test_nose.py index 77f79b53b3c..3b3d3d2f5fb 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -335,7 +335,7 @@ def test_failing(): """ ) result = pytester.runpytest(p) - result.assert_outcomes(skipped=1) + result.assert_outcomes(skipped=1, warnings=1) def test_SkipTest_in_test(pytester: Pytester) -> None: From d7e883e9dccf5a2871435514040524295d795833 Mon Sep 17 00:00:00 2001 From: symonk Date: Sat, 31 Jul 2021 15:02:29 +0100 Subject: [PATCH 3/3] #closes 8593 - Improve test and add a `changelog` entry for the change --- changelog/8953.feature.rst | 2 ++ testing/test_pytester.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 changelog/8953.feature.rst diff --git a/changelog/8953.feature.rst b/changelog/8953.feature.rst new file mode 100644 index 00000000000..aa60fa19ca2 --- /dev/null +++ b/changelog/8953.feature.rst @@ -0,0 +1,2 @@ +:class:`RunResult <_pytest.pytester.RunResult>` method :meth:`assert_outcomes <_pytest.pytester.RunResult.assert_outcomes>` now accepts a +``warnings`` argument to assert the total number of warnings captured. diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 23809d114a1..b0a94223c33 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -849,12 +849,15 @@ def test_testdir_makefile_ext_empty_string_makes_file(testdir) -> None: assert "test_testdir_makefile" in str(p1) +@pytest.mark.filterwarnings("default") def test_pytester_assert_outcomes_warnings(pytester: Pytester) -> None: - p = pytester.makepyfile( + pytester.makepyfile( """ + import warnings + def test_with_warning(): - pass + warnings.warn(UserWarning("some custom warning")) """ ) - result = pytester.runpytest(p, "--strict") + result = pytester.runpytest() result.assert_outcomes(passed=1, warnings=1)