0
单一功能我有一个项目的一个结构:存根模块
# A.py
def foo():
result = None
# some long, very long calculations
return result
# B.py
from A import foo
def bar():
result = foo()
# some not so long and complex calculations
return some_other_result
# test.py
import A
import B
def setup_module():
A.foo = lambda: return "Hello"
def test_foo():
assert B.foo() == "Hello"
但是,这并不工作,因为在B
的foo
被直接导入。
如何存根A.foo
函数?
说明:我无法编辑A.py
和B.py
。只有用于测试的文件,因此从from A import foo
到import A
和B.py
中的A.foo
的使用没有变化是可能的。