简体   繁体   中英

Patch a function call within function from different file using Python

I am currently learning how to perform unit test in Python.

I have a file app/utils/B.py that is imported by file app/views/A.py B.py imports function from 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"})

A.py

from app.utils.B import do_something

def calculation():
   do_something()

My question is, when writing the test for A, I tried to see if log_error works. I tried @patch("app.views.A.log_error", autospec=True) but this mock doesn't been called.

Could anyone suggest a solution. Thanks!

刚刚自己找到答案,路径模块B代替

@patch("app.utils.B.log_eror", autospec=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM