请问python的unittest能不能期待一个错误出现而通过测试,不报错?比如下边的代码运行时会抛出一个ERROR,有没有可能换一个测试函数实现顺利通过测试(出来一个点)?改成assertRaises也是要报错。谢谢。import unittestclass TestStringMethods(unittest.TestCase): def test_score(self): self.failUnlessRaises(ZeroDivisionError, 1/0)if __name__ == '__main__': unittest.main()
2 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
“断言”只管判断,而不在乎判断的东西到底什么。
异常不是“断言”这个层面考虑的问题。
你的问题自己
try
起来就完了。
def fail_unless_raises(error_type, func):
try:
func()
except error_type:
return
else:
raise Exception('xxx')
fail_unless_raises(ZeroDivisionError, lambda: 1/1)
千巷猫影
TA贡献1829条经验 获得超7个赞
import unittest
class TestStringMethods(unittest.TestCase):
def test_score(self):
with self.assertRaises(ZeroDivisionError):
1/0
if __name__ == '__main__':
unittest.main()
- 2 回答
- 0 关注
- 1066 浏览
添加回答
举报
0/150
提交
取消