@@ -63,32 +63,30 @@ def test_invalid_enter_exit(self):
63
63
with rec :
64
64
pass # can't enter twice
65
65
66
- #
67
- # ============ test pytest.deprecated_call() ==============
68
- #
69
66
70
- def dep (i ):
71
- if i == 0 :
72
- py .std .warnings .warn ("is deprecated" , DeprecationWarning )
73
- return 42
67
+ class TestDeprecatedCall (object ):
68
+ """test pytest.deprecated_call()"""
74
69
75
- reg = {}
76
- def dep_explicit (i ):
77
- if i == 0 :
78
- py .std .warnings .warn_explicit ("dep_explicit" , category = DeprecationWarning ,
79
- filename = "hello" , lineno = 3 )
70
+ def dep (self , i ):
71
+ if i == 0 :
72
+ py .std .warnings .warn ("is deprecated" , DeprecationWarning )
73
+ return 42
74
+
75
+ def dep_explicit (self , i ):
76
+ if i == 0 :
77
+ py .std .warnings .warn_explicit ("dep_explicit" , category = DeprecationWarning ,
78
+ filename = "hello" , lineno = 3 )
80
79
81
- class TestDeprecatedCall (object ):
82
80
def test_deprecated_call_raises (self ):
83
- excinfo = pytest .raises (AssertionError ,
84
- " pytest.deprecated_call(dep, 3)" )
81
+ with pytest .raises (AssertionError ) as excinfo :
82
+ pytest .deprecated_call (self . dep , 3 )
85
83
assert str (excinfo ).find ("did not produce" ) != - 1
86
84
87
85
def test_deprecated_call (self ):
88
- pytest .deprecated_call (dep , 0 )
86
+ pytest .deprecated_call (self . dep , 0 )
89
87
90
88
def test_deprecated_call_ret (self ):
91
- ret = pytest .deprecated_call (dep , 0 )
89
+ ret = pytest .deprecated_call (self . dep , 0 )
92
90
assert ret == 42
93
91
94
92
def test_deprecated_call_preserves (self ):
@@ -104,25 +102,48 @@ def test_deprecated_call_preserves(self):
104
102
assert warn_explicit is py .std .warnings .warn_explicit
105
103
106
104
def test_deprecated_explicit_call_raises (self ):
107
- pytest .raises (AssertionError ,
108
- " pytest.deprecated_call(dep_explicit, 3)" )
105
+ with pytest .raises (AssertionError ):
106
+ pytest .deprecated_call (self . dep_explicit , 3 )
109
107
110
108
def test_deprecated_explicit_call (self ):
111
- pytest .deprecated_call (dep_explicit , 0 )
112
- pytest .deprecated_call (dep_explicit , 0 )
109
+ pytest .deprecated_call (self . dep_explicit , 0 )
110
+ pytest .deprecated_call (self . dep_explicit , 0 )
113
111
114
112
def test_deprecated_call_pending (self ):
115
- f = lambda : py .std .warnings .warn (PendingDeprecationWarning ("hi" ))
113
+ def f ():
114
+ py .std .warnings .warn (PendingDeprecationWarning ("hi" ))
116
115
pytest .deprecated_call (f )
117
116
118
117
def test_deprecated_call_specificity (self ):
119
118
other_warnings = [Warning , UserWarning , SyntaxWarning , RuntimeWarning ,
120
119
FutureWarning , ImportWarning , UnicodeWarning ]
121
120
for warning in other_warnings :
122
- f = lambda : py .std .warnings .warn (warning ("hi" ))
121
+ def f ():
122
+ py .std .warnings .warn (warning ("hi" ))
123
123
with pytest .raises (AssertionError ):
124
124
pytest .deprecated_call (f )
125
125
126
+ def test_deprecated_function_already_called (self , testdir ):
127
+ """deprecated_call should be able to catch a call to a deprecated
128
+ function even if that function has already been called in the same
129
+ module. See #1190.
130
+ """
131
+ testdir .makepyfile ("""
132
+ import warnings
133
+ import pytest
134
+
135
+ def deprecated_function():
136
+ warnings.warn("deprecated", DeprecationWarning)
137
+
138
+ def test_one():
139
+ deprecated_function()
140
+
141
+ def test_two():
142
+ pytest.deprecated_call(deprecated_function)
143
+ """ )
144
+ result = testdir .runpytest ()
145
+ result .stdout .fnmatch_lines ('*=== 2 passed in *===' )
146
+
126
147
127
148
class TestWarns (object ):
128
149
def test_strings (self ):
0 commit comments