简体   繁体   English

当assertRaises失败时,python unittest assertRaises抛出异常

[英]python unittest assertRaises throws exception when assertRaises fails

I've got code where assertRaises throws an exception when assertRaises fails. 我有代码,其中assertRaises在assertRaises失败时抛出异常。 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. 我认为如果assertRaises失败,那么测试就会失败,我会在最后得到一个报告说测试失败了。 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. 我正在使用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)

The code as posted is wrong. 发布的代码是错误的。 For a start, class myClass(): shoudl be class myClass: . 首先, class myClass(): shoudl是class myClass: . Also if name == "main": should be: if name == "main":应该是:

if __name__ == "__main__":
    unittest.main()

Apart from these problems, this fails because getName() is raising exception myExcOne and your test expects exception myExcTwo . 除了这些问题之外,这也会失败,因为getName()会引发异常myExcOne并且您的测试需要异常myExcTwo

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: 请编辑问题中的代码,以便我们将其剪切并粘贴到Python会话中:

import unittest

class myExcOne(Exception): "exception one"

class myExcTwo(Exception): "exception two"

class myClass:
    def getName(self):
        raise myExcTwo

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)

if __name__ == "__main__":
    unittest.main()

Starting with an aside, the () after the classname in a class statement is perfectly correct in modern Python -- not an error at all. 从旁开始, class声明中的classname之后的()在现代Python中是完全正确的 - 根本不是错误。

On the meat of the issue, assertRaises(MyException, foo) is documented to propagate exceptions raised by calling foo() whose type is NOT a subclass of MyException -- it only catches MyException and subclasses thereof. 在问题的assertRaises(MyException, foo)上, assertRaises(MyException, foo) 记录为传播通过调用foo()引发的异常,其类型不是MyException的子类 - 它只捕获MyException及其子类。 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 the unittest module, here , and I quote: 由于您的代码引发了一种类型的异常,并且您的测试需要一种不同的不相关类型,因此引发的异常将按照unittest模块的文档传播, 这里 ,我引用:

The test passes if exception is raised, is an error if another exception is raised, or fails if no exception is raised. 如果引发异常则测试通过,如果引发另一个异常则为错误,如果没有引发异常则失败。

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. 当您捕获在try / except块中传播的异常时,您将使错误无效,并且没有任何东西可供unittest诊断。 If your purpose is to turn this error into a failure (a debatable strategy...), your except block should call self.fail . 如果您的目的是将此错误转换为失败(有争议的策略......),则您的except块应调用self.fail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM