我有一个模块设置了大致如下:
# 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.
我不能弄清楚为什么这片不会坚持我的期望。
我希望这个试验,以打印的"你好!"。