From 9c55bbdef7f1717aa17f7ca17cb2396522426310 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 16 Aug 2007 23:05:00 +0000 Subject: [PATCH] Added 'django-admin.py testserver' command and docs git-svn-id: http://code.djangoproject.com/svn/django/trunk@5912 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/commands/testserver.py | 26 ++++++++++++ docs/django-admin.txt | 42 ++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 django/core/management/commands/testserver.py diff --git a/django/core/management/commands/testserver.py b/django/core/management/commands/testserver.py new file mode 100644 index 0000000000..907960894c --- /dev/null +++ b/django/core/management/commands/testserver.py @@ -0,0 +1,26 @@ +from django.core.management.base import BaseCommand + +class Command(BaseCommand): + help = 'Runs a development server with data from the given fixture(s).' + args = '[fixture ...]' + + requires_model_validation = False + + def handle(self, *fixture_labels, **options): + from django.conf import settings + from django.core.management import call_command + from django.test.utils import create_test_db + + verbosity = int(options.get('verbosity', 1)) + + # Create a test database. + db_name = create_test_db(verbosity=verbosity) + + # Import the fixture data into the test database. + call_command('loaddata', *fixture_labels, **{'verbosity': verbosity}) + + # Run the development server. Turn off auto-reloading because it causes + # a strange error -- it causes this handle() method to be called + # multiple times. + shutdown_message = '\nServer stopped.\nNote that the test database, %r, has not been deleted. You can explore it on your own.' % db_name + call_command('runserver', shutdown_message=shutdown_message, use_reloader=False) diff --git a/docs/django-admin.txt b/docs/django-admin.txt index ad17907b5e..aea990c5dc 100644 --- a/docs/django-admin.txt +++ b/docs/django-admin.txt @@ -235,6 +235,7 @@ The ``dumpdata`` command can be used to generate input for ``loaddata``. reset [appname appname ...] --------------------------- + Executes the equivalent of ``sqlreset`` for the given appnames. runfcgi [options] @@ -426,7 +427,46 @@ test Discover and run tests for all installed models. See `Testing Django applications`_ for more information. -.. _testing django applications: ../testing/ +.. _testing Django applications: ../testing/ + +testserver [fixture fixture ...] +-------------------------------- + +**New in Django development version** + +Runs a Django development server (as in ``runserver``) using data from the +given fixture(s). + +For example, this command:: + + django-admin.py testserver mydata.json + +...would perform the following steps: + + 1. Create a test database, as described in `testing Django applications`_. + 2. Populate the test database with fixture data from the given fixtures. + (For more on fixtures, see the documentation for ``loaddata`` above.) + 3. Runs the Django development server (as in ``runserver``), pointed at + this newly created test database instead of your production database. + +This is useful in a number of ways: + + * When you're writing `unit tests`_ of how your views act with certain + fixture data, you can use ``testserver`` to interact with the views in + a Web browser, manually. + + * Let's say you're developing your Django application and have a "pristine" + copy of a database that you'd like to interact with. You can dump your + database to a fixture (using the ``dumpdata`` command, explained above), + then use ``testserver`` to run your Web application with that data. With + this arrangement, you have the flexibility of messing up your data + in any way, knowing that whatever data changes you're making are only + being made to a test database. + +Note that this server can only run on the default port on localhost; it does +not yet accept a ``host`` or ``port`` parameter. + +.. _unit tests: ../testing/ validate --------