मैं एक मॉड्यूल स्थापित मोटे तौर पर के रूप में इस प्रकार है:
# 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()
प्रति के रूप में इस सवाल का जवाब मुझे यकीन है कि के लिए monkeypatch वास्तविक आयातित आवृत्ति के एक समारोह है । किसी भी अन्य रास्ता फेंकता "does not exist on module"
त्रुटि.
हालांकि जब मैं परीक्षण चलाने के लिए मैं मारा एक त्रुटि है, क्योंकि मूल समारोह generate
कहा जा रहा है, के बावजूद यह जा रहा है monkeypatched.
मैं समझ नहीं क्यों इस पैच छड़ी नहीं होगा जिस तरह से मैं यह उम्मीद भी है.
मुझे उम्मीद है कि इस टेस्ट के लिए प्रिंट "नमस्ते!".