Django TransactionTestCase with rollback emulation Django TransactionTestCase with rollback emulation postgresql postgresql

Django TransactionTestCase with rollback emulation


This issue is really well explained in django related ticket: https://code.djangoproject.com/ticket/23727

Quoted from there:

When using a TransactionTestCase with serialized_rollback=True, after creating the database and running its migrations (along with emitting the post_migrate signal), the contents of the database are serialized to _test_serialized_contents.

After the first test case, _fixture_teardown() would flush the tables but then the post_migrate signal would be emitted and new rows (with new PKs) would be created in the django_content_type table.

Then in any subsequent test cases in a suite, _fixture_setup() attempts to deserialize the content of _test_serialized_contents, but these rows are identical to the rows already in the database except for their PKs.

This causes an IntegrityError due to the unique constraint in the django_content_type table.

A patch has been created/released, but only for Django 1.9.x.

For previous versions, you should continue to use TEST_NON_SERIALIZED_APPS I guess...