Skip to content

Commit

Permalink
FIXUP: as per Bruno's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
hunse committed Jul 28, 2015
1 parent e1da327 commit af7818e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
19 changes: 17 additions & 2 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,22 @@ def __exit__(self, *tp):
# builtin pytest.warns helper

def warns(ExpectedWarning, *args, **kwargs):
"""Assert that code raises a particular class of warning.
Specifically, the input ``ExpectedWarning`` can be a warning class or
tuple of warning classes, and the code must return that warning
(if a single class) or one of those warnings (if a tuple).
This helper produces a list of ``warnings.WarningMessage`` objects,
one for each warning raised.
This function can be used as a context manager, or any of the other ways
``pytest.raises`` can be used::
>>> with warns(RuntimeWarning):
... warnings.warn("my warning", RuntimeWarning)
"""

msg = ("exceptions must be old-style classes or"
" derived from Warning, not %s")
if isinstance(ExpectedWarning, tuple):
Expand Down Expand Up @@ -1213,7 +1229,7 @@ def __enter__(self):
def __exit__(self, type, value, traceback):
self.catcher.__exit__(type, value, traceback)
if not any(r.category in self.ExpectedWarning for r in self.record):
pytest.fail("DID NOT RAISE")
pytest.fail("DID NOT WARN")

#
# the basic pytest Function item
Expand Down Expand Up @@ -2175,4 +2191,3 @@ def get_scope_node(node, scope):
return node.session
raise ValueError("unknown scope")
return node.getparent(cls)

1 change: 0 additions & 1 deletion testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def test_raises_flip_builtin_AssertionError(self):
raise BuiltinAssertionError
""")

@pytest.mark.skipif('sys.version < "2.5"')
def test_raises_as_contextmanager(self, testdir):
testdir.makepyfile("""
from __future__ import with_statement
Expand Down
1 change: 0 additions & 1 deletion testing/python/warns.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def test_warns_tuple(self):
(RuntimeWarning, SyntaxWarning),
lambda: warnings.warn('w3', UserWarning)))

@pytest.mark.skipif('sys.version < "2.5"')
def test_warns_as_contextmanager(self, testdir):
with pytest.warns(RuntimeWarning):
warnings.warn("runtime", RuntimeWarning)
Expand Down

0 comments on commit af7818e

Please sign in to comment.