From d78e2ae3557feb1c9374cc56f6c130b13a7ef789 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 1 Sep 2006 13:33:26 +0000 Subject: [PATCH] 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 --- django/conf/global_settings.py | 7 ++++++- django/test/utils.py | 5 ++++- docs/settings.txt | 12 ++++++++++++ docs/testing.txt | 17 ++++++++++------- tests/runtests.py | 10 +++++++--- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 56a4ccb48a..c382d7bf96 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -301,4 +301,9 @@ AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',) # TESTING # ########### -TEST_RUNNER='django.test.simple.run_tests' +# The name of the method to use to invoke the test suite +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 diff --git a/django/test/utils.py b/django/test/utils.py index be011b864c..6d6827918d 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -21,7 +21,10 @@ def create_test_db(verbosity=1, autoclobber=False): if settings.DATABASE_ENGINE == "sqlite3": TEST_DATABASE_NAME = ":memory:" else: - TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME + if settings.TEST_DATABASE_NAME: + TEST_DATABASE_NAME = settings.TEST_DATABASE_NAME + else: + TEST_DATABASE_NAME = TEST_DATABASE_PREFIX + settings.DATABASE_NAME # Create the test database and connect to it. We need to autocommit # if the database supports it because PostgreSQL doesn't allow diff --git a/docs/settings.txt b/docs/settings.txt index 6dabeeb71d..b927b62ca7 100644 --- a/docs/settings.txt +++ b/docs/settings.txt @@ -766,6 +766,18 @@ The name of the method to use for starting the test suite. See .. _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 ----------- diff --git a/docs/testing.txt b/docs/testing.txt index 1ab3a41ba6..98ed1e8aec 100644 --- a/docs/testing.txt +++ b/docs/testing.txt @@ -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 used to validate that code behaves as expected. When refactoring or 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 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 database is created and models are initialized. This test database is -created from scratch every time you run your tests. The test database -gets its name by prepending ``test_`` to the database name specified by -``settings.DATABASE_NAME``; all other database settings will the same as -they would be for the project normally. +created from scratch every time you run your tests. + +By default, the test database gets its name by prepending ``test_`` to +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. 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 tested. This is the same format returned by ``django.db.models.get_apps()`` -Verbosity determines the amount of debug information that will be -provided to the console; '0' is no output, '1' is normal output, +Verbosity determines the amount of notification and debug information that +will be printed to the console; '0' is no output, '1' is normal output, and `2` is verbose output. Testing utilities diff --git a/tests/runtests.py b/tests/runtests.py index 57087ef3a7..95ec237ee4 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -5,6 +5,7 @@ import unittest MODEL_TESTS_DIR_NAME = 'modeltests' 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) 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.db.models.loading import get_apps, load_app old_installed_apps = settings.INSTALLED_APPS + old_test_database_name = settings.TEST_DATABASE_NAME - # load all the ALWAYS_INSTALLED_APPS + settings.TEST_DATABASE_NAME = TEST_DATABASE_NAME settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS + + # load all the ALWAYS_INSTALLED_APPS get_apps() test_models = [] @@ -105,9 +109,9 @@ def django_tests(verbosity, tests_to_run): from django.test.simple import run_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.TESTS_DATABASE_NAME = old_test_database_name if __name__ == "__main__": from optparse import OptionParser usage = "%prog [options] [model model model ...]"