- #1
- 961
- 664
- TL;DR Summary
- A 'provider' module imports 'a_missing_module'. A 'client' module imports just one function from 'provider'. (It does not import the entire 'provider' module). How can we mock out 'a_missing_module' while testing?
The following provider module imports a_missing_module and implements a helper_function. (I haven't put in any code that actually uses the missing module, but there could be such code in general).
Now this client module imports the helper_function from the provider:
How can I write a test module that mocks out the import statement in the provider? In the past, I have used things like
Code:
# provider.py
import a_missing_module
def helper_function():
return 999
Now this client module imports the helper_function from the provider:
Code:
# client.py
from provider import helper_function
def client_function():
return helper_function()
How can I write a test module that mocks out the import statement in the provider? In the past, I have used things like
sys.modules['client.provider.missing_module'] = Mock()
in cases where the client imports an entire provider module, but now it only imports a function from the provider, so I don't know how to proceed.