Merge pull request #4620 from Sup3rGeo/bugfix/warningschecker-twice
Bugfix/warningschecker twice
This commit is contained in:
commit
f3b6425324
|
@ -0,0 +1 @@
|
||||||
|
Fixed ``pytest.warns`` bug when context manager is reused (e.g. multiple parametrization).
|
|
@ -192,6 +192,10 @@ class WarningsRecorder(warnings.catch_warnings):
|
||||||
warnings.warn = self._saved_warn
|
warnings.warn = self._saved_warn
|
||||||
super(WarningsRecorder, self).__exit__(*exc_info)
|
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):
|
class WarningsChecker(WarningsRecorder):
|
||||||
def __init__(self, expected_warning=None, match_expr=None):
|
def __init__(self, expected_warning=None, match_expr=None):
|
||||||
|
|
|
@ -683,3 +683,27 @@ class TestAssertionWarnings:
|
||||||
self.create_file(testdir, False)
|
self.create_file(testdir, False)
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
result.stdout.fnmatch_lines(["*1 failed in*"])
|
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 *"])
|
||||||
|
|
Loading…
Reference in New Issue