[Pytest-commit] commit/pytest: hpk42: add changelog entry and refactor unittest.mock.patch fix a bit

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Mon Jul 28 10:44:12 CEST 2014


1 new commit in pytest:

https://bitbucket.org/hpk42/pytest/commits/40c9e9432ff6/
Changeset:   40c9e9432ff6
User:        hpk42
Date:        2014-07-28 10:34:01
Summary:     add changelog entry and refactor unittest.mock.patch fix a bit
Affected #:  3 files

diff -r 7b861c1de21df974bf015ee9b254bf266d73d300 -r 40c9e9432ff68474553a702b718262ee8e2fa1ac AUTHORS
--- a/AUTHORS
+++ b/AUTHORS
@@ -44,3 +44,4 @@
 Andy Freeland
 Trevor Bekolay
 David Mohr
+Nicolas Delaby

diff -r 7b861c1de21df974bf015ee9b254bf266d73d300 -r 40c9e9432ff68474553a702b718262ee8e2fa1ac CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -6,6 +6,9 @@
   or a tuple of exception classes.  Thanks David Mohr for the complete
   PR.
 
+- fix integration of pytest with unittest.mock.patch decorator when
+  it uses the "new" argument.  Thanks Nicolas Delaby for test and PR.
+
 
 2.6
 -----------------------------------

diff -r 7b861c1de21df974bf015ee9b254bf266d73d300 -r 40c9e9432ff68474553a702b718262ee8e2fa1ac _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -1861,28 +1861,17 @@
         return ("<FixtureDef name=%r scope=%r baseid=%r >" %
                 (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 ('mock', 'unittest.mock'):
-        # stdlib comes last, because mock might be also installed
-        # as a third party with upgraded version compare to
-        # unittest.mock
-        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 num_mock_patch_args(function):
+    """ return number of arguments used up by mock arguments (if any) """
+    patchings = getattr(function, "patchings", None)
+    if not patchings:
+        return 0
+    mock = sys.modules.get("mock", sys.modules.get("unittest.mock", None))
+    if mock is not None:
+        return len([p for p in patchings
+                        if not p.attribute_name and p.new is mock.DEFAULT])
+    return len(patchings)
+
 
 def getfuncargnames(function, startindex=None):
     # XXX merge with main.py's varnames
@@ -1893,7 +1882,7 @@
     if startindex is None:
         startindex = inspect.ismethod(function) and 1 or 0
     if realfunction != function:
-        startindex = handle_mock_module_patching(function, startindex)
+        startindex += num_mock_patch_args(function)
         function = realfunction
     argnames = inspect.getargs(py.code.getrawcode(function))[0]
     defaults = getattr(function, 'func_defaults',

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the pytest-commit mailing list