From 948a5d5ac674b6db1d8df30072fad28ef2809381 Mon Sep 17 00:00:00 2001 From: Victor Maryama Date: Wed, 9 Jan 2019 12:31:26 +0100 Subject: [PATCH 1/5] Added test for Issue 4617 --- testing/test_warnings.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 3bac9a545..45e196149 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -683,3 +683,27 @@ class TestAssertionWarnings: self.create_file(testdir, False) result = testdir.runpytest() result.stdout.fnmatch_lines(["*1 failed in*"]) + + +def test_warningschecker_twice(testdir): + """Issue #4617""" + + testdir.makepyfile( + """ + import pytest + import warnings + + @pytest.mark.parametrize("other", [1, 2]) + @pytest.mark.parametrize("expectation", [ + pytest.warns(DeprecationWarning, + match="Message A"), + pytest.warns(DeprecationWarning, + match="Message A"), + ]) + def test_parametrized_warnings(other, expectation): + with expectation: + warnings.warn("Message A", DeprecationWarning) + """ + ) + result = testdir.runpytest() + result.stdout.fnmatch_lines(["* 4 passed in *"]) From df3b5557d176744967e1fef42c25c765794d2a32 Mon Sep 17 00:00:00 2001 From: Victor Maryama Date: Wed, 9 Jan 2019 12:51:04 +0100 Subject: [PATCH 2/5] Reseting entered state in WarningsRecorder (fixes 4617) --- src/_pytest/recwarn.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index f39f7aee7..da408a892 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -192,6 +192,10 @@ class WarningsRecorder(warnings.catch_warnings): warnings.warn = self._saved_warn super(WarningsRecorder, self).__exit__(*exc_info) + # Built-in catch_warnings does not reset entered state so we do it + # manually here for this context manager to become reusable + self._entered = False + class WarningsChecker(WarningsRecorder): def __init__(self, expected_warning=None, match_expr=None): From fe4835c15e9164e346c885228a519b2f4fa64a70 Mon Sep 17 00:00:00 2001 From: Victor Maryama Date: Wed, 9 Jan 2019 12:55:15 +0100 Subject: [PATCH 3/5] Added changelog entry. --- changelog/4617.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/4617.bugfix.rst diff --git a/changelog/4617.bugfix.rst b/changelog/4617.bugfix.rst new file mode 100644 index 000000000..048cd60be --- /dev/null +++ b/changelog/4617.bugfix.rst @@ -0,0 +1 @@ +Fixed ``pytest.warns`` bug when context manager is reused (e.g. multiple parametrization). \ No newline at end of file From 081accb62cea86164019c3df87f448f3b846a2d1 Mon Sep 17 00:00:00 2001 From: Victor Maryama Date: Wed, 9 Jan 2019 13:11:22 +0100 Subject: [PATCH 4/5] Fixed linting in changelog. --- changelog/4617.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/4617.bugfix.rst b/changelog/4617.bugfix.rst index 048cd60be..166378efd 100644 --- a/changelog/4617.bugfix.rst +++ b/changelog/4617.bugfix.rst @@ -1 +1 @@ -Fixed ``pytest.warns`` bug when context manager is reused (e.g. multiple parametrization). \ No newline at end of file +Fixed ``pytest.warns`` bug when context manager is reused (e.g. multiple parametrization). From 7ee03e0996d61099c594cf3f7046ee2ff4d30ee1 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 9 Jan 2019 15:35:52 +0100 Subject: [PATCH 5/5] Punctuation [ci skip] --- src/_pytest/recwarn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index da408a892..a58c75d3a 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -193,7 +193,7 @@ class WarningsRecorder(warnings.catch_warnings): super(WarningsRecorder, self).__exit__(*exc_info) # Built-in catch_warnings does not reset entered state so we do it - # manually here for this context manager to become reusable + # manually here for this context manager to become reusable. self._entered = False