【发布时间】:2016-05-30 10:20:02
【问题描述】:
使用 Python 3.5,为什么下面的所有测试在运行时都通过了?既然调用div 时不会引发Exception,那么assertRaises() 怎么不抱怨?
根据assertRaises() 的文档:“如果没有引发异常,则失败”。
谁能帮帮我?
..
----------------------------------------------------------------------
Ran 2 tests in 0.002s
def div(self, x, y):
if y == 0:
raise Exception("Division by zero")
return x / y
class MyTest(unittest.TestCase):
def test1(self):
with self.assertRaises(Exception) as cm:
self.div(2, 1)
def test2(self):
self.assertRaises(Exception, div, 2, 1)
【问题讨论】:
-
不要扔又抓
Exception,因为很容易不小心抓到错误的东西,比如被扔在这里的TypeError
标签: python unit-testing