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()
.
You can read more with examples here. When a setUp() method is defined, the test runner will run that method prior to each test. Likewise, if a tearDown() method is defined, the test runner will invoke that method after each test.
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.
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.)
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With