Parcourir la source

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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5912 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Adrian Holovaty il y a 17 ans
Parent
commit
9c55bbdef7
2 fichiers modifiés avec 67 ajouts et 1 suppressions
  1. 26 0
      django/core/management/commands/testserver.py
  2. 41 1
      docs/django-admin.txt

+ 26 - 0
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)

+ 41 - 1
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
 --------