Merge pull request #4620 from Sup3rGeo/bugfix/warningschecker-twice

Bugfix/warningschecker twice
This commit is contained in:
Daniel Hahler 2019-01-09 15:36:29 +01:00 committed by GitHub
commit f3b6425324
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1 @@
Fixed ``pytest.warns`` bug when context manager is reused (e.g. multiple parametrization).

View File

@ -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):

View File

@ -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 *"])