Consider the following project structure:
/project
- folder/
- some_config_module.py
- func.py
- test/
- __init__.py
- test_config.py
- test.py
Assume the following code
# func.py
import folder.some_config_module as config
def search_for_data():
var = config.predefined_parameter
# rest of code
I would now like to write a unit-test for search_for_data
. To do this in a sensible way, I need to mock the import of the some_config_module
, i.e. my try
# test.py
import unittest
from unittest.mock import patch
import tests.test_config as test_config
from folder.func import serach_for_data
class TestSearchForData(unittest.TestCase):
@patch('folder.some_config_module')
def test_one(self, test_config):
self.assertEqual(search_for_data(), 0)
if __name__ == "__main__":
unittest.main()
This does not result in the expected behaviour: what I would like is for search_for_data
inside of test_one
to import test.test_config
and not folder.some_config_module
. This code was based on this answer... But I seem to misunderstand something rather fundamental about mock
..