Skip to main content

Posts

Showing posts from 2018

Know where to patch!!!

Have you tried to patch a function call in your unit test and wondering why the hell patching is not working, value I'm returning from the patched function is not the value being used in the function call. Well, may be you are not patching at the right place. I saw this problem at work. I'll try to explain with a simple example. you have a module module_a which has a function greet. There is another module module_b which uses this function from module_a in another function - sayHi . When we write test case for module_b's sayHi, we want to patch function call to module_a's greet method. module_a.py def greet (): """ returns sum of two numbers""" return 'hello world' module_b.py from module_a import greet class myClass (): def sayHi ( self ): return greet() if __name__ == '__main__' : res = myClass().sayHi() print (res) test_mod_b.py import un