Skip to content

Bugfix/warningschecker twice #4620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4617.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``pytest.warns`` bug when context manager is reused (e.g. multiple parametrization).
4 changes: 4 additions & 0 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def __exit__(self, *exc_info):
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):
Expand Down
24 changes: 24 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,27 @@ def test_false_function_no_warn(self, testdir):
self.create_file(testdir, False)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 failed in*"])


def test_warningschecker_twice(testdir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go for a simple unittest here:

def test_warningschecker_twice():
    expectation = pytest.warns(UserWarning)
    with expectation:
        warnings.warn("Message A", UserWarning)
    with expectation:
        warnings.warn("Message B", UserWarning)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoddemus
Makes sense, sorry for merging it too early.
Will you create a followup?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, way simpler! But now it should be another PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we can't change this one anymore. Can you do it @Sup3rGeo? If you don't have the time, I will get to it later. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: #4622

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