Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#20142 closed Bug (fixed)

Transaction methods are not re-enabled when fixture loading fails

Reported by: Martin Larente Owned by: Filipa Andrade
Component: Testing framework Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


When a test case fails to properly load a fixture (integrity error or some other reason), the transaction methods (which are disabled before the test is run) are not re-enabled before the next test is run. This can cause other tests (especially transaction test cases) to fail.

I've created a simple django application to reproduce this. You just have to add the application to a Django project, and then run: test myapp.Before myapp.Broken myapp.After

This command is used to enforce the test order that shows the bug. The Before and After tests are actually the same test that checks if transaction methods are enabled (as they should be for a transaction test case). The first one passes and the other one fails:

import django.db.transaction
from django.test.testcases import TestCase
from django.test.testcases import TransactionTestCase

ORIGINAL = django.db.transaction.enter_transaction_management

class TransactionCheckMixin(object):
    def test_transaction_method(self):
        self.assertEqual(ORIGINAL, django.db.transaction.enter_transaction_management)

class Before(TransactionCheckMixin, TransactionTestCase):

class Broken(TestCase):
    fixtures = ['broken_fixture']

    def test_something(self):
        self.assertTrue(True)  # will not happen

class After(TransactionCheckMixin, TransactionTestCase):

The problem lies in the _fixture_setup method of the TestCase class:

class TestCase(TransactionTestCase):
    Does basically the same as TransactionTestCase, but surrounds every test
    with a transaction, monkey-patches the real transaction management routines
    to do nothing, and rollsback the test transaction at the end of the test.
    You have to use TransactionTestCase, if you need transaction management
    inside a test.

    def _fixture_setup(self):
        if not connections_support_transactions():
            return super(TestCase, self)._fixture_setup()

        assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances'

        for db_name in self._databases_names():
            transaction.managed(True, using=db_name)

        from django.contrib.sites.models import Site

        for db in self._databases_names(include_mirrors=False):
            if hasattr(self, 'fixtures'):
                call_command('loaddata', *self.fixtures,
                                'verbosity': 0,
                                'commit': False,
                                'database': db,
                                'skip_validation': True,

It should handle errors and call the _fixture_teardown method if there's an error.

Attachments (1) (1.7 KB) - added by Martin Larente 5 years ago.
Simple app that show the bug

Download all attachments as: .zip

Change History (5)

Changed 5 years ago by Martin Larente

Attachment: added

Simple app that show the bug

comment:1 Changed 5 years ago by Filipa Andrade

Owner: changed from nobody to Filipa Andrade
Status: newassigned
Triage Stage: UnreviewedAccepted

Confirmed the bug in django 1.5 and development version.

Last edited 5 years ago by Filipa Andrade (previous) (diff)

comment:2 Changed 5 years ago by Filipa Andrade

Easy pickings: set
Has patch: set

This fix does not contain tests as it is impossible to test it without mocking and a decision about using mocks in the project as yet not been made. This was discussed and agreed with Russel.

comment:3 Changed 5 years ago by Filipa Andrade <filipa.andrade@…>

Resolution: fixed
Status: assignedclosed

In 5883ae56b3e993052c46f7d9e756465b1387dd34:

Fixed #20142 -- Added error handling for fixture setup

TestCase._fixture_setup disables transactions so,
in case of an error, cleanup is needed to re-enable
transactions, otherwise following TransactionTestCase
would fail.

comment:4 Changed 5 years ago by Aymeric Augustin <aymeric.augustin@…>

In 5915800debef7cec01672dc0b467b7c2bd3aba03:

Merge pull request #1127 from filias/20142

Fixed #20142 -- Added error handling for fixture setup

Note: See TracTickets for help on using tickets.
Back to Top