Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fail a python unittest if the setUpClass throws exception

I am having little trouble using the 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.

  2. When there is a setUpClass exception, how can we ensure that tearDownClass runs (unittest doesn't run it, should we manualy call it).

like image 875
sysuser Avatar asked Aug 15 '12 20:08

sysuser


People also ask

How do I check if an exception is thrown in Python?

There are two ways you can use assertRaises: using keyword arguments. Just pass the exception, the callable function and the parameters of the callable function as keyword arguments that will elicit the exception. Make a function call that should raise the exception with a context.

Which item in Python will stop a unit test abruptly?

An exception object is created when a Python script raises an exception. If the script explicitly doesn't handle the exception, the program will be forced to terminate abruptly.

Is PyUnit the same as unittest?

PyUnit is an easy way to create unit testing programs and UnitTests with Python. (Note that docs.python.org uses the name "unittest", which is also the module name.)

What does unittest main () do?

Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.


2 Answers

You can call tearDownClass on an exception as Jeff points it out, but you may also implements the __del__(cls) method :

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.

Hope it helps

like image 78
Y__ Avatar answered Sep 18 '22 18:09

Y__


The best option would be is to add handler for the except which calls tearDownClass and re-raise exception.

@classmethod
def setUpClass(cls):
    try:
        super(MyTest, cls).setUpClass()
        # setup routine...
    except Exception:  # pylint: disable = W0703
        super(MyTest, cls).tearDownClass()
        raise
like image 27
Andriy Ivaneyko Avatar answered Sep 21 '22 18:09

Andriy Ivaneyko