Unit testing a big python application comes with its own set of worries which includes mocking calls to parts of code which we will test somewhere else.
Let us say I have a utils.py (every project has one anyways)
And I have another module which uses word_length using the from module import function syntax.
Now I want to unit test all the functions in user.py and since I am going to test just the user module and want to avoid costly calls made by my utils module I am going to mock out calls to word_length
using Python mock library.
One would expect this assertion to fail because we have mocked out the word_length to return 10. But this passes and our mock is not working. Why? Because NAMESPACE. Here we have patched the function in the namespace utils. But we have imported the function to the user.py namespace by using from module import function
. So we need to patch the function in the user namespace where it is used and not in the utils where it is defined. So change the line
But what if we have used simply like
This time we can straight away use the @patch('project.utils.word_length')
as we are importing the entire module and namespace remains as such.