Mám modul nastavený zhruba takto:
# 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()
Podľa tejto odpovede som sa uistiť, monkeypatch skutočné dovezené stupňa funkciu. Všetky ostatné cesta vrhá "does not exist on module"
chyby.
Avšak, keď som spustiť test som narazila chybu, pretože pôvodná funkcia generate
sa nazýva, napriek tomu, že to monkeypatched.
Nemôžem prísť na to, prečo tento patch nebudete držať, tak ako som ju očakávať príliš.
Ja by som očakávať, že tento test, ak chcete tlačiť "dobrý deň!".