[英]How to pytest a python function with raise exception
My folder struct looks like this:我的文件夹结构如下所示:
|- src
|- __init__.py
|- funcA.py
|- util.py
|- tests
|- __init__.py
|- test_funcA.py
|- test_util.py
My goal is to test a function in funcA.py我的目标是在 funcA.py 中测试一个函数
def f():
try:
helper()
except Exception as e:
raise Exception('error: fail to call helper')
The helper function in util.py util.py 中的辅助函数
def helper():
try:
#do something
except Exception as e:
raise Exception('error: fail to do something')
The unit test I write for f() is not cover these two lines except Exception as e: raise Exception('error: fail to call helper')
我为 f() 编写的单元测试不包括这两行,
except Exception as e: raise Exception('error: fail to call helper')
Here is my testcase for f这是我的 f 测试用例
def test__f():
with mock.patch('src.utils.helper', side_effect=Exception('fail to call helper')):
from src import funcA
with pytest.raises(Exception):
funcA.f()
How to write unit test to cover f's raise exception?如何编写单元测试来覆盖 f 的引发异常? Thanks
谢谢
I think that the "src.utils.helper" is wrong.我认为“src.utils.helper”是错误的。 I guess you should use "src.funcA.helper".
我想你应该使用“src.funcA.helper”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.