From c80b80f3aeaeedbc91d44c3db17dc631ae8a919e Mon Sep 17 00:00:00 2001
From: Nicolas Trangez <ikke@nicolast.be>
Date: Wed, 26 Sep 2007 13:35:57 +0200
Subject: [PATCH] Implement TEST_FIXTURES feature

Setting TEST_FIXTURES to a tuple listing some fixture names will
auto-load these fixtures in the test database before running any
tests. This is especially useful when using doctests, which don't
allow to define a fixture to load, unlike unittests.
---
 django/conf/global_settings.py |    4 ++++
 django/test/simple.py          |    4 ++--
 django/test/utils.py           |    7 +++++++
 docs/settings.txt              |   10 ++++++++++
 4 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
index b3cbf09..8e7df95 100644
--- a/django/conf/global_settings.py
+++ b/django/conf/global_settings.py
@@ -350,6 +350,10 @@ TEST_DATABASE_NAME = None
 TEST_DATABASE_CHARSET = None
 TEST_DATABASE_COLLATION = None
 
+# The name of a fixture to load in the test database before
+# running tests
+TEST_FIXTURES = None
+
 ############
 # FIXTURES #
 ############
diff --git a/django/test/simple.py b/django/test/simple.py
index 6fa381a..5d26278 100644
--- a/django/test/simple.py
+++ b/django/test/simple.py
@@ -3,7 +3,7 @@ from django.conf import settings
 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.utils import create_test_db, destroy_test_db
+from django.test.utils import create_test_db, populate_test_db, destroy_test_db
 from django.test.testcases import OutputChecker, DocTestRunner
 
 # The module name for tests outside models.py
@@ -140,10 +140,10 @@ def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
 
     old_name = settings.DATABASE_NAME
     create_test_db(verbosity, autoclobber=not interactive)
+    populate_test_db(settings.TEST_FIXTURES)
     result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
     destroy_test_db(old_name, verbosity)
     
     teardown_test_environment()
     
     return len(result.failures) + len(result.errors)
-    
\ No newline at end of file
diff --git a/django/test/utils.py b/django/test/utils.py
index 6edd186..4a19f81 100644
--- a/django/test/utils.py
+++ b/django/test/utils.py
@@ -161,6 +161,13 @@ def create_test_db(verbosity=1, autoclobber=False):
 
     return TEST_DATABASE_NAME
 
+def populate_test_db(fixtures):
+    if not fixtures:
+        return
+
+    # Load test fixture in DB
+    call_command('loaddata', *fixtures, **{'verbosity': 0})
+
 def destroy_test_db(old_database_name, verbosity=1):
     # If the database wants to drop the test DB itself, let it
     creation_module = get_creation_module()
diff --git a/docs/settings.txt b/docs/settings.txt
index 7ad1f64..af6c906 100644
--- a/docs/settings.txt
+++ b/docs/settings.txt
@@ -910,6 +910,16 @@ of the MySQL manual for details).
 
 .. _section 10.3.2: http://www.mysql.org/doc/refman/5.0/en/charset-database.html
 
+TEST_FIXTURES
+-------------
+
+**New in Django development version **
+
+Default: ``None``
+
+The fixtures to load in the test database before running any tests. This is a
+tuple giving the name of all fixtures to load.
+
 TEST_DATABASE_NAME
 ------------------
 
-- 
1.5.3.2

