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

  • Python
  • Thread starter Swamp Thing
  • Start date
  • Tags
    module
In summary, the conversation discusses how to mock out an import statement in a provider module for testing purposes. The client module imports a helper function from the provider, and in the past, sys.modules['client.provider.missing_module'] = Mock() was used to mock out the entire provider module. However, since the client now only imports a function, sys.modules['a_missing_module'] = Mock() can be used instead without specifying the full dotted path to the module.
  • #1
Swamp Thing
Insights Author
908
572
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
  • #2
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

1. What is Pytest and Unittest?

Pytest and Unittest are two popular testing frameworks in Python used for writing and executing automated tests. They provide a set of tools and methods to test code and check for expected behaviors and outcomes.

2. What is mocking in testing?

Mocking is a technique used in testing to simulate the behavior of an object or module that is not available or not yet implemented. It allows developers to test their code without having to rely on external dependencies.

3. Why would I need to mock a non-existent imported module?

Mocking a non-existent imported module can be useful in situations where the code being tested has dependencies on external modules that are not yet fully developed or available. By mocking these dependencies, developers can continue testing their code without being blocked by missing modules.

4. How do I mock a non-existent imported module in Pytest?

In Pytest, you can use the pytest-mock plugin to easily mock out non-existent imported modules. This plugin provides a mock object that can be used to create mock objects and functions to simulate the behavior of the missing module.

5. Can I use mocking in both Pytest and Unittest?

Yes, both Pytest and Unittest support mocking. However, the syntax and methods for mocking may differ between the two frameworks. Pytest uses the pytest-mock plugin, while Unittest has its own unittest.mock module for mocking.

Similar threads

  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top