What does Django's `TestCase` give you that `unittest.TestCase` does not?
How to write and run unit tests with Django TestCase, and what it adds over unittest.TestCase. The test runner creates a separate test database from your migrations, applies them, and destroys it at the end, so tests never touch development data. Isolation between test methods comes from transactions rather than truncation: TestCase opens an atomic block before each test and rolls it back afterwards, so every test starts from the same state, no cleanup code is required, and the suite stays fast. Explains setUpTestData, which runs once per class inside an outer transaction so shared fixture rows are created a single time while each test still runs in its own nested block, and why objects it assigns are copied per test so an in-memory mutation in one test cannot leak into the next. Includes runnable Django code and real output for test collection, rollback isolation, and hook call counts, plus TransactionTestCase, SimpleTestCase, assertNumQueries, and alphabetical test ordering.
Comentarii