I am testing functions from different modules that use an imported helper function from helpers.py
. I am monkeypatching this helper function in each module function’s test (please see below). I would like to put setup_do_thing()
and mock_do_thing()
in a conftest.py
file to cut down on repetition and remove general_mocks.py
, but am unable to do this due to the different import paths supplied to monkeypatch.setattr()
in test_module_1.py
and test_module_2.py
.
Is there a way to put the fixture in a single file and supply to multiple test modules?
app/helper.py
:
def do_thing():
#do_thing
app/module1.py
:
from helper import do_thing
def use_do_thing_for_x():
...
do_thing()
...
app/module2.py
:
from helper import do_thing
def use_do_thing_for_y():
...
do_thing
...
tests/general_mocks.py
:
def mock_do_thing():
#do_other_thing
tests/test_module_1.py
:
from general_mocks import mock_do_thing
@pytest.fixture
def setup_do_thing(monkeypatch):
monkey_patch.setattr('app.module1.do_thing',
mock_do_thing)
def test_use_do_thing_for_x(setup_do_thing):
...
tests/test_module_2.py
:
from general_mocks import mock_do_thing
@pytest.fixture
def setup_do_thing(monkeypatch):
monkey_patch.setattr('app.module2.do_thing',
mock_do_thing)
def test_use_do_thing_for_y(setup_do_thing):
...