Reputation: 165
I have a file structure similar to the following:
app/
|--- __init__.py
|--- module_under_test.py
test/
|--- test_module.py
I want to unittest a module in a package by using pytest. When I import the module in my test file, the code in the __init__.py gets executed. However I want to bypass the code in the __init__.py file because in __init__.py other external dependencies are used. One way I can think of is mocking all necessary objects in __init__.py by using patch.
What is the best practise to solve this problem? Thank you.
Upvotes: 3
Views: 1374
Reputation: 473
If you really need to avoid invoking __init__.py
file, there is a rather dirty workaround. For the given directory structure:
app/
|--- __init__.py
|--- module_under_test.py
test/
|--- test_module.py
Add at the top of the test_module.py
import sys
sys.path.append('../app')
from module_under_test import some_function
Inspired by How to import a module but ignoring the package's __init__.py?
Upvotes: 1