Handle also unittest.mock

Move handling in dedicated function to isolate its logic

--HG--
branch : mock-unittest-252
This commit is contained in:
Nicolas Delaby 2014-07-27 10:43:50 +02:00
parent 0dd378da30
commit e6ad6e02d2
1 changed files with 22 additions and 7 deletions

View File

@ -1861,6 +1861,27 @@ class FixtureDef:
return ("<FixtureDef name=%r scope=%r baseid=%r >" % return ("<FixtureDef name=%r scope=%r baseid=%r >" %
(self.argname, self.scope, self.baseid)) (self.argname, self.scope, self.baseid))
def handle_mock_module_patching(function, startindex):
"""
Special treatment when test_function is decorated
by mock.patch
"""
for candidate_module_name in ('unittest.mock', 'mock'):
# stdlib comes first
try:
mock = sys.modules[candidate_module_name]
except KeyError:
pass
else:
for patching in getattr(function, "patchings", []):
if (not patching.attribute_name
and patching.new is mock.DEFAULT):
startindex += 1
break
else:
startindex += len(getattr(function, "patchings", []))
return startindex
def getfuncargnames(function, startindex=None): def getfuncargnames(function, startindex=None):
# XXX merge with main.py's varnames # XXX merge with main.py's varnames
#assert not inspect.isclass(function) #assert not inspect.isclass(function)
@ -1870,13 +1891,7 @@ def getfuncargnames(function, startindex=None):
if startindex is None: if startindex is None:
startindex = inspect.ismethod(function) and 1 or 0 startindex = inspect.ismethod(function) and 1 or 0
if realfunction != function: if realfunction != function:
mock = sys.modules.get('mock') startindex = handle_mock_module_patching(function, startindex)
if mock is not None:
for patching in getattr(function, "patchings", []):
if not patching.attribute_name and patching.new is mock.DEFAULT:
startindex += 1
else:
startindex += len(getattr(function, "patchings", []))
function = realfunction function = realfunction
argnames = inspect.getargs(py.code.getrawcode(function))[0] argnames = inspect.getargs(py.code.getrawcode(function))[0]
defaults = getattr(function, 'func_defaults', defaults = getattr(function, 'func_defaults',