I have a module set up roughly as follows:
# foo.py
def generate_things_based_on_other_things():
# some nasty things here
# bar.py
from foo import generate_things_based_on_other_things as generate
def coo():
generate()
# conftest.py
import pytest
@pytest.fixture(autouse=True)
def patch_generate(monkeypatch):
def mock_generate():
print("hello!")
monkeypatch.setattr("app.bar.generate", mock_generate)
# test_bar.py
from bar import coo
def test_coo():
coo()
As per this answer I made sure to monkeypatch the actual imported instance of the function. Any other path throws a "does not exist on module"
error.
However when I run the test I hit an error, because the original function generate
is being called, despite it being monkeypatched.
I can't figure out why this patch won't stick the way I expect it too.
I would expect this test to print "hello!".