Skip to content
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

pytest.warns checks for subclass relationship rather than class equality #2166

Merged
merged 6 commits into from
Jan 3, 2017
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
7 changes: 4 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ New Features

* pytest now warns when a callable ids raises in a parametrized test. Thanks `@fogo`_ for the PR.

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


Changes
Expand Down Expand Up @@ -42,14 +43,15 @@ Changes
.. _@mattduck: https://github.com/mattduck
.. _@wheerd: https://github.com/wheerd
.. _@fogo: https://github.com/fogo
.. _@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
.. _#2007: https://github.com/pytest-dev/pytest/issues/2007
.. _#2013: https://github.com/pytest-dev/pytest/issues/2013
.. _#2101: https://github.com/pytest-dev/pytest/pull/2101

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

3.0.6.dev0 (unreleased)
=======================
Expand All @@ -69,7 +71,6 @@ Changes

*

.. _@lesteve: https://github.com/lesteve
.. _@malinoff: https://github.com/malinoff
.. _@pelme: https://github.com/pelme

Expand Down
3 changes: 2 additions & 1 deletion _pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, self.expected_warning)
for r in self):
__tracebackhide__ = True
pytest.fail("DID NOT WARN. No warnings of type {0} was emitted. "
"The list of emitted warnings is: {1}.".format(
Expand Down
22 changes: 22 additions & 0 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,28 @@ def test_record_only(self):
assert str(record[0].message) == "user"
assert str(record[1].message) == "runtime"

def test_record_by_subclass(self):
Copy link
Member

Choose a reason for hiding this comment

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

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
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

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"

class MyUserWarning(UserWarning): pass

class MyRuntimeWarning(RuntimeWarning): pass

with pytest.warns((UserWarning, RuntimeWarning)) as record:
warnings.warn("user", MyUserWarning)
warnings.warn("runtime", MyRuntimeWarning)

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