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
|
b
|
TEST_DATABASE_NAME = None
|
350 | 350 | TEST_DATABASE_CHARSET = None |
351 | 351 | TEST_DATABASE_COLLATION = None |
352 | 352 | |
| 353 | # The name of a fixture to load in the test database before |
| 354 | # running tests |
| 355 | TEST_FIXTURES = None |
| 356 | |
353 | 357 | ############ |
354 | 358 | # FIXTURES # |
355 | 359 | ############ |
diff --git a/django/test/simple.py b/django/test/simple.py
index 6fa381a..5d26278 100644
a
|
b
|
from django.conf import settings
|
3 | 3 | from django.db.models import get_app, get_apps |
4 | 4 | from django.test import _doctest as doctest |
5 | 5 | from django.test.utils import setup_test_environment, teardown_test_environment |
6 | | from django.test.utils import create_test_db, destroy_test_db |
| 6 | from django.test.utils import create_test_db, populate_test_db, destroy_test_db |
7 | 7 | from django.test.testcases import OutputChecker, DocTestRunner |
8 | 8 | |
9 | 9 | # The module name for tests outside models.py |
… |
… |
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
|
140 | 140 | |
141 | 141 | old_name = settings.DATABASE_NAME |
142 | 142 | create_test_db(verbosity, autoclobber=not interactive) |
| 143 | populate_test_db(settings.TEST_FIXTURES) |
143 | 144 | result = unittest.TextTestRunner(verbosity=verbosity).run(suite) |
144 | 145 | destroy_test_db(old_name, verbosity) |
145 | 146 | |
146 | 147 | teardown_test_environment() |
147 | 148 | |
148 | 149 | return len(result.failures) + len(result.errors) |
149 | | |
150 | | No newline at end of file |
diff --git a/django/test/utils.py b/django/test/utils.py
index 6edd186..4a19f81 100644
a
|
b
|
def create_test_db(verbosity=1, autoclobber=False):
|
161 | 161 | |
162 | 162 | return TEST_DATABASE_NAME |
163 | 163 | |
| 164 | def populate_test_db(fixtures): |
| 165 | if not fixtures: |
| 166 | return |
| 167 | |
| 168 | # Load test fixture in DB |
| 169 | call_command('loaddata', *fixtures, **{'verbosity': 0}) |
| 170 | |
164 | 171 | def destroy_test_db(old_database_name, verbosity=1): |
165 | 172 | # If the database wants to drop the test DB itself, let it |
166 | 173 | creation_module = get_creation_module() |
diff --git a/docs/settings.txt b/docs/settings.txt
index 7ad1f64..af6c906 100644
a
|
b
|
of the MySQL manual for details).
|
910 | 910 | |
911 | 911 | .. _section 10.3.2: http://www.mysql.org/doc/refman/5.0/en/charset-database.html |
912 | 912 | |
| 913 | TEST_FIXTURES |
| 914 | ------------- |
| 915 | |
| 916 | **New in Django development version ** |
| 917 | |
| 918 | Default: ``None`` |
| 919 | |
| 920 | The fixtures to load in the test database before running any tests. This is a |
| 921 | tuple giving the name of all fixtures to load. |
| 922 | |
913 | 923 | TEST_DATABASE_NAME |
914 | 924 | ------------------ |
915 | 925 | |