Skip to content

Commit

Permalink
pytest.warns checks for subclass relationship
Browse files Browse the repository at this point in the history
rather than class equality. This makes it more similar to
pytest.raises.
  • Loading branch information
lesteve committed Dec 28, 2016
1 parent 669332b commit 56528f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ New Features
* Added an ini option ``doctest_encoding`` to specify which encoding to use for doctest files.
Thanks `@wheerd`_ for the PR (`#2101`_).

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


Changes
Expand Down Expand Up @@ -40,13 +41,14 @@ Changes
.. _@fushi: https://github.com/fushi
.. _@mattduck: https://github.com/mattduck
.. _@wheerd: https://github.com/wheerd
.. _@lesteve: https://github.com/lesteve

.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
.. _#1952: https://github.com/pytest-dev/pytest/pull/1952
.. _#2013: https://github.com/pytest-dev/pytest/issues/2013
.. _#2101: https://github.com/pytest-dev/pytest/pull/2101

.. _#2164: https://github.com/pytest-dev/pytest/pull/2164

3.0.5.dev0
==========
Expand Down
3 changes: 2 additions & 1 deletion _pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ 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
exp_warning in self.expected_warning for r in self):
__tracebackhide__ = True
pytest.fail("DID NOT WARN")
10 changes: 10 additions & 0 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ def test_record_only(self):
assert str(record[0].message) == "user"
assert str(record[1].message) == "runtime"

def test_record_by_subclass(self):
with pytest.warns(Warning) as record:
warnings.warn("user", UserWarning)
warnings.warn("runtime", RuntimeWarning)

assert len(record) == 2
assert str(record[0].message) == "user"
assert str(record[1].message) == "runtime"


def test_double_test(self, testdir):
"""If a test is run again, the warning should still be raised"""
testdir.makepyfile('''
Expand Down

0 comments on commit 56528f0

Please sign in to comment.