我正在使用编写单元测试nose http://somethingaboutorange.com/mrl/projects/nose/0.11.2/,我想检查函数是否引发警告(该函数使用warnings.warn
)。这是很容易就能做到的事情吗?
def your_code():
# ...
warnings.warn("deprecated", DeprecationWarning)
# ...
def your_test():
with warnings.catch_warnings(record=True) as w:
your_code()
assert len(w) > 1
当然,您不仅可以检查长度,还可以深入检查:
assert str(w.args[0]) == "deprecated"
在 python 2.7 或更高版本中,您可以通过最后一次检查来执行此操作:
assert str(w[0].message[0]) == "deprecated"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)