@@ -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,28 +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 (object ):
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
- other = Foo ()
255
230
spy = mocker .spy (foo , "bar" )
256
231
257
- with pytest .raises (Exception ) as exc_info :
258
- foo .bar (10 )
259
- 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 )
260
236
261
- foo .bar .assert_called_once_with (arg = 10 )
262
- 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
263
265
264
266
265
267
@skip_pypy
@@ -293,7 +295,7 @@ class Foo(Base):
293
295
assert other .bar (arg = 10 ) == 20
294
296
calls = [mocker .call (foo , arg = 10 ), mocker .call (other , arg = 10 )]
295
297
assert spy .call_args_list == calls
296
- assert spy .return_value == 20
298
+ assert spy .spy_return == 20
297
299
298
300
299
301
@skip_pypy
@@ -306,9 +308,9 @@ def bar(cls, arg):
306
308
spy = mocker .spy (Foo , "bar" )
307
309
assert Foo .bar (arg = 10 ) == 20
308
310
Foo .bar .assert_called_once_with (arg = 10 )
309
- assert Foo .bar .return_value == 20
311
+ assert Foo .bar .spy_return == 20
310
312
spy .assert_called_once_with (arg = 10 )
311
- assert spy .return_value == 20
313
+ assert spy .spy_return == 20
312
314
313
315
314
316
@skip_pypy
@@ -325,9 +327,9 @@ class Foo(Base):
325
327
spy = mocker .spy (Foo , "bar" )
326
328
assert Foo .bar (arg = 10 ) == 20
327
329
Foo .bar .assert_called_once_with (arg = 10 )
328
- assert Foo .bar .return_value == 20
330
+ assert Foo .bar .spy_return == 20
329
331
spy .assert_called_once_with (arg = 10 )
330
- assert spy .return_value == 20
332
+ assert spy .spy_return == 20
331
333
332
334
333
335
@skip_pypy
@@ -346,9 +348,9 @@ def bar(cls, arg):
346
348
spy = mocker .spy (Foo , "bar" )
347
349
assert Foo .bar (arg = 10 ) == 20
348
350
Foo .bar .assert_called_once_with (arg = 10 )
349
- assert Foo .bar .return_value == 20
351
+ assert Foo .bar .spy_return == 20
350
352
spy .assert_called_once_with (arg = 10 )
351
- assert spy .return_value == 20
353
+ assert spy .spy_return == 20
352
354
353
355
354
356
@skip_pypy
@@ -361,9 +363,9 @@ def bar(arg):
361
363
spy = mocker .spy (Foo , "bar" )
362
364
assert Foo .bar (arg = 10 ) == 20
363
365
Foo .bar .assert_called_once_with (arg = 10 )
364
- assert Foo .bar .return_value == 20
366
+ assert Foo .bar .spy_return == 20
365
367
spy .assert_called_once_with (arg = 10 )
366
- assert spy .return_value == 20
368
+ assert spy .spy_return == 20
367
369
368
370
369
371
@skip_pypy
@@ -380,9 +382,9 @@ class Foo(Base):
380
382
spy = mocker .spy (Foo , "bar" )
381
383
assert Foo .bar (arg = 10 ) == 20
382
384
Foo .bar .assert_called_once_with (arg = 10 )
383
- assert Foo .bar .return_value == 20
385
+ assert Foo .bar .spy_return == 20
384
386
spy .assert_called_once_with (arg = 10 )
385
- assert spy .return_value == 20
387
+ assert spy .spy_return == 20
386
388
387
389
388
390
def test_callable_like_spy (testdir , mocker ):
@@ -402,7 +404,7 @@ def __call__(self, x):
402
404
spy = mocker .spy (uut , "call_like" )
403
405
uut .call_like (10 )
404
406
spy .assert_called_once_with (10 )
405
- assert spy .return_value == 20
407
+ assert spy .spy_return == 20
406
408
407
409
408
410
@contextmanager
0 commit comments