[Fixed]-Django test: TransactionManagementError: You can't execute queries until the end of the 'atomic' block

35👍

Apparently, moving from django.test.TestCase to django.test.TransactionTestCase solved the issue. Here are some important points regarding the differences between django.test.TestCase and django.test.TransactionTestCase:

TransactionTestCase and TestCase are identical except for the manner in which the database is reset to a known state and the ability for test code to test the effects of commit and rollback:

  • TransactionTestCase resets the database after the test runs by truncating all tables. A TransactionTestCase may call commit and rollback and observe the effects of these calls on the database.

  • 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.

Here you can find more details from the docs TransactionTestCase

👤hnroot

Leave a comment