Skip to content

Commit beaa8e5

Browse files
committed
Fixes #653 use deprecated_call as context_manager
1 parent c30eafa commit beaa8e5

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Dave Hunt
2828
David Mohr
2929
Edison Gustavo Muenz
3030
Eduardo Schettino
31+
Endre Galaczi
3132
Elizaveta Shashkova
3233
Eric Hunsberger
3334
Eric Siegerman

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

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

1011
2.8.0
1112
-----------------------------

_pytest/recwarn.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ def pytest_namespace():
2828
'warns': warns}
2929

3030

31-
def deprecated_call(func, *args, **kwargs):
31+
def deprecated_call(func=None, *args, **kwargs):
3232
"""Assert that ``func(*args, **kwargs)`` triggers a DeprecationWarning.
33+
34+
This function can be used as a context manager::
35+
36+
>>> with deprecated_call():
37+
... myobject.deprecated_method()
3338
"""
39+
if not func:
40+
warnings.simplefilter('always')
41+
return WarningsChecker(expected_warning=DeprecationWarning)
42+
3443
wrec = WarningsRecorder()
3544
with wrec:
3645
warnings.simplefilter('always') # ensure all warnings are triggered

doc/en/recwarn.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,9 @@ command ``warnings.simplefilter('always')``::
114114
warnings.warn("deprecated", DeprecationWarning)
115115
assert len(recwarn) == 1
116116
assert recwarn.pop(DeprecationWarning)
117+
118+
You can also use it as a contextmanager::
119+
120+
def test_global():
121+
with pytest.deprecated_call():
122+
myobject.deprecated_method()

testing/test_recwarn.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def dep_explicit(i):
7979
filename="hello", lineno=3)
8080

8181
class TestDeprecatedCall(object):
82+
8283
def test_deprecated_call_raises(self):
8384
excinfo = pytest.raises(AssertionError,
8485
"pytest.deprecated_call(dep, 3)")
@@ -111,6 +112,16 @@ def test_deprecated_explicit_call(self):
111112
pytest.deprecated_call(dep_explicit, 0)
112113
pytest.deprecated_call(dep_explicit, 0)
113114

115+
def test_deprecated_call_as_context_manager_no_warning(self):
116+
with pytest.raises(pytest.fail.Exception) as ex:
117+
with pytest.deprecated_call():
118+
dep(1)
119+
assert str(ex.value) == "DID NOT WARN"
120+
121+
def test_deprecated_call_as_context_manager(self):
122+
with pytest.deprecated_call():
123+
dep(0)
124+
114125

115126
class TestWarns(object):
116127
def test_strings(self):

0 commit comments

Comments
 (0)