159

What is the difference between setUp() and setUpClass() in the Python unittest framework? Why would setup be handled in one method over the other?

I want to understand what part of setup is done in the setUp() and setUpClass() functions, as well as with tearDown() and tearDownClass().

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
etja
  • 1,591
  • 2
  • 10
  • 6

4 Answers4

228

The difference manifests itself when you have more than one test method in your class. setUpClass and tearDownClass are run once for the whole class; setUp and tearDown are run before and after each test method.

For example:

class Example(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("setUpClass")

    def setUp(self):
        print("setUp")

    def test1(self):
        print("test1")

    def test2(self):
        print("test2")

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

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

When you run this test, it prints:

setUpClass
setUp
test1
tearDown
.setUp
test2
tearDown
.tearDownClass

(The dots (.) are unittest's default output when a test passes.) Observe that setUp and tearDown appear before and after test1 and test2, whereas setUpClass and tearDownClass appear only once, at the beginning and end of the whole test case.

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
  • Shouldn't the order be this ? : setUpClass setUp test1 tearDown .setUp test2 .tearDown tearDownClass – Jai Sharma Dec 20 '17 at 09:24
  • Notice the "." in front of tearDown and the absence of "." in front of tearDownClass – Jai Sharma Dec 20 '17 at 11:15
  • 2
    Ah, sorry, didn’t spot that. No, `unittest` doesn’t consider a test to have passed until its `tearDown` has completed without incident. – Benjamin Hodgson Dec 20 '17 at 11:16
  • So each of the methods test1 and test2 should have their own set of setUp & tearDown, right? In your answer, test1 doesn't have any tearDown method and thus it should've printed the default output(with the . ). Correct me if i'm wrong. – Jai Sharma Dec 20 '17 at 11:21
  • 1
    The output in the answer is correct. I pasted it directly from unittest’s output. `setUp` and `tearDown` are each run once for every `test` method (so twice in total in this example) but `setUpClass` and `tearDownClass` are run just once each. – Benjamin Hodgson Dec 20 '17 at 11:24
33

What is the difference between setUp() and setUpClass() in the Python unittest framework?

The main difference (as noted in the answer by Benjamin Hodgson) is that setUpClass is called only once and that is before all the tests, while setUp is called immediately before each and every test. (NB: The same applies to the equivalent methods in other xUnit test frameworks, not just Python's unittest.)

From the unittest documentation:

setUpClass()

A class method called before tests in an individual class are run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def setUpClass(cls):
    ...

and:

setUp()

Method called to prepare the test fixture. This is called immediately before calling the test method; other than AssertionError or SkipTest, any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.

Why would setup be handled in one method over the other?

This part of the question has not been answered yet. As per my comment in response to the answer by Gearon, the setUp method is meant for elements of the fixture that are common to all tests (to avoid duplicating that code in each test). I find this is often useful as removing duplication (usually) improves readability and reduces the maintenance burden.

The setUpClass method is for expensive elements that you would rather only have to do once, such as opening a database connection, opening a temporary file on the filesystem, loading a shared library for testing, etc. Doing such things before each test would slow down the test suite too much, so we just do it once before all the tests. This is a slight degradation in the independence of the tests but a necessary optimization in some situations. Arguably, one should not be doing such things in unit tests as it is usually possible to mock the database / filesystem / library / whatever without using the real thing. As such, I find that setUpClass is rarely needed. However, it is useful when testing the above examples (or similar) becomes necessary.

Community
  • 1
  • 1
Biggsy
  • 1,306
  • 1
  • 12
  • 28
1

The setUp() and setUpClass() methods in the Python unittest framework are used to set up the test environment before running the tests. The main difference between the two methods is that:

Method When is it called? What is it used for?
setUp() Before each test method is run Setting up the test environment for each individual test method
setUpClass() Once, before all the tests in the class are run Setting up the test environment for the entire class

Here are some examples of when you might want to use setUpClass() instead of setUp():

  1. If you need to create a database connection or a web driver that will be used by all the tests in the class.
  2. If you need to load some initial data into the database.
  3. If you need to set some global configuration options.

In general, you should use setUpClass() for tasks that need to be done only once, before all the tests in the class are run. You should use setUp() for tasks that need to be done before each individual test method is run.

The tearDown() and tearDownClass() methods are used to tear down the test environment after the tests have been run. The tearDown() method is called after each test method is run, and the tearDownClass() method is called after all the tests in the class have been run. These methods are used to clean up any resources that were used by the tests, such as database connections or web drivers, and to reset the test environment to its initial state.

I hope this helps!

Bothraj P
  • 26
  • 3
0

As they mentioned above, setup() and teardown() will run after and before each test. however SetupClass() and TearDownClass() will run only once for the whole class.