Skip to content

Commit

Permalink
Fixes #653 use deprecated_call as context_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
chiller committed Sep 21, 2015
1 parent c30eafa commit beaa8e5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Dave Hunt
David Mohr
Edison Gustavo Muenz
Eduardo Schettino
Endre Galaczi
Elizaveta Shashkova
Eric Hunsberger
Eric Siegerman
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Fix issue #411: Add __eq__ method to assertion comparison example.
Thanks Ben Webb.
- Fix issue #653: deprecated_call can be used as context manager.

2.8.0
-----------------------------
Expand Down
11 changes: 10 additions & 1 deletion _pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ def pytest_namespace():
'warns': warns}


def deprecated_call(func, *args, **kwargs):
def deprecated_call(func=None, *args, **kwargs):
"""Assert that ``func(*args, **kwargs)`` triggers a DeprecationWarning.
This function can be used as a context manager::
>>> with deprecated_call():
... myobject.deprecated_method()
"""
if not func:
warnings.simplefilter('always')
return WarningsChecker(expected_warning=DeprecationWarning)

wrec = WarningsRecorder()
with wrec:
warnings.simplefilter('always') # ensure all warnings are triggered
Expand Down
6 changes: 6 additions & 0 deletions doc/en/recwarn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,9 @@ command ``warnings.simplefilter('always')``::
warnings.warn("deprecated", DeprecationWarning)
assert len(recwarn) == 1
assert recwarn.pop(DeprecationWarning)

You can also use it as a contextmanager::

def test_global():
with pytest.deprecated_call():
myobject.deprecated_method()
11 changes: 11 additions & 0 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def dep_explicit(i):
filename="hello", lineno=3)

class TestDeprecatedCall(object):

def test_deprecated_call_raises(self):
excinfo = pytest.raises(AssertionError,
"pytest.deprecated_call(dep, 3)")
Expand Down Expand Up @@ -111,6 +112,16 @@ def test_deprecated_explicit_call(self):
pytest.deprecated_call(dep_explicit, 0)
pytest.deprecated_call(dep_explicit, 0)

def test_deprecated_call_as_context_manager_no_warning(self):
with pytest.raises(pytest.fail.Exception) as ex:
with pytest.deprecated_call():
dep(1)
assert str(ex.value) == "DID NOT WARN"

def test_deprecated_call_as_context_manager(self):
with pytest.deprecated_call():
dep(0)


class TestWarns(object):
def test_strings(self):
Expand Down

0 comments on commit beaa8e5

Please sign in to comment.