How to Use setUp and tearDown in unittest in Python
In Python's
unittest framework, use setUp to prepare the test environment before each test method runs, and tearDown to clean up after each test method finishes. These methods help keep tests isolated and avoid repeated setup code.Syntax
The setUp and tearDown methods are defined inside a test class that inherits from unittest.TestCase. setUp runs before each test method, and tearDown runs after each test method.
def setUp(self):— prepare test environmentdef tearDown(self):— clean up after test
python
import unittest class MyTest(unittest.TestCase): def setUp(self): # Code here runs before each test pass def tearDown(self): # Code here runs after each test pass def test_example(self): self.assertTrue(True)
Example
This example shows how setUp creates a list before each test, and tearDown clears it after each test. This keeps tests independent and avoids shared state.
python
import unittest class ListTest(unittest.TestCase): def setUp(self): self.test_list = [1, 2, 3] print('setUp: List created') def tearDown(self): self.test_list.clear() print('tearDown: List cleared') def test_append(self): self.test_list.append(4) self.assertEqual(self.test_list, [1, 2, 3, 4]) def test_pop(self): value = self.test_list.pop() self.assertEqual(value, 3) self.assertEqual(self.test_list, [1, 2]) if __name__ == '__main__': unittest.main()
Output
setUp: List created
.tearDown: List cleared
setUp: List created
.tearDown: List cleared
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Common Pitfalls
Common mistakes include:
- Not calling
super().setUp()orsuper().tearDown()when overriding these methods in subclasses, which can skip important setup or cleanup. - Modifying shared state outside
setUpandtearDown, causing tests to affect each other. - Putting expensive setup code in
setUpwhen it only needs to run once (usesetUpClassinstead).
python
import unittest class BaseTest(unittest.TestCase): def setUp(self): print('Base setup') class ChildTest(BaseTest): def setUp(self): # Missing super() call - Base setup is skipped print('Child setup') def test_something(self): self.assertTrue(True) # Correct way: class FixedChildTest(BaseTest): def setUp(self): super().setUp() # Calls BaseTest setUp print('Fixed child setup') def test_something(self): self.assertTrue(True)
Quick Reference
| Method | When It Runs | Purpose |
|---|---|---|
| setUp | Before each test method | Prepare test environment |
| tearDown | After each test method | Clean up test environment |
| setUpClass | Once before all tests in class | Prepare expensive resources |
| tearDownClass | Once after all tests in class | Release expensive resources |
Key Takeaways
Use setUp to prepare fresh test data before each test method runs.
Use tearDown to clean up or reset after each test to avoid side effects.
Always call super().setUp() and super().tearDown() when overriding in subclasses.
Avoid sharing mutable state between tests outside setUp and tearDown.
For expensive setup needed once, use setUpClass and tearDownClass instead.