@@ -132,28 +132,6 @@ def test_mock_patch_dict_resetall(mocker):
132
132
assert x == {"new" : 10 }
133
133
134
134
135
- def test_deprecated_mock (testdir ):
136
- """
137
- Use backward-compatibility-only mock fixture to ensure complete coverage.
138
- """
139
- p1 = testdir .makepyfile (
140
- """
141
- import os
142
-
143
- def test(mock, tmpdir):
144
- mock.patch("os.listdir", return_value=["mocked"])
145
- assert os.listdir(str(tmpdir)) == ["mocked"]
146
- mock.stopall()
147
- assert os.listdir(str(tmpdir)) == []
148
- """
149
- )
150
- result = testdir .runpytest (str (p1 ))
151
- result .stdout .fnmatch_lines (
152
- ['*DeprecationWarning: "mock" fixture has been deprecated, use "mocker"*' ]
153
- )
154
- assert result .ret == 0
155
-
156
-
157
135
@pytest .mark .parametrize (
158
136
"name" ,
159
137
[
@@ -238,27 +216,52 @@ def bar(self, arg):
238
216
assert foo .bar (arg = 10 ) == 20
239
217
assert other .bar (arg = 10 ) == 20
240
218
foo .bar .assert_called_once_with (arg = 10 )
241
- assert foo .bar .return_value == 20
219
+ assert foo .bar .spy_return == 20
242
220
spy .assert_called_once_with (arg = 10 )
243
- assert spy .return_value == 20
221
+ assert spy .spy_return == 20
244
222
245
223
246
224
def test_instance_method_spy_exception (mocker ):
247
- excepted_message = "foo"
248
-
249
225
class Foo :
250
226
def bar (self , arg ):
251
- raise Exception (excepted_message )
227
+ raise Exception ("Error with {}" . format ( arg ) )
252
228
253
229
foo = Foo ()
254
230
spy = mocker .spy (foo , "bar" )
255
231
256
- with pytest .raises (Exception ) as exc_info :
257
- foo .bar (10 )
258
- assert str (exc_info .value ) == excepted_message
232
+ expected_calls = []
233
+ for i , v in enumerate ([10 , 20 ]):
234
+ with pytest .raises (Exception , match = "Error with {}" .format (v )) as exc_info :
235
+ foo .bar (arg = v )
259
236
260
- foo .bar .assert_called_once_with (arg = 10 )
261
- assert spy .side_effect == exc_info .value
237
+ expected_calls .append (mocker .call (arg = v ))
238
+ assert foo .bar .call_args_list == expected_calls
239
+ assert str (spy .spy_exception ) == "Error with {}" .format (v )
240
+
241
+
242
+ def test_spy_reset (mocker ):
243
+ class Foo (object ):
244
+ def bar (self , x ):
245
+ if x == 0 :
246
+ raise ValueError ("invalid x" )
247
+ return x * 3
248
+
249
+ spy = mocker .spy (Foo , "bar" )
250
+ assert spy .spy_return is None
251
+ assert spy .spy_exception is None
252
+
253
+ Foo ().bar (10 )
254
+ assert spy .spy_return == 30
255
+ assert spy .spy_exception is None
256
+
257
+ with pytest .raises (ValueError ):
258
+ Foo ().bar (0 )
259
+ assert spy .spy_return is None
260
+ assert str (spy .spy_exception ) == "invalid x"
261
+
262
+ Foo ().bar (15 )
263
+ assert spy .spy_return == 45
264
+ assert spy .spy_exception is None
262
265
263
266
264
267
@skip_pypy
@@ -292,7 +295,7 @@ class Foo(Base):
292
295
assert other .bar (arg = 10 ) == 20
293
296
calls = [mocker .call (foo , arg = 10 ), mocker .call (other , arg = 10 )]
294
297
assert spy .call_args_list == calls
295
- assert spy .return_value == 20
298
+ assert spy .spy_return == 20
296
299
297
300
298
301
@skip_pypy
@@ -305,9 +308,9 @@ def bar(cls, arg):
305
308
spy = mocker .spy (Foo , "bar" )
306
309
assert Foo .bar (arg = 10 ) == 20
307
310
Foo .bar .assert_called_once_with (arg = 10 )
308
- assert Foo .bar .return_value == 20
311
+ assert Foo .bar .spy_return == 20
309
312
spy .assert_called_once_with (arg = 10 )
310
- assert spy .return_value == 20
313
+ assert spy .spy_return == 20
311
314
312
315
313
316
@skip_pypy
@@ -323,9 +326,9 @@ class Foo(Base):
323
326
spy = mocker .spy (Foo , "bar" )
324
327
assert Foo .bar (arg = 10 ) == 20
325
328
Foo .bar .assert_called_once_with (arg = 10 )
326
- assert Foo .bar .return_value == 20
329
+ assert Foo .bar .spy_return == 20
327
330
spy .assert_called_once_with (arg = 10 )
328
- assert spy .return_value == 20
331
+ assert spy .spy_return == 20
329
332
330
333
331
334
@skip_pypy
@@ -344,9 +347,9 @@ def bar(cls, arg):
344
347
spy = mocker .spy (Foo , "bar" )
345
348
assert Foo .bar (arg = 10 ) == 20
346
349
Foo .bar .assert_called_once_with (arg = 10 )
347
- assert Foo .bar .return_value == 20
350
+ assert Foo .bar .spy_return == 20
348
351
spy .assert_called_once_with (arg = 10 )
349
- assert spy .return_value == 20
352
+ assert spy .spy_return == 20
350
353
351
354
352
355
@skip_pypy
@@ -359,9 +362,9 @@ def bar(arg):
359
362
spy = mocker .spy (Foo , "bar" )
360
363
assert Foo .bar (arg = 10 ) == 20
361
364
Foo .bar .assert_called_once_with (arg = 10 )
362
- assert Foo .bar .return_value == 20
365
+ assert Foo .bar .spy_return == 20
363
366
spy .assert_called_once_with (arg = 10 )
364
- assert spy .return_value == 20
367
+ assert spy .spy_return == 20
365
368
366
369
367
370
@skip_pypy
@@ -377,9 +380,9 @@ class Foo(Base):
377
380
spy = mocker .spy (Foo , "bar" )
378
381
assert Foo .bar (arg = 10 ) == 20
379
382
Foo .bar .assert_called_once_with (arg = 10 )
380
- assert Foo .bar .return_value == 20
383
+ assert Foo .bar .spy_return == 20
381
384
spy .assert_called_once_with (arg = 10 )
382
- assert spy .return_value == 20
385
+ assert spy .spy_return == 20
383
386
384
387
385
388
def test_callable_like_spy (testdir , mocker ):
@@ -399,7 +402,7 @@ def __call__(self, x):
399
402
spy = mocker .spy (uut , "call_like" )
400
403
uut .call_like (10 )
401
404
spy .assert_called_once_with (10 )
402
- assert spy .return_value == 20
405
+ assert spy .spy_return == 20
403
406
404
407
405
408
@contextmanager
0 commit comments