@@ -22,13 +22,17 @@ def _get_mock_module(config):
22
22
"unittest.mock" for Python 3, but the user can force to always use "mock" on Python 3 using
23
23
the mock_use_standalone_module ini option.
24
24
"""
25
- if not hasattr (_get_mock_module , '_module' ):
26
- use_standalone_module = parse_ini_boolean (config .getini ('mock_use_standalone_module' ))
25
+ if not hasattr (_get_mock_module , "_module" ):
26
+ use_standalone_module = parse_ini_boolean (
27
+ config .getini ("mock_use_standalone_module" )
28
+ )
27
29
if sys .version_info [0 ] == 2 or use_standalone_module :
28
30
import mock
31
+
29
32
_get_mock_module ._module = mock
30
33
else :
31
34
import unittest .mock
35
+
32
36
_get_mock_module ._module = unittest .mock
33
37
34
38
return _get_mock_module ._module
@@ -100,8 +104,7 @@ def spy(self, obj, name):
100
104
if isinstance (value , (classmethod , staticmethod )):
101
105
autospec = False
102
106
103
- result = self .patch .object (obj , name , side_effect = method ,
104
- autospec = autospec )
107
+ result = self .patch .object (obj , name , side_effect = method , autospec = autospec )
105
108
return result
106
109
107
110
def stub (self , name = None ):
@@ -134,7 +137,7 @@ def _start_patch(self, mock_func, *args, **kwargs):
134
137
p = mock_func (* args , ** kwargs )
135
138
mocked = p .start ()
136
139
self ._patches .append (p )
137
- if hasattr (mocked , ' reset_mock' ):
140
+ if hasattr (mocked , " reset_mock" ):
138
141
self ._mocks .append (mocked )
139
142
return mocked
140
143
@@ -144,8 +147,7 @@ def object(self, *args, **kwargs):
144
147
145
148
def multiple (self , * args , ** kwargs ):
146
149
"""API to mock.patch.multiple"""
147
- return self ._start_patch (self .mock_module .patch .multiple , * args ,
148
- ** kwargs )
150
+ return self ._start_patch (self .mock_module .patch .multiple , * args , ** kwargs )
149
151
150
152
def dict (self , * args , ** kwargs ):
151
153
"""API to mock.patch.dict"""
@@ -173,8 +175,10 @@ def mock(mocker):
173
175
Same as "mocker", but kept only for backward compatibility.
174
176
"""
175
177
import warnings
176
- warnings .warn ('"mock" fixture has been deprecated, use "mocker" instead' ,
177
- DeprecationWarning )
178
+
179
+ warnings .warn (
180
+ '"mock" fixture has been deprecated, use "mocker" instead' , DeprecationWarning
181
+ )
178
182
return mocker
179
183
180
184
@@ -188,67 +192,60 @@ def assert_wrapper(__wrapped_mock_method__, *args, **kwargs):
188
192
__wrapped_mock_method__ (* args , ** kwargs )
189
193
return
190
194
except AssertionError as e :
191
- if getattr (e , ' _mock_introspection_applied' , 0 ):
195
+ if getattr (e , " _mock_introspection_applied" , 0 ):
192
196
msg = text_type (e )
193
197
else :
194
198
__mock_self = args [0 ]
195
199
msg = text_type (e )
196
200
if __mock_self .call_args is not None :
197
201
actual_args , actual_kwargs = __mock_self .call_args
198
- msg += ' \n \n pytest introspection follows:\n '
202
+ msg += " \n \n pytest introspection follows:\n "
199
203
try :
200
204
assert actual_args == args [1 :]
201
205
except AssertionError as e :
202
- msg += ' \n Args:\n ' + text_type (e )
206
+ msg += " \n Args:\n " + text_type (e )
203
207
try :
204
208
assert actual_kwargs == kwargs
205
209
except AssertionError as e :
206
- msg += ' \n Kwargs:\n ' + text_type (e )
210
+ msg += " \n Kwargs:\n " + text_type (e )
207
211
e = AssertionError (msg )
208
212
e ._mock_introspection_applied = True
209
213
raise e
210
214
211
215
212
216
def wrap_assert_not_called (* args , ** kwargs ):
213
217
__tracebackhide__ = True
214
- assert_wrapper (_mock_module_originals ["assert_not_called" ],
215
- * args , ** kwargs )
218
+ assert_wrapper (_mock_module_originals ["assert_not_called" ], * args , ** kwargs )
216
219
217
220
218
221
def wrap_assert_called_with (* args , ** kwargs ):
219
222
__tracebackhide__ = True
220
- assert_wrapper (_mock_module_originals ["assert_called_with" ],
221
- * args , ** kwargs )
223
+ assert_wrapper (_mock_module_originals ["assert_called_with" ], * args , ** kwargs )
222
224
223
225
224
226
def wrap_assert_called_once (* args , ** kwargs ):
225
227
__tracebackhide__ = True
226
- assert_wrapper (_mock_module_originals ["assert_called_once" ],
227
- * args , ** kwargs )
228
+ assert_wrapper (_mock_module_originals ["assert_called_once" ], * args , ** kwargs )
228
229
229
230
230
231
def wrap_assert_called_once_with (* args , ** kwargs ):
231
232
__tracebackhide__ = True
232
- assert_wrapper (_mock_module_originals ["assert_called_once_with" ],
233
- * args , ** kwargs )
233
+ assert_wrapper (_mock_module_originals ["assert_called_once_with" ], * args , ** kwargs )
234
234
235
235
236
236
def wrap_assert_has_calls (* args , ** kwargs ):
237
237
__tracebackhide__ = True
238
- assert_wrapper (_mock_module_originals ["assert_has_calls" ],
239
- * args , ** kwargs )
238
+ assert_wrapper (_mock_module_originals ["assert_has_calls" ], * args , ** kwargs )
240
239
241
240
242
241
def wrap_assert_any_call (* args , ** kwargs ):
243
242
__tracebackhide__ = True
244
- assert_wrapper (_mock_module_originals ["assert_any_call" ],
245
- * args , ** kwargs )
243
+ assert_wrapper (_mock_module_originals ["assert_any_call" ], * args , ** kwargs )
246
244
247
245
248
246
def wrap_assert_called (* args , ** kwargs ):
249
247
__tracebackhide__ = True
250
- assert_wrapper (_mock_module_originals ["assert_called" ],
251
- * args , ** kwargs )
248
+ assert_wrapper (_mock_module_originals ["assert_called" ], * args , ** kwargs )
252
249
253
250
254
251
def wrap_assert_methods (config ):
@@ -263,26 +260,25 @@ def wrap_assert_methods(config):
263
260
mock_module = _get_mock_module (config )
264
261
265
262
wrappers = {
266
- ' assert_called' : wrap_assert_called ,
267
- ' assert_called_once' : wrap_assert_called_once ,
268
- ' assert_called_with' : wrap_assert_called_with ,
269
- ' assert_called_once_with' : wrap_assert_called_once_with ,
270
- ' assert_any_call' : wrap_assert_any_call ,
271
- ' assert_has_calls' : wrap_assert_has_calls ,
272
- ' assert_not_called' : wrap_assert_not_called ,
263
+ " assert_called" : wrap_assert_called ,
264
+ " assert_called_once" : wrap_assert_called_once ,
265
+ " assert_called_with" : wrap_assert_called_with ,
266
+ " assert_called_once_with" : wrap_assert_called_once_with ,
267
+ " assert_any_call" : wrap_assert_any_call ,
268
+ " assert_has_calls" : wrap_assert_has_calls ,
269
+ " assert_not_called" : wrap_assert_not_called ,
273
270
}
274
271
for method , wrapper in wrappers .items ():
275
272
try :
276
273
original = getattr (mock_module .NonCallableMock , method )
277
274
except AttributeError : # pragma: no cover
278
275
continue
279
276
_mock_module_originals [method ] = original
280
- patcher = mock_module .patch .object (
281
- mock_module .NonCallableMock , method , wrapper )
277
+ patcher = mock_module .patch .object (mock_module .NonCallableMock , method , wrapper )
282
278
patcher .start ()
283
279
_mock_module_patches .append (patcher )
284
280
285
- if hasattr (config , ' add_cleanup' ):
281
+ if hasattr (config , " add_cleanup" ):
286
282
add_cleanup = config .add_cleanup
287
283
else :
288
284
# pytest 2.7 compatibility
@@ -298,26 +294,33 @@ def unwrap_assert_methods():
298
294
299
295
300
296
def pytest_addoption (parser ):
301
- parser .addini ('mock_traceback_monkeypatch' ,
302
- 'Monkeypatch the mock library to improve reporting of the '
303
- 'assert_called_... methods' ,
304
- default = True )
305
- parser .addini ('mock_use_standalone_module' ,
306
- 'Use standalone "mock" (from PyPI) instead of builtin "unittest.mock" '
307
- 'on Python 3' ,
308
- default = False )
297
+ parser .addini (
298
+ "mock_traceback_monkeypatch" ,
299
+ "Monkeypatch the mock library to improve reporting of the "
300
+ "assert_called_... methods" ,
301
+ default = True ,
302
+ )
303
+ parser .addini (
304
+ "mock_use_standalone_module" ,
305
+ 'Use standalone "mock" (from PyPI) instead of builtin "unittest.mock" '
306
+ "on Python 3" ,
307
+ default = False ,
308
+ )
309
309
310
310
311
311
def parse_ini_boolean (value ):
312
312
if value in (True , False ):
313
313
return value
314
314
try :
315
- return {' true' : True , ' false' : False }[value .lower ()]
315
+ return {" true" : True , " false" : False }[value .lower ()]
316
316
except KeyError :
317
- raise ValueError (' unknown string for bool: %r' % value )
317
+ raise ValueError (" unknown string for bool: %r" % value )
318
318
319
319
320
320
def pytest_configure (config ):
321
- tb = config .getoption ('--tb' )
322
- if parse_ini_boolean (config .getini ('mock_traceback_monkeypatch' )) and tb != 'native' :
321
+ tb = config .getoption ("--tb" )
322
+ if (
323
+ parse_ini_boolean (config .getini ("mock_traceback_monkeypatch" ))
324
+ and tb != "native"
325
+ ):
323
326
wrap_assert_methods (config )
0 commit comments