File tree 5 files changed +29
-1
lines changed
5 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ Dave Hunt
28
28
David Mohr
29
29
Edison Gustavo Muenz
30
30
Eduardo Schettino
31
+ Endre Galaczi
31
32
Elizaveta Shashkova
32
33
Eric Hunsberger
33
34
Eric Siegerman
Original file line number Diff line number Diff line change 6
6
7
7
- Fix issue #411: Add __eq__ method to assertion comparison example.
8
8
Thanks Ben Webb.
9
+ - Fix issue #653: deprecated_call can be used as context manager.
9
10
10
11
2.8.0
11
12
-----------------------------
Original file line number Diff line number Diff line change @@ -28,9 +28,18 @@ def pytest_namespace():
28
28
'warns' : warns }
29
29
30
30
31
- def deprecated_call (func , * args , ** kwargs ):
31
+ def deprecated_call (func = None , * args , ** kwargs ):
32
32
"""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()
33
38
"""
39
+ if not func :
40
+ warnings .simplefilter ('always' )
41
+ return WarningsChecker (expected_warning = DeprecationWarning )
42
+
34
43
wrec = WarningsRecorder ()
35
44
with wrec :
36
45
warnings .simplefilter ('always' ) # ensure all warnings are triggered
Original file line number Diff line number Diff line change @@ -114,3 +114,9 @@ command ``warnings.simplefilter('always')``::
114
114
warnings.warn("deprecated", DeprecationWarning)
115
115
assert len(recwarn) == 1
116
116
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()
Original file line number Diff line number Diff line change @@ -79,6 +79,7 @@ def dep_explicit(i):
79
79
filename = "hello" , lineno = 3 )
80
80
81
81
class TestDeprecatedCall (object ):
82
+
82
83
def test_deprecated_call_raises (self ):
83
84
excinfo = pytest .raises (AssertionError ,
84
85
"pytest.deprecated_call(dep, 3)" )
@@ -111,6 +112,16 @@ def test_deprecated_explicit_call(self):
111
112
pytest .deprecated_call (dep_explicit , 0 )
112
113
pytest .deprecated_call (dep_explicit , 0 )
113
114
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
+
114
125
115
126
class TestWarns (object ):
116
127
def test_strings (self ):
You can’t perform that action at this time.
0 commit comments