Python Pytest, unittest : Mocking out a non-existent imported module

  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    module
AI Thread Summary
To write a test module that mocks the import statement in the provider module, the key approach is to use `sys.modules` to replace the missing module with a mock. Instead of needing a full dotted path for the module, simply assigning `sys.modules['a_missing_module'] = Mock()` suffices. This method allows for effective testing of the client module that imports the `helper_function` from the provider, even when the actual implementation of the missing module is not included. This technique simplifies the mocking process when only specific functions are imported from a module.
Swamp Thing
Insights Author
Messages
1,031
Reaction score
769
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).
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.
 
Technology news on Phys.org
Ok, got it after some trial and error. You just do sys.modules['a_missing_module'] = Mock(). You don't need to worry about a full dotted path to the module, unlike the more usual kind of mocking/patching.
 
  • Informative
Likes Wrichik Basu
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
7
Views
5K
Replies
9
Views
1K
Replies
4
Views
5K
Replies
18
Views
1K
Replies
1
Views
1K
Back
Top