[Django]-Django data between tests

6👍

From the documentation, the bottom part explaining the difference between TransactionTestCase and TestCase:

A TestCase, on the other hand, does not truncate tables after a test. Instead, it encloses the test code in a database transaction that is rolled back at the end of the test. This guarantees that the rollback at the end of the test restores the database to its initial state.

Since each individual test method is wrapped in an atomic block, and each atomic block is rolled back at the end of the test method, this is expected behaviour. Django does this to ensure that tests are isolated, making it easier to find any problems when they occur.

If you need data to persists in multiple test methods, you can create them in setUpTestData(). Just make sure not to change the in-memory objects created by setUpTestData(). The in-memory changes will persists, but the database changes will be rolled back.

👤knbk

Leave a comment