Skip to content

Commit 9c55bbd

Browse files
Added 'django-admin.py testserver' command and docs
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5912 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent a5400cf commit 9c55bbd

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

docs/django-admin.txt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ The ``dumpdata`` command can be used to generate input for ``loaddata``.
235235

236236
reset [appname appname ...]
237237
---------------------------
238+
238239
Executes the equivalent of ``sqlreset`` for the given appnames.
239240

240241
runfcgi [options]
@@ -426,7 +427,46 @@ test
426427

427428
Discover and run tests for all installed models. See `Testing Django applications`_ for more information.
428429

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/
430470

431471
validate
432472
--------

0 commit comments

Comments
 (0)