简体   繁体   English

如果 setUpClass 抛出异常,如何使 python 单元测试失败

[英]How to fail a python unittest if the setUpClass throws exception

I am having little trouble using the python setUpClass.我在使用 python setUpClass 时遇到了一些麻烦。

For example consider the following case例如考虑以下情况

class MyTest(unittest.case.TestCase):

    @classmethod
    def setUpClass(cls):
        print "Test setup"
        try:
            1/0
        except:
            raise

    @classmethod
    def tearDownClass(cls):
        print "Test teardown"

A couple of questions几个问题

  1. Is the above code the right way to handle test setUpClass exceptions (by raising it so that the python unittest can take care of it), there are fail(), skip() methods, but those can only be used by test instances and not the test classes.上面的代码是否是处理测试 setUpClass 异常的正确方法(通过引发它以便 python unittest 可以处理它),有 fail()、skip() 方法,但这些方法只能由测试实例使用,而不是测试类。

  2. When there is a setUpClass exception, how can we ensure that tearDownClass runs (unittest doesn't run it, should we manualy call it).当出现setUpClass异常时,如何保证tearDownClass运行(unittest不运行,应该手动调用)。

You can call tearDownClass on an exception as Jeff points it out, but you may also implements the __del__(cls) method :您可以像 Jeff 指出的那样对异常调用tearDownClass ,但您也可以实现__del__(cls)方法:

import unittest

class MyTest(unittest.case.TestCase):

    @classmethod
    def setUpClass(cls):
        print "Test setup"
        try:
            1/0
        except:
            raise

    @classmethod
    def __del__(cls):
        print "Test teardown"

    def test_hello(cls):
        print "Hello"

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

Will have the following output :将有以下输出:

Test setup
E
======================================================================
ERROR: setUpClass (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "my_test.py", line 8, in setUpClass
    1/0
ZeroDivisionError: integer division or modulo by zero

----------------------------------------------------------------------
Ran 0 tests in 0.000s

FAILED (errors=1)
Test teardown

Note : you should be aware that the __del__ method will be called at the end of the program execution, which is maybe not what you want if you have a more than one test class.注意:您应该知道__del__方法将在程序执行结束时被调用,如果您有一个以上的测试类,这可能不是您想要的。

Hope it helps希望能帮助到你

The best option would be is to add handler for the except which calls tearDownClass and re-raise exception.最好的选择是为调用tearDownClass 并重新引发异常的except 添加处理程序。

@classmethod
def setUpClass(cls):
    try:
        super(MyTest, cls).setUpClass()
        # setup routine...
    except Exception:  # pylint: disable = W0703
        super(MyTest, cls).tearDownClass()
        raise
import contextlib

class MyTestCase(unitest.TestCase):

    @classmethod
    def setUpClass(cls):
        with contextlib.ExitStack() as stack:
            # ensure teardown is called if error occurs
            stack.callback(cls.tearDownClass)

            # Do the things here!

            # remove callback at the end if no error found
            stack.pop_all()

use tearDownModule.使用tearDownModule。 It should be called after setUpClass runs.它应该在 setUpClass 运行后调用。

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

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