Skip to content

Commit ef5d81a

Browse files
authored
add assert_outcomes(warnings=) functionality to RunResult
* expose `warnings=` to pytester `assert_outcomes()` * fix test fallout from adding warnings= to assert_outcomes() * #closes 8593 - Improve test and add a `changelog` entry for the change
1 parent c27db3b commit ef5d81a

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

changelog/8953.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:class:`RunResult <_pytest.pytester.RunResult>` method :meth:`assert_outcomes <_pytest.pytester.RunResult.assert_outcomes>` now accepts a
2+
``warnings`` argument to assert the total number of warnings captured.

src/_pytest/pytester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ def assert_outcomes(
588588
errors: int = 0,
589589
xpassed: int = 0,
590590
xfailed: int = 0,
591+
warnings: int = 0,
591592
) -> None:
592593
"""Assert that the specified outcomes appear with the respective
593594
numbers (0 means it didn't occur) in the text output from a test run."""
@@ -603,6 +604,7 @@ def assert_outcomes(
603604
errors=errors,
604605
xpassed=xpassed,
605606
xfailed=xfailed,
607+
warnings=warnings,
606608
)
607609

608610

src/_pytest/pytester_assertions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def assert_outcomes(
4242
errors: int = 0,
4343
xpassed: int = 0,
4444
xfailed: int = 0,
45+
warnings: int = 0,
4546
) -> None:
4647
"""Assert that the specified outcomes appear with the respective
4748
numbers (0 means it didn't occur) in the text output from a test run."""
@@ -54,6 +55,7 @@ def assert_outcomes(
5455
"errors": outcomes.get("errors", 0),
5556
"xpassed": outcomes.get("xpassed", 0),
5657
"xfailed": outcomes.get("xfailed", 0),
58+
"warnings": outcomes.get("warnings", 0),
5759
}
5860
expected = {
5961
"passed": passed,
@@ -62,5 +64,6 @@ def assert_outcomes(
6264
"errors": errors,
6365
"xpassed": xpassed,
6466
"xfailed": xfailed,
67+
"warnings": warnings,
6568
}
6669
assert obtained == expected

testing/test_nose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def test_failing():
335335
"""
336336
)
337337
result = pytester.runpytest(p)
338-
result.assert_outcomes(skipped=1)
338+
result.assert_outcomes(skipped=1, warnings=1)
339339

340340

341341
def test_SkipTest_in_test(pytester: Pytester) -> None:

testing/test_pytester.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,17 @@ def test_testdir_makefile_ext_empty_string_makes_file(testdir) -> None:
847847
"""For backwards compat #8192"""
848848
p1 = testdir.makefile("", "")
849849
assert "test_testdir_makefile" in str(p1)
850+
851+
852+
@pytest.mark.filterwarnings("default")
853+
def test_pytester_assert_outcomes_warnings(pytester: Pytester) -> None:
854+
pytester.makepyfile(
855+
"""
856+
import warnings
857+
858+
def test_with_warning():
859+
warnings.warn(UserWarning("some custom warning"))
860+
"""
861+
)
862+
result = pytester.runpytest()
863+
result.assert_outcomes(passed=1, warnings=1)

0 commit comments

Comments
 (0)