我有代码,当 assertRaises 失败时,assertRaises 会抛出异常。我认为如果 assertRaises 失败,那么测试就会失败,最后我会得到一份报告说测试失败。我没想到会抛出异常。下面是我的代码。我做错了什么?我正在使用 Python 2.6.2。
import unittest
class myClass:
def getName(self):
raise myExcOne, "my exception one"
#raise myExcTwo, "my exception two"
#return "a"
class myExcOne(Exception):
"exception one"
class myExcTwo(Exception):
"exception two"
class test_myClass(unittest.TestCase):
def setUp(self):
self.myClass = myClass()
def testgetNameEmpty(self):
#self.assertRaises(myExcOne,self.myClass.getName)
#self.assertRaises(myExcTwo,self.myClass.getName)
try:
self.assertRaises(myExcTwo,self.myClass.getName)
except Exception as e:
pass
if __name__ == "__main__":
#unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(test_myClass)
unittest.TextTestRunner(verbosity=2).run(suite)