[英]Patch a function call within function from different file using Python
我目前正在学习如何在 Python 中执行单元测试。
我有一个文件app/utils/B.py
,它是由文件app/views/A.py
B.py
导入函数从app/utils/util.py
。
B.py
from app.utils.util import log_error
def do_something():
try:
int(10/0)
except Exception as e:
log_error({"msg": "error"})
py
from app.utils.B import do_something
def calculation():
do_something()
我的问题是,在为 A 编写测试时,我尝试查看log_error
有效。 我试过@patch("app.views.A.log_error", autospec=True)
但是这个模拟没有被调用。
任何人都可以提出解决方案。 谢谢!
刚刚自己找到答案,路径模块B代替
@patch("app.utils.B.log_eror", autospec=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.