How to run setUp only once in python unittest

Confirmed environment

Overview

When unittest does multiple tests, setUp is run each time the test is called. However, if the setUp process is heavy, it will take too long to execute because it is called multiple times.

So, if you want to call setUp only once, how should you write it? It is solved by defining setUpClass instead of using setUp.

The following code is given as a specific example.

test_setup.py


import unittest

class HogeTest(unittest.TestCase):

    def setUp(self):
        print('Called `setUp`.')

    @classmethod
    def setUpClass(self):
        print('Called `setUpClass`.')

    def test_called1(self):
        self.assertTrue(True)

    def test_called2(self):
        self.assertTrue(True)

    def test_called3(self):
        self.assertTrue(True)

This code has a method that is executed when setUp and setUpClass are initialized. These are the methods provided by unittest respectively. Also, the method here is defined to output a character string each time it is executed. There are also three defined tests.

Now let's see if the output string does what this code expects. Expect that the character string with setUp is output multiple times, and that the character string with setUpClass is output once.

Here is the result of the actual execution.

$ python3 -m unittest test_setup.py
Called `setUpClass`.
Called `setUp`.
.Called `setUp`.
.Called `setUp`.
.
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

It was confirmed that it was working as expected.

reference

qiita: python unit test template stackoverflow: Run setUp only once

Recommended Posts

How to run setUp only once in python unittest
How to run tests in bulk with Python unittest
How to run Leap Motion in non-Apple Python
How to run Notepad ++ Python
How to develop in Python
How to run python in virtual space (for MacOS)
How to read csv containing only integers in Python
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to test that Exception is raised in python unittest
[Introduction to Python] How to use class in Python?
How to install OpenCV on Cloud9 and run it in Python
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
Unittest in python
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
[Python2.7] Summary of how to use unittest
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to run a Maya Python script
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
Summary of how to import files in Python 3
Type Python scripts to run in QGIS Processing
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
How to run some script regularly in Django
How to run CNN in 1 system notation in Tensorflow 2
How to run MeCab on Ubuntu 18.04 LTS Python
Summary of how to use MNIST in Python
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to handle datetime type in python sqlite3