What is the difference between setUp() and setUpClass() in Python unittest?
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.
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.