Skip to content

Commit f3b6425

Browse files
authored
Merge pull request #4620 from Sup3rGeo/bugfix/warningschecker-twice
Bugfix/warningschecker twice
2 parents a4c426b + 7ee03e0 commit f3b6425

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

changelog/4617.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed ``pytest.warns`` bug when context manager is reused (e.g. multiple parametrization).

src/_pytest/recwarn.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ def __exit__(self, *exc_info):
192192
warnings.warn = self._saved_warn
193193
super(WarningsRecorder, self).__exit__(*exc_info)
194194

195+
# Built-in catch_warnings does not reset entered state so we do it
196+
# manually here for this context manager to become reusable.
197+
self._entered = False
198+
195199

196200
class WarningsChecker(WarningsRecorder):
197201
def __init__(self, expected_warning=None, match_expr=None):

testing/test_warnings.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,27 @@ def test_false_function_no_warn(self, testdir):
683683
self.create_file(testdir, False)
684684
result = testdir.runpytest()
685685
result.stdout.fnmatch_lines(["*1 failed in*"])
686+
687+
688+
def test_warningschecker_twice(testdir):
689+
"""Issue #4617"""
690+
691+
testdir.makepyfile(
692+
"""
693+
import pytest
694+
import warnings
695+
696+
@pytest.mark.parametrize("other", [1, 2])
697+
@pytest.mark.parametrize("expectation", [
698+
pytest.warns(DeprecationWarning,
699+
match="Message A"),
700+
pytest.warns(DeprecationWarning,
701+
match="Message A"),
702+
])
703+
def test_parametrized_warnings(other, expectation):
704+
with expectation:
705+
warnings.warn("Message A", DeprecationWarning)
706+
"""
707+
)
708+
result = testdir.runpytest()
709+
result.stdout.fnmatch_lines(["* 4 passed in *"])

0 commit comments

Comments
 (0)