0

I'd like to patch a function which is called inside a decorator with the python mock library. I fail to be able to patch it though, resulting in the original function being called. It probably has to do with namespaces but it's not clear to me what I should use as function name, the first argument to the patch decorator.

What I have is the following:

def is_always_running_on_Windows_64bit_mode():
    return True

class ToolsTests(BaseTestCase):
    @patch('tools.is_running_on_Windows_64bit_mode', is_always_running_on_Windows_64bit_mode)
    @parameters()
    def test_benchmarks_in_x86_not_x64_folder(self):
        ... 
        #do some testing where the test decorator parameters should believe we're in 64 bit mode 
        ...

BaseTestCase derives from unittest.TestCase and is defined in some other module. In that same module I have the decorator itself:

def parameters(*parameters):
    def decorator(method, parameters=parameters):
        if tools.is_running_on_Windows_64bit_mode():
            regressions_folder = "Regressions_x64"
        else:
            regressions_folder = "Regressions"
        # etc etc
    return decorator

The problem is that tools.is_running_on_Windows_64bit_mode is called, not my own is_always_running_on_Windows_64bit_mode.

Searching for answers is a tad difficult as it's about decorating a decorator, if there's one out there already, I'm sorry for creating a redundant question.

ikku100
  • 809
  • 1
  • 7
  • 16
  • This isn't really about decorators; that it is called inside a decorator is not an issue here. What is `tools`, is that a top-level module or something in a package? – Martijn Pieters Apr 22 '16 at 13:54
  • Hm, interesting. But to answer your question: it's a module in a different package. – ikku100 Apr 22 '16 at 16:42
  • So you'll need to use the full package name to patch. `package.tools. is_running_on_Windows_64bit_mode`. – Martijn Pieters Apr 22 '16 at 17:14
  • `def is_always_running_on_Windows_64bit_mode:` should be `def is_always_running_on_Windows_64bit_mode():`. `Class ToolsTests(BaseTestCase):` should be `class ToolsTests(BaseTestCase):`. – pzp Apr 26 '16 at 16:17
  • Thanks, updated. @martijn: I've tried all sorts of naming as I expected to be exactly that but haven't been able to fix it. To complete the question I think I need to write a proper simple version that runs and shows it's not working but alas do not have enough time for this. Thank you though. Of: dankjewel. – ikku100 Apr 28 '16 at 16:42
  • 1
    This can help maybe http://stackoverflow.com/questions/7667567/can-i-patch-a-python-decorator-before-it-wraps-a-function – gcw Aug 30 '16 at 18:39

0 Answers0