Browse files

Fixed #8138 -- Changed django.test.TestCase to rollback tests (when t…

…he database supports it) instead of flushing and reloading the database. This can substantially reduce the time it takes to run large test suites.

This change may be slightly backwards incompatible, if existing tests need to test transactional behavior, or if they rely on invalid assumptions or a specific test case ordering.  For the first case, django.test.TransactionTestCase should be used.  TransactionTestCase is also a quick fix to get around test case errors revealed by the new rollback approach, but a better long-term fix is to correct the test case.  See the testing doc for full details.

Many thanks to:
* Marc Remolt for the initial proposal and implementation.
* Luke Plant for initial testing and improving the implementation.
* Ramiro Morales for feedback and help with tracking down a mysterious PostgreSQL issue.
* Eric Holscher for feedback regarding the effect of the change on the Ellington testsuite.
* Russell Keith-Magee for guidance and feedback from beginning to end.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent f9f9d70 commit 344f16e2205a4959ba65d975716a38db77d2061e @kmtracey kmtracey committed Jan 16, 2009
@@ -334,6 +334,7 @@ answer newbie questions, and generally made Django that much better:
Massimiliano Ravelli <>
Brian Ray <>
+ Marc Remolt <>
David Reynolds <>
@@ -311,7 +311,8 @@ def create_test_db(self, verbosity=1, autoclobber=False):
settings.DATABASE_NAME = test_database_name
+ settings.DATABASE_SUPPORTS_TRANSACTIONS = self._rollback_works()
call_command('syncdb', verbosity=verbosity, interactive=False)
if settings.CACHE_BACKEND.startswith('db://'):
@@ -362,7 +363,19 @@ def _create_test_db(self, verbosity, autoclobber):
return test_database_name
+ def _rollback_works(self):
+ cursor = self.connection.cursor()
+ cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
+ self.connection._commit()
+ cursor.execute('INSERT INTO ROLLBACK_TEST (X) VALUES (8)')
+ self.connection._rollback()
+ count, = cursor.fetchone()
+ cursor.execute('DROP TABLE ROLLBACK_TEST')
+ self.connection._commit()
+ return count == 0
def destroy_test_db(self, old_database_name, verbosity=1):
Destroy a test database, prompting the user for confirmation if the
@@ -3,4 +3,4 @@
from django.test.client import Client
-from django.test.testcases import TestCase
+from django.test.testcases import TestCase, TransactionTestCase
@@ -19,6 +19,7 @@
from django.utils.encoding import smart_str
from django.utils.http import urlencode
from django.utils.itercompat import is_iterable
+from django.db import transaction, close_connection
MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
@@ -69,7 +70,9 @@ def __call__(self, environ):
response = middleware_method(request, response)
response = self.apply_response_fixes(request, response)
+ signals.request_finished.disconnect(close_connection)
+ signals.request_finished.connect(close_connection)
return response
@@ -3,7 +3,7 @@
from django.db.models import get_app, get_apps
from django.test import _doctest as doctest
from django.test.utils import setup_test_environment, teardown_test_environment
-from django.test.testcases import OutputChecker, DocTestRunner
+from django.test.testcases import OutputChecker, DocTestRunner, TestCase
# The module name for tests outside
TEST_MODULE = 'tests'
@@ -99,6 +99,43 @@ def build_test(label):
else: # label is app.TestClass.test_method
return TestClass(parts[2])
+def partition_suite(suite, classes, bins):
+ """
+ Partitions a test suite by test type.
+ classes is a sequence of types
+ bins is a sequence of TestSuites, one more than classes
+ Tests of type classes[i] are added to bins[i],
+ tests with no match found in classes are place in bins[-1]
+ """
+ for test in suite:
+ if isinstance(test, unittest.TestSuite):
+ partition_suite(test, classes, bins)
+ else:
+ for i in range(len(classes)):
+ if isinstance(test, classes[i]):
+ bins[i].addTest(test)
+ break
+ else:
+ bins[-1].addTest(test)
+def reorder_suite(suite, classes):
+ """
+ Reorders a test suite by test type.
+ classes is a sequence of types
+ All tests of type clases[0] are placed first, then tests of type classes[1], etc.
+ Tests with no match in classes are placed last.
+ """
+ class_count = len(classes)
+ bins = [unittest.TestSuite() for i in range(class_count+1)]
+ partition_suite(suite, classes, bins)
+ for i in range(class_count):
+ bins[0].addTests(bins[i+1])
+ return bins[0]
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
Run the unit tests for all the test labels in the provided list.
@@ -137,6 +174,8 @@ def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
for test in extra_tests:
+ suite = reorder_suite(suite, (TestCase,))
old_name = settings.DATABASE_NAME
from django.db import connection
connection.creation.create_test_db(verbosity, autoclobber=not interactive)
@@ -7,7 +7,7 @@
from django.core import mail
from import call_command
from django.core.urlresolvers import clear_url_caches
-from django.db import transaction
+from django.db import transaction, connection
from django.http import QueryDict
from django.test import _doctest as doctest
from django.test.client import Client
@@ -27,6 +27,31 @@ def to_list(value):
value = [value]
return value
+real_commit = transaction.commit
+real_rollback = transaction.rollback
+real_enter_transaction_management = transaction.enter_transaction_management
+real_leave_transaction_management = transaction.leave_transaction_management
+real_savepoint_commit = transaction.savepoint_commit
+real_savepoint_rollback = transaction.savepoint_rollback
+def nop(x=None):
+ return
+def disable_transaction_methods():
+ transaction.commit = nop
+ transaction.rollback = nop
+ transaction.savepoint_commit = nop
+ transaction.savepoint_rollback = nop
+ transaction.enter_transaction_management = nop
+ transaction.leave_transaction_management = nop
+def restore_transaction_methods():
+ transaction.commit = real_commit
+ transaction.rollback = real_rollback
+ transaction.savepoint_commit = real_savepoint_commit
+ transaction.savepoint_rollback = real_savepoint_rollback
+ transaction.enter_transaction_management = real_enter_transaction_management
+ transaction.leave_transaction_management = real_leave_transaction_management
class OutputChecker(doctest.OutputChecker):
def check_output(self, want, got, optionflags):
@@ -173,8 +198,8 @@ def report_unexpected_exception(self, out, test, example, exc_info):
# Rollback, in case of database errors. Otherwise they'd have
# side effects on other tests.
-class TestCase(unittest.TestCase):
+class TransactionTestCase(unittest.TestCase):
def _pre_setup(self):
"""Performs any pre-test setup. This includes:
@@ -185,16 +210,22 @@ def _pre_setup(self):
* Clearing the mail test outbox.
+ self._fixture_setup()
+ self._urlconf_setup()
+ mail.outbox = []
+ def _fixture_setup(self):
call_command('flush', verbosity=0, interactive=False)
if hasattr(self, 'fixtures'):
# We have to use this slightly awkward syntax due to the fact
# that we're using *args and **kwargs together.
call_command('loaddata', *self.fixtures, **{'verbosity': 0})
+ def _urlconf_setup(self):
if hasattr(self, 'urls'):
self._old_root_urlconf = settings.ROOT_URLCONF
settings.ROOT_URLCONF = self.urls
- mail.outbox = []
def __call__(self, result=None):
@@ -211,7 +242,7 @@ def __call__(self, result=None):
import sys
result.addError(self, sys.exc_info())
- super(TestCase, self).__call__(result)
+ super(TransactionTestCase, self).__call__(result)
except (KeyboardInterrupt, SystemExit):
@@ -226,6 +257,13 @@ def _post_teardown(self):
* Putting back the original ROOT_URLCONF if it was changed.
+ self._fixture_teardown()
+ self._urlconf_teardown()
+ def _fixture_teardown(self):
+ pass
+ def _urlconf_teardown(self):
if hasattr(self, '_old_root_urlconf'):
settings.ROOT_URLCONF = self._old_root_urlconf
@@ -359,3 +397,37 @@ def assertTemplateNotUsed(self, response, template_name):
self.failIf(template_name in template_names,
(u"Template '%s' was used unexpectedly in rendering the"
u" response") % template_name)
+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):
+ return super(TestCase, self)._fixture_setup()
+ transaction.enter_transaction_management()
+ transaction.managed(True)
+ disable_transaction_methods()
+ from django.contrib.sites.models import Site
+ Site.objects.clear_cache()
+ if hasattr(self, 'fixtures'):
+ call_command('loaddata', *self.fixtures, **{
+ 'verbosity': 0,
+ 'commit': False
+ })
+ def _fixture_teardown(self):
+ return super(TestCase, self)._fixture_teardown()
+ restore_transaction_methods()
+ transaction.rollback()
+ transaction.leave_transaction_management()
+ connection.close()
@@ -785,6 +785,52 @@ just change the base class of your test from ``unittest.TestCase`` to
will continue to be available, but it will be augmented with some useful
+.. versionadded:: 1.1
+.. class:: TransactionTestCase()
+Django ``TestCase`` classes make use of database transaction facilities, if
+available, to speed up the process of resetting the database to a known state
+at the beginning of each test. A consequence of this, however, is that the
+effects of transaction commit and rollback cannot be tested by a Django
+``TestCase`` class. If your test requires testing of such transactional
+behavior, you should use a Django ``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. A ``TranscationTestCase`` resets
+the database before the test runs by truncating all tables and reloading
+initial data. 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 and reload initial
+data at the beginning of a test. Instead, it encloses the test code in a
+database transaction that is rolled back at the end of the test. It also
+prevents the code under test from issuing any commit or rollback operations
+on the database, to ensure that the rollback at the end of the test restores
+the database to its initial state. In order to guarantee that all ``TestCase``
+code starts with a clean database, the Django test runner runs all ``TestCase``
+tests first, before any other tests (e.g. doctests) that may alter the
+database without restoring it to its original state.
+When running on a database that does not support rollback (e.g. MySQL with the
+MyISAM storage engine), ``TestCase`` falls back to initializing the database
+by truncating tables and reloading initial data.
+.. note::
+ The ``TestCase`` use of rollback to un-do the effects of the test code
+ may reveal previously-undetected errors in test code. For example,
+ test code that assumes primary keys values will be assigned starting at
+ one may find that assumption no longer holds true when rollbacks instead
+ of table truncation are being used to reset the database. Similarly,
+ the reordering of tests so that all ``TestCase`` classes run first may
+ reveal unexpected dependencies on test case ordering. In such cases a
+ quick fix is to switch the ``TestCase`` to a ``TransactionTestCase``.
+ A better long-term fix, that allows the test to take advantage of the
+ speed benefit of ``TestCase``, is to fix the underlying test problem.
Default test client
@@ -1,10 +1,13 @@
+import re
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.comments import signals
from django.contrib.comments.models import Comment
from regressiontests.comment_tests.models import Article
from regressiontests.comment_tests.tests import CommentTestCase
+post_redirect_re = re.compile(r'^http://testserver/posted/\?c=(?P<pk>\d+$)')
class CommentViewTests(CommentTestCase):
def testPostCommentHTTPMethods(self):
@@ -181,18 +184,26 @@ def testCommentNext(self):
a = Article.objects.get(pk=1)
data = self.getValidData(a)
response ="/post/", data)
- self.assertEqual(response["Location"], "http://testserver/posted/?c=1")
+ location = response["Location"]
+ match = post_redirect_re.match(location)
+ self.failUnless(match != None, "Unexpected redirect location: %s" % location)
data["next"] = "/somewhere/else/"
data["comment"] = "This is another comment"
response ="/post/", data)
- self.assertEqual(response["Location"], "http://testserver/somewhere/else/?c=2")
+ location = response["Location"]
+ match ="^http://testserver/somewhere/else/\?c=\d+$", location)
+ self.failUnless(match != None, "Unexpected redirect location: %s" % location)
def testCommentDoneView(self):
a = Article.objects.get(pk=1)
data = self.getValidData(a)
response ="/post/", data)
- response = self.client.get("/posted/", {'c':1})
+ location = response["Location"]
+ match = post_redirect_re.match(location)
+ self.failUnless(match != None, "Unexpected redirect location: %s" % location)
+ pk = int('pk'))
+ response = self.client.get(location)
self.assertTemplateUsed(response, "comments/posted.html")
- self.assertEqual(response.context[0]["comment"], Comment.objects.get(pk=1))
+ self.assertEqual(response.context[0]["comment"], Comment.objects.get(pk=pk))
Oops, something went wrong.

0 comments on commit 344f16e

Please sign in to comment.