pytest.warns checks for subclass relationship rather than class equality #2166
Conversation
56528f0
to
c7ab6a2
c7ab6a2
to
6173120
Thanks @lesteve for the PR, appreciate it! I rebased your branch into the latest |
@@ -238,6 +238,16 @@ def test_record_only(self): | |||
assert str(record[0].message) == "user" | |||
assert str(record[1].message) == "runtime" | |||
|
|||
def test_record_by_subclass(self): |
nicoddemus
Dec 29, 2016
Member
Could you please also add a test which checks for warning subclasses when the user passes more than one warning to pytest.warns
? For example:
class Warn1(UserWarning): pass
class Warn2(RuntimeWarning): pass
with pytest.warns((UserWarning, RuntimeWarning)):
warnings.warn("w1", Warn1)
warnings.warn("w2", Warn2)
assert len(record) == 2
...
Could you please also add a test which checks for warning subclasses when the user passes more than one warning to pytest.warns
? For example:
class Warn1(UserWarning): pass
class Warn2(RuntimeWarning): pass
with pytest.warns((UserWarning, RuntimeWarning)):
warnings.warn("w1", Warn1)
warnings.warn("w2", Warn2)
assert len(record) == 2
...
lesteve
Dec 29, 2016
Author
Contributor
Done
Done
nicoddemus
Dec 30, 2016
Member
Thanks!
Thanks!
babbff4
to
fa418c9
Not at all, I think this is a really nice recent addition in GitHub that maintainers can push into PR branches! |
@@ -238,6 +238,16 @@ def test_record_only(self): | |||
assert str(record[0].message) == "user" | |||
assert str(record[1].message) == "runtime" | |||
|
|||
def test_record_by_subclass(self): |
nicoddemus
Dec 30, 2016
Member
Thanks!
Thanks!
_pytest/recwarn.py
Outdated
@@ -216,7 +216,8 @@ def __exit__(self, *exc_info): | |||
# only check if we're not currently handling an exception | |||
if all(a is None for a in exc_info): | |||
if self.expected_warning is not None: | |||
if not any(r.category in self.expected_warning for r in self): | |||
if not any(issubclass(r.category, exp_warning) for |
RonnyPfannschmidt
Jan 3, 2017
Member
lets use issubclass with the propper classinfo support here, no need for own loops, please check the docs
lets use issubclass with the propper classinfo support here, no need for own loops, please check the docs
lesteve
Jan 3, 2017
Author
Contributor
Good point!
Good point!
rather than class equality. This makes it more similar to pytest.raises.
Thanks a lot @lesteve! |
Great, thanks a lot! |
This was referenced Mar 6, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
This makes it more similar to pytest.raises. Fix #2151.