I have some utils function at src/utils/helper.py
Imagine I have a function called func_a
in utils/helper.py and it is used at multiple places in my project.
And every time I use it, I import it like this
from src.utils.helper import func_a
Now I want to mock this func_a
in my tests.
I want to create a fixture in conftest.py so that I don't need to write a mock function again and again for each test file.
The problem is, in my mock function I CANNOT write like this.
https://pypi.org/project/pytest-mock/
mocker.patch('src.utils.helper.func_a', return_value="some_value", autospec=True)
I have to write it like this for each test file
mocker.patch('src.pipeline.node_1.func_a', return_value="some_value", autospec=True)
As per the docs https://docs.python.org/3/library/unittest.mock.html#where-to-patch
Since I am importing func_a
like from src.utils.helper import func_a
I have to mock where it is used and not where it is defined.
But the problem with this approach is that I can not define it in my fixture in conftest.py
Directory Structure
├── src
│ ├── pipeline
│ │ ├── __init__.py
│ │ ├── node_1.py
│ │ ├── node_2.py
│ │ └── node_3.py
│ └── utils
│ ├── __init__.py
│ └── helper.py
└── tests
├── __init__.py
├── conftest.py
└── pipeline
├── __init__.py
├── test_node_1.py
├── test_node_2.py
└── test_node_3.py