When writing tests with python unittests, use doCleanups for setUps that can fail

When writing a test using the unittest module, you may write a fixture to prepare for the test. Although tearDown receives and handles exceptions in the test function. Exceptions during setUp will be bypassed. Use doCleanups for setUps that can fail.

About test fixture

When writing a test using the unittest module, you may write a fixture to prepare for the test. For example, setUp / tearDown corresponds to that.

Write as follows.

class Tests(unittest.TestCase):
    def setUp(self):
        print("setUp")

    def tearDown(self):
        print("tearDown")

    def test_it(self):
        print("before execute")
        _callFUT()
        print("after execute")

At this time, it is executed in the following order.

  1. setUp
  2. test_it
  3. tearDown

tearDown is also called when an exception occurs in the test function in 2.


class Tests2(unittest.TestCase):
    def setUp(self):
        print("setUp")

    def tearDown(self):
        print("tearDown")

    def doCleanups(self):
        print("cleanup")

    def test_it(self):
        print("before execute")
        raise Exception
        print("after execute")

The execution order is the same.

  1. setUp
  2. test_it
  3. tearDown

If setUp throws an exception

Of course, the code written in setUp of test is also python code, so there are some that may raise exceptions. In such a case, it is dangerous to think that tearDown guarantees the cleanup after execution. In fact, tearDown is unaware of the exceptions in setUp.

class Tests3(unittest.TestCase):
    def setUp(self):
        print("setUp")
        raise Exception("setup")

    def tearDown(self):
        print("tearDown")

    def test_it(self):
        print("before execute")
        raise Exception("execute")
        print("after execute")

Only the following are called

  1. setUp

tearDown is not called. At this time, if you want to add some post-processing, use doCleanups. doCleanups is called unconditionally regardless of exceptions at setUp and tearDown.

class Tests4(unittest.TestCase):
    def setUp(self):
        print("setUp")
        raise Exception("setup")

    def tearDown(self):
        print("tearDown")

    def doCleanups(self):
        print("cleanup")

    def test_it(self):
        print("before execute")
        print("after execute")

The execution order in this case is

  1. setUp
  2. doCleanups

Will be.

Recommended Posts

When writing tests with python unittests, use doCleanups for setUps that can fail
Directory structure when writing tests with Python 3 standard unittest
Use a macro that runs when saving python with vscode
Use DeepL with python (for dissertation translation)
Mathematical optimization that can be used for free work with Python + PuLP
Until you can use opencv with python
Use logger with Python for the time being
Wrap C with Cython for use from Python
Wrap C ++ with Cython for use from Python
[Python] I want to use only index when looping a list with a for statement
Beginners can use Python for web scraping (1) Improved version
String manipulation with python & pandas that I often use
Note that writing like this with ruby is writing like this with python
Use Python installed with pyenv for PL / Python execution environment
Use Cursur that closes automatically with sqlite3 in Python
Python knowledge notes that can be used with AtCoder
[Python] Use JSON with Python
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Solution when you want to use cv_bridge with python3 (virtualenv)
Why can I use the module by importing with python?
[python] A note when trying to use numpy with Cython
Scripts that can be used when using bottle in Python
This and that for using Step Functions with CDK + Python
When I tried to use pip with python, I was told that XML_SetHashSalt could not be found.
Understand the probabilities and statistics that can be used for progress management with a python program
Workaround for the problem that? Is displayed when checking the version when using non-standard perl | python with pkg version