Skip to content

Commit 56528f0

Browse files
committed
pytest.warns checks for subclass relationship
rather than class equality. This makes it more similar to pytest.raises.
1 parent 669332b commit 56528f0

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ New Features
88
* Added an ini option ``doctest_encoding`` to specify which encoding to use for doctest files.
99
Thanks `@wheerd`_ for the PR (`#2101`_).
1010

11-
*
11+
* ``pytest.warns`` now checks for subclass relationship rather than
12+
class equality. Thanks `@lesteve`_ for the PR (`#2163`_)
1213

1314

1415
Changes
@@ -40,13 +41,14 @@ Changes
4041
.. _@fushi: https://github.com/fushi
4142
.. _@mattduck: https://github.com/mattduck
4243
.. _@wheerd: https://github.com/wheerd
44+
.. _@lesteve: https://github.com/lesteve
4345

4446
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
4547
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
4648
.. _#1952: https://github.com/pytest-dev/pytest/pull/1952
4749
.. _#2013: https://github.com/pytest-dev/pytest/issues/2013
4850
.. _#2101: https://github.com/pytest-dev/pytest/pull/2101
49-
51+
.. _#2164: https://github.com/pytest-dev/pytest/pull/2164
5052

5153
3.0.5.dev0
5254
==========

_pytest/recwarn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def __exit__(self, *exc_info):
216216
# only check if we're not currently handling an exception
217217
if all(a is None for a in exc_info):
218218
if self.expected_warning is not None:
219-
if not any(r.category in self.expected_warning for r in self):
219+
if not any(issubclass(r.category, exp_warning) for
220+
exp_warning in self.expected_warning for r in self):
220221
__tracebackhide__ = True
221222
pytest.fail("DID NOT WARN")

testing/test_recwarn.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ def test_record_only(self):
215215
assert str(record[0].message) == "user"
216216
assert str(record[1].message) == "runtime"
217217

218+
def test_record_by_subclass(self):
219+
with pytest.warns(Warning) as record:
220+
warnings.warn("user", UserWarning)
221+
warnings.warn("runtime", RuntimeWarning)
222+
223+
assert len(record) == 2
224+
assert str(record[0].message) == "user"
225+
assert str(record[1].message) == "runtime"
226+
227+
218228
def test_double_test(self, testdir):
219229
"""If a test is run again, the warning should still be raised"""
220230
testdir.makepyfile('''

0 commit comments

Comments
 (0)