[英]Python test if a function was not called
我是编写单元测试的新手,所以请原谅我缺乏知识。 我看过以前的帖子,但仍然无法让它工作。
我有
def get_bugs():
bugs = []
if ...:
bugs.append(123)
# can be empty
return bugs
def operate(bugs):
for bug in bugs:
do something
def main():
bugs = get_bugs()
if bugs:
operate(bugs)
.... # other methods
-------------------------
# in my test
@mock.patch.object(myutility, "get_bugs", autospec=True, return_value=[])
def test_nobugstooperate():
# logic to ensure myutility.operate was not called because there are no bugs
我如何实现从未调用 mutility.operate 的测试? 我不能在它上面使用“.call”,因为它不可用。
据我所知,您无法测试是否尚未调用方法。 如果您在进行单元测试,则应该编写多个测试,每个测试都测试被测试单元的一种预期行为。 在您的情况下,您实际上必须测试您的operate
函数是否在有错误需要操作时被调用。 如果没有错误,您不应该担心operate
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.