pythonunittestassertRaises 当assertRaises失败时抛出异常

发布于 2024-07-30 15:04:21 字数 982 浏览 2 评论 0原文

我有代码,当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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘逸的'云 2024-08-06 15:04:26

顺便说一句,class 语句中类名后面的 () 在现代 Python 中是完全正确的——根本不是错误。

关于问题的实质,assertRaises(MyException, foo) 记录用于传播通过调用 foo() 的类型不是 MyException 的子类——它只捕获 MyException 及其子类。 当您的代码引发一种类型的异常并且您的测试需要不同的不相关类型之一时,引发的异常将按照 unittest 模块的文档 这里,我引用:

如果引发异常,则测试通过;如果引发另一个异常,则测试为错误;如果未引发异常,则测试失败。

“是一个错误”意味着“传播另一个异常”。

当您捕获在 try/ except 块中传播的异常时,您可以使错误无效,并且没有任何内容可供 unittest 诊断。 如果您的目的是将此错误转变为失败(一个有争议的策略...),您的 except 块应该调用 self.fail 。

Starting with an aside, the () after the classname in a class 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 calling foo() whose type is NOT a subclass of MyException -- it only catches MyException 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 the unittest module, here, and I quote:

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. If your purpose is to turn this error into a failure (a debatable strategy...), your except block should call self.fail.

随遇而安 2024-08-06 15:04:25

发布的代码是错误的。 首先,class myClass(): 应该是 class myClass:。 另外 if name == "main": 应该是:

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

除了这些问题之外,这会失败,因为 getName() 引发异常 myExcOne 并且您的测试需要异常 myExcTwo

这是一些有效的代码。 请编辑您问题中的代码,以便我们可以轻松地将其剪切并粘贴到 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()

The code as posted is wrong. For a start, class myClass(): shoudl be class myClass:. Also if name == "main": should be:

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

Apart from these problems, this fails because getName() is raising exception myExcOne and your test expects exception 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:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文