Performance: TestCase and SimpleTestCase
This concept affects test execution speed and resource usage during development, impacting developer feedback loop time.
Jump into concepts and practice - no test required
from django.test import SimpleTestCase class MyTests(SimpleTestCase): def test_simple(self): self.assertEqual(1 + 1, 2)
from django.test import TestCase class MyTests(TestCase): def test_simple(self): self.assertEqual(1 + 1, 2)
| Pattern | Database Setup | Test Speed | Isolation | Verdict |
|---|---|---|---|---|
| TestCase | Creates test DB and rolls back | Slower due to DB setup | High isolation | [!] OK |
| SimpleTestCase | No DB setup | Faster tests | No DB isolation | [OK] Good |
TestCaseTestCase is designed for tests that require database access and setup.SimpleTestCaseSimpleTestCase is used when no database interaction is needed.TestCase -> Option BTestCase [OK]SimpleTestCase in a Django test file?SimpleTestCase in the django.test module.from module import ClassName.from django.test import SimpleTestCase
class MyTests(SimpleTestCase):
def test_addition(self):
self.assertEqual(2 + 3, 5)
def test_database(self):
from myapp.models import Item
self.assertEqual(Item.objects.count(), 0)SimpleTestCase behaviorSimpleTestCase does not set up the database, so database queries will fail.test_addition is a simple math check and will pass. test_database tries to query the database, causing an error.TestCase but your tests fail with errors about database access. What is a likely cause?TestCase requires the test database to be migrated before running tests.TestCase is used.SimpleTestCase is faster and sufficient.TestCase for database testsTestCase to set up the test database.