Refs #2333 - Added a TEST_DATABASE_NAME setting that can be used to override the 'test_' + DATABASE_NAME naming policy. This setting is then used in runtests.py to restore the use of 'django_test_db' as the Django model/regression test database. Thanks to Michael Radziej for the feedback.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3706 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2006-09-01 13:33:26 +00:00
parent 60ebb616a9
commit d78e2ae355
5 changed files with 39 additions and 12 deletions

View File

@ -301,4 +301,9 @@ AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)
# TESTING # # TESTING #
########### ###########
# The name of the method to use to invoke the test suite
TEST_RUNNER = 'django.test.simple.run_tests' TEST_RUNNER = 'django.test.simple.run_tests'
# The name of the database to use for testing purposes.
# If None, a name of 'test_' + DATABASE_NAME will be assumed
TEST_DATABASE_NAME = None

View File

@ -20,6 +20,9 @@ def create_test_db(verbosity=1, autoclobber=False):
# in-memory database. # in-memory database.
if settings.DATABASE_ENGINE == "sqlite3": if settings.DATABASE_ENGINE == "sqlite3":
TEST_DATABASE_NAME = ":memory:" TEST_DATABASE_NAME = ":memory:"
else:
if settings.TEST_DATABASE_NAME:
TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME
else: else:
TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME

View File

@ -766,6 +766,18 @@ The name of the method to use for starting the test suite. See
.. _Testing Django Applications: ../testing/ .. _Testing Django Applications: ../testing/
TEST_DATABASE_NAME
------------------
**New in Django development version**
Default: ``None``
The name of database to use when running the test suite. If a value of
``None`` is specified, the test database will use the name ``'test_' + settings.DATABASE_NAME``. See `Testing Django Applications`_.
.. _Testing Django Applications: ../testing/
TIME_FORMAT TIME_FORMAT
----------- -----------

View File

@ -8,7 +8,7 @@ Automated testing is an extremely useful weapon in the bug-killing arsenal
of the modern developer. When initially writing code, a test suite can be of the modern developer. When initially writing code, a test suite can be
used to validate that code behaves as expected. When refactoring or used to validate that code behaves as expected. When refactoring or
modifying code, tests serve as a guide to ensure that behavior hasn't modifying code, tests serve as a guide to ensure that behavior hasn't
changed as a result of the refactor. changed unexpectedly as a result of the refactor.
Testing an web application is a complex task, as there are many Testing an web application is a complex task, as there are many
components of a web application that must be validated and tested. To components of a web application that must be validated and tested. To
@ -189,10 +189,13 @@ but you only want to run the animals unit tests, run::
When you run your tests, you'll see a bunch of text flow by as the test When you run your tests, you'll see a bunch of text flow by as the test
database is created and models are initialized. This test database is database is created and models are initialized. This test database is
created from scratch every time you run your tests. The test database created from scratch every time you run your tests.
gets its name by prepending ``test_`` to the database name specified by
``settings.DATABASE_NAME``; all other database settings will the same as By default, the test database gets its name by prepending ``test_`` to
they would be for the project normally. the database name specified by the ``DATABASE_NAME`` setting; all other
database settings will the same as they would be for the project normally.
If you wish to use a name other than the default for the test database,
you can use the ``TEST_DATABASE_NAME`` setting to provide a name.
Once the test database has been established, Django will run your tests. Once the test database has been established, Django will run your tests.
If everything goes well, at the end you'll see:: If everything goes well, at the end you'll see::
@ -265,8 +268,8 @@ arguments:
The module list is the list of Python modules that contain the models to be The module list is the list of Python modules that contain the models to be
tested. This is the same format returned by ``django.db.models.get_apps()`` tested. This is the same format returned by ``django.db.models.get_apps()``
Verbosity determines the amount of debug information that will be Verbosity determines the amount of notification and debug information that
provided to the console; '0' is no output, '1' is normal output, will be printed to the console; '0' is no output, '1' is normal output,
and `2` is verbose output. and `2` is verbose output.
Testing utilities Testing utilities

View File

@ -5,6 +5,7 @@ import unittest
MODEL_TESTS_DIR_NAME = 'modeltests' MODEL_TESTS_DIR_NAME = 'modeltests'
REGRESSION_TESTS_DIR_NAME = 'regressiontests' REGRESSION_TESTS_DIR_NAME = 'regressiontests'
TEST_DATABASE_NAME = 'django_test_db'
MODEL_TEST_DIR = os.path.join(os.path.dirname(__file__), MODEL_TESTS_DIR_NAME) MODEL_TEST_DIR = os.path.join(os.path.dirname(__file__), MODEL_TESTS_DIR_NAME)
REGRESSION_TEST_DIR = os.path.join(os.path.dirname(__file__), REGRESSION_TESTS_DIR_NAME) REGRESSION_TEST_DIR = os.path.join(os.path.dirname(__file__), REGRESSION_TESTS_DIR_NAME)
@ -71,9 +72,12 @@ def django_tests(verbosity, tests_to_run):
from django.conf import settings from django.conf import settings
from django.db.models.loading import get_apps, load_app from django.db.models.loading import get_apps, load_app
old_installed_apps = settings.INSTALLED_APPS old_installed_apps = settings.INSTALLED_APPS
old_test_database_name = settings.TEST_DATABASE_NAME
settings.TEST_DATABASE_NAME = TEST_DATABASE_NAME
settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
# load all the ALWAYS_INSTALLED_APPS # load all the ALWAYS_INSTALLED_APPS
settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
get_apps() get_apps()
test_models = [] test_models = []
@ -105,9 +109,9 @@ def django_tests(verbosity, tests_to_run):
from django.test.simple import run_tests from django.test.simple import run_tests
run_tests(test_models, verbosity, extra_tests=extra_tests) run_tests(test_models, verbosity, extra_tests=extra_tests)
# Restore the old INSTALLED_APPS setting # Restore the old settings
settings.INSTALLED_APPS = old_installed_apps settings.INSTALLED_APPS = old_installed_apps
settings.TESTS_DATABASE_NAME = old_test_database_name
if __name__ == "__main__": if __name__ == "__main__":
from optparse import OptionParser from optparse import OptionParser
usage = "%prog [options] [model model model ...]" usage = "%prog [options] [model model model ...]"