8

I have the following method in a module that calls another method imported from another module:

def imported_function():
    do_unnecessary_things_for_unittest()

The actual method that needs to be tested, imports and uses the above function:

from somewhere import imported_function

def function_to_be_tested():
    imported_function()
    do_something_more()
    return 42

The inner calls and related calculations inside imported_function are not important and they are not what I want to test so I just want to skip them while testing the actual function function_to_be_tested.

Thus, I tried to monkey patch the module named somewhere inside the test method but no luck.

def test_function_to_be_tested(self):
    import somewhere
    somewhere.__dict__['imported_function'] = lambda : True

The question is, how can I monkey patch a method of a module while testing so that it will not be called during the test phase?

1
  • 3
    Not directly answering your questions, but I suggest using a mocking library, such as mocktest: gfxmonk.net/dist/doc/mocktest/doc Apart from just mocking objects and functions, this library also allows you to define expectations on function calls. For example, you can define that function_to_be_tested must be called once with no arguments. You can also define what value should the function return. If the expectations are not met, the test will fail. This adds extra safety, since you know that the function has actually been called during the test as you intended. Commented Aug 31, 2012 at 7:46

2 Answers 2

17

I think better to use Mock Library

So you can do something like:

from somewhere import imported_function

@patch(imported_function)
def test_function_to_be_tested(self, imported_function):
    imported_function.return_value = True
    #Your test

I think for unit tests it's better than monkey patch.

Sign up to request clarification or add additional context in comments.

1 Comment

I had to do @patch('imported_function') to make it works.
8

Say you have the following files:

somewhere.py

def imported_function():
    return False

testme.py

from somewhere import imported_function

def function_to_be_tested():
    return imported_function()

A call to testme.function_to_be_tested() would return False.


Now, the trick is to import somewhere before testme:

import somewhere
somewhere.__dict__['imported_function'] = lambda : True

import testme
def test_function_to_be_tested():
    print testme.function_to_be_tested()

test_function_to_be_tested()

Output:

True


Or, reload the testme module

import testme

def test_function_to_be_tested():
    print testme.function_to_be_tested()
    import somewhere
    somewhere.__dict__['imported_function'] = lambda : True
    print testme.function_to_be_tested()
    reload(testme)
    print testme.function_to_be_tested()

test_function_to_be_tested()

Output:

False
False
True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.