pythonunittestassertRaises 当assertRaises失败时抛出异常
我有代码,当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)
I've got code where assertRaises throws an exception when assertRaises fails. I thought that if assertRaises fails then the test would fail and I'd get a report at the end that says the test failed. I wasn't expecting the exception to be thrown. Below is my code. I'm I doing something wrong? I'm using 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顺便说一句,
class
语句中类名后面的()
在现代 Python 中是完全正确的——根本不是错误。关于问题的实质,
assertRaises(MyException, foo)
记录用于传播通过调用foo() 的类型不是
MyException
的子类——它只捕获MyException
及其子类。 当您的代码引发一种类型的异常并且您的测试需要不同的不相关类型之一时,引发的异常将按照unittest
模块的文档 这里,我引用:“是一个错误”意味着“传播另一个异常”。
当您捕获在 try/ except 块中传播的异常时,您可以使错误无效,并且没有任何内容可供
unittest
诊断。 如果您的目的是将此错误转变为失败(一个有争议的策略...),您的except 块应该调用
self.fail 。
Starting with an aside, the
()
after the classname in aclass
statement is perfectly correct in modern Python -- not an error at all.On the meat of the issue,
assertRaises(MyException, foo)
is documented to propagate exceptions raised by callingfoo()
whose type is NOT a subclass ofMyException
-- it only catchesMyException
and subclasses thereof. As your code raises an exception of one type and your test expects one of a different unrelated type, the raised exception will then propagate, as per the docs of theunittest
module, here, and I quote:And "is an error" means "propagates the other exception".
As you catch the exception propagating in your try/except block, you nullify the error, and there's nothing left for
unittest
to diagnose. If your purpose is to turn this error into a failure (a debatable strategy...), yourexcept
block should callself.fail
.发布的代码是错误的。 首先,
class myClass():
应该是class myClass:
。 另外if name == "main":
应该是:除了这些问题之外,这会失败,因为
getName()
引发异常myExcOne
并且您的测试需要异常myExcTwo
。这是一些有效的代码。 请编辑您问题中的代码,以便我们可以轻松地将其剪切并粘贴到 Python 会话中:
The code as posted is wrong. For a start,
class myClass():
shoudl beclass myClass:
. Alsoif name == "main":
should be:Apart from these problems, this fails because
getName()
is raising exceptionmyExcOne
and your test expects exceptionmyExcTwo
.Here is some code that works. Please edit the code in your question so that it is easy for us to cut and paste it into a Python session: