Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added 'django-admin.py testserver' command and docs

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5912 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9c55bbdef7f1717aa17f7ca17cb2396522426310 1 parent a5400cf
Adrian Holovaty authored August 16, 2007
26  django/core/management/commands/testserver.py
... ...
@@ -0,0 +1,26 @@
  1
+from django.core.management.base import BaseCommand
  2
+
  3
+class Command(BaseCommand):
  4
+    help = 'Runs a development server with data from the given fixture(s).'
  5
+    args = '[fixture ...]'
  6
+
  7
+    requires_model_validation = False
  8
+
  9
+    def handle(self, *fixture_labels, **options):
  10
+        from django.conf import settings
  11
+        from django.core.management import call_command
  12
+        from django.test.utils import create_test_db
  13
+
  14
+        verbosity = int(options.get('verbosity', 1))
  15
+
  16
+        # Create a test database.
  17
+        db_name = create_test_db(verbosity=verbosity)
  18
+
  19
+        # Import the fixture data into the test database.
  20
+        call_command('loaddata', *fixture_labels, **{'verbosity': verbosity})
  21
+
  22
+        # Run the development server. Turn off auto-reloading because it causes
  23
+        # a strange error -- it causes this handle() method to be called
  24
+        # multiple times.
  25
+        shutdown_message = '\nServer stopped.\nNote that the test database, %r, has not been deleted. You can explore it on your own.' % db_name
  26
+        call_command('runserver', shutdown_message=shutdown_message, use_reloader=False)
42  docs/django-admin.txt
@@ -235,6 +235,7 @@ The ``dumpdata`` command can be used to generate input for ``loaddata``.
235 235
 
236 236
 reset [appname appname ...]
237 237
 ---------------------------
  238
+
238 239
 Executes the equivalent of ``sqlreset`` for the given appnames.
239 240
 
240 241
 runfcgi [options]
@@ -426,7 +427,46 @@ test
426 427
 
427 428
 Discover and run tests for all installed models.  See `Testing Django applications`_ for more information.
428 429
 
429  
-.. _testing django applications: ../testing/
  430
+.. _testing Django applications: ../testing/
  431
+
  432
+testserver [fixture fixture ...]
  433
+--------------------------------
  434
+
  435
+**New in Django development version**
  436
+
  437
+Runs a Django development server (as in ``runserver``) using data from the
  438
+given fixture(s).
  439
+
  440
+For example, this command::
  441
+
  442
+    django-admin.py testserver mydata.json
  443
+
  444
+...would perform the following steps:
  445
+
  446
+    1. Create a test database, as described in `testing Django applications`_.
  447
+    2. Populate the test database with fixture data from the given fixtures.
  448
+       (For more on fixtures, see the documentation for ``loaddata`` above.)
  449
+    3. Runs the Django development server (as in ``runserver``), pointed at
  450
+       this newly created test database instead of your production database.
  451
+
  452
+This is useful in a number of ways:
  453
+
  454
+    * When you're writing `unit tests`_ of how your views act with certain
  455
+      fixture data, you can use ``testserver`` to interact with the views in
  456
+      a Web browser, manually.
  457
+
  458
+    * Let's say you're developing your Django application and have a "pristine"
  459
+      copy of a database that you'd like to interact with. You can dump your
  460
+      database to a fixture (using the ``dumpdata`` command, explained above),
  461
+      then use ``testserver`` to run your Web application with that data. With
  462
+      this arrangement, you have the flexibility of messing up your data
  463
+      in any way, knowing that whatever data changes you're making are only
  464
+      being made to a test database.
  465
+
  466
+Note that this server can only run on the default port on localhost; it does
  467
+not yet accept a ``host`` or ``port`` parameter.
  468
+
  469
+.. _unit tests: ../testing/
430 470
 
431 471
 validate
432 472
 --------

0 notes on commit 9c55bbd

Please sign in to comment.
Something went wrong with that request. Please try again.