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

  • Context: Python 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
  • Tags Tags
    module
Click For Summary
SUMMARY

This discussion focuses on mocking a non-existent imported module in Python using the Pytest framework. The user demonstrates how to mock the missing module 'a_missing_module' in the 'provider.py' file by directly assigning a Mock object to 'sys.modules'. The solution highlights that when importing a specific function from a module, a full dotted path is unnecessary for mocking. This approach simplifies the testing process for cases where the imported module does not exist.

PREREQUISITES
  • Understanding of Python's import system
  • Familiarity with the Pytest testing framework
  • Knowledge of mocking techniques in Python
  • Experience with the sys module for module manipulation
NEXT STEPS
  • Learn how to use unittest.mock for advanced mocking techniques
  • Explore Pytest fixtures for setting up test environments
  • Study the differences between mocking and patching in Python
  • Investigate best practices for testing modules with external dependencies
USEFUL FOR

Software developers, particularly those working with Python, testers implementing unit tests, and anyone interested in improving their skills in mocking and testing non-existent modules.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
785
TL;DR
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   Reactions: Wrichik Basu

Similar threads

Replies
5
Views
15K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K