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?
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 thatfunction_to_be_testedmust 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.