From 3388d82c1c330422ffad0198f3995b482c187d07 Mon Sep 17 00:00:00 2001 From: Chris Gilling Date: Mon, 3 Mar 2014 10:36:59 -0800 Subject: [PATCH] Fix to work properly when @patch is used with new not equal to DEFAULT also updated test_mock to include this situation --- _pytest/python.py | 8 +++++++- testing/python/integration.py | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/_pytest/python.py b/_pytest/python.py index a882dec70..ecb316104 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1836,7 +1836,13 @@ def getfuncargnames(function, startindex=None): if startindex is None: startindex = inspect.ismethod(function) and 1 or 0 if realfunction != function: - startindex += len(getattr(function, "patchings", [])) + try: + from mock import DEFAULT + for patching in getattr(function, "patchings", []): + if not patching.attribute_name and patching.new is DEFAULT: + startindex += 1 + except ImportError: + startindex += len(getattr(function, "patchings", [])) function = realfunction argnames = inspect.getargs(py.code.getrawcode(function))[0] defaults = getattr(function, 'func_defaults', diff --git a/testing/python/integration.py b/testing/python/integration.py index 6fbf44572..e070b4398 100644 --- a/testing/python/integration.py +++ b/testing/python/integration.py @@ -124,8 +124,11 @@ class TestMockDecoration: def test_hello(self, abspath): os.path.abspath("hello") abspath.assert_any_call("hello") + def mock_basename(path): + return "mock_basename" @mock.patch("os.path.abspath") @mock.patch("os.path.normpath") + @mock.patch("os.path.basename",new=mock_basename) def test_someting(normpath, abspath, tmpdir): abspath.return_value = "this" os.path.normpath(os.path.abspath("hello"))