我在 Python 中有一个以下函数,我想用 unittest 进行测试,如果函数获取 0 作为参数,它会引发警告。我已经尝试过 assertRaises,但由于我没有发出警告,所以它不起作用。
def isZero(i):
if i != 0:
print "OK"
else:
warning = Warning("the input is 0!")
print warning
return i
最佳答案
从 Python 3.2 开始,您可以简单地使用 assertWarns()
方法。
with self.assertWarns(Warning):
do_something()
关于python - 如何使用 Python 的 unittest 测试已引发警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3892218/