TestCase setUpTestData vs setUp - zamaniamin/python GitHub Wiki

In Django's TestCase, both setUpTestData() and setUp() methods are used to prepare the environment for tests, but they serve different purposes and are called at different times in the test lifecycle. Here’s a detailed comparison:

setUpTestData()

  • Purpose: This method is used to set up data that is shared across all test methods in a class. It's called only once for the entire test class.
  • Execution Time: setUpTestData() is executed only once when the test class is loaded, before any test methods run.
  • Typical Use Case: Use setUpTestData() for creating data that is read-only and shared among all tests to avoid repetitive database setup operations. This can make tests run faster because the data is only created once.
  • Example:
    from django.test import TestCase
    from myapp.models import MyModel
    
    class MyModelTestCase(TestCase):
        @classmethod
        def setUpTestData(cls):
            cls.my_model_instance = MyModel.objects.create(name='test')
    
        def test_example_1(self):
            self.assertEqual(self.my_model_instance.name, 'test')
    
        def test_example_2(self):
            self.assertEqual(self.my_model_instance.name, 'test')
    

setUp()

  • Purpose: This method is used to set up the environment before each individual test method is run.
  • Execution Time: setUp() is executed before each test method. This means if you have 10 test methods, setUp() will run 10 times.
  • Typical Use Case: Use setUp() for creating or resetting data that needs to be fresh for each test, ensuring no test affects another.
  • Example:
    from django.test import TestCase
    from myapp.models import MyModel
    
    class MyModelTestCase(TestCase):
        def setUp(self):
            self.my_model_instance = MyModel.objects.create(name='test')
    
        def test_example_1(self):
            self.assertEqual(self.my_model_instance.name, 'test')
    
        def test_example_2(self):
            self.assertEqual(self.my_model_instance.name, 'test')
    

Summary

  • setUpTestData():

    • Runs once per test class.
    • Suitable for creating shared, read-only test data.
    • Helps improve performance by reducing repetitive setup operations.
  • setUp():

    • Runs before every test method.
    • Suitable for setting up or resetting data specific to each test method.
    • Ensures isolation between tests.

Combined Usage

You can combine both methods to take advantage of shared setup data while still having fresh setups for individual tests:

from django.test import TestCase
from myapp.models import MyModel

class MyModelTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.shared_instance = MyModel.objects.create(name='shared')

    def setUp(self):
        self.test_instance = MyModel.objects.create(name='test')

    def test_example_1(self):
        self.assertEqual(self.shared_instance.name, 'shared')
        self.assertEqual(self.test_instance.name, 'test')

    def test_example_2(self):
        self.assertEqual(self.shared_instance.name, 'shared')
        self.assertEqual(self.test_instance.name, 'test')

In this example, shared_instance is created once and shared across tests, while test_instance is created fresh for each test method.