parts/django/docs/topics/testing.txt
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 ===========================
       
     2 Testing Django applications
       
     3 ===========================
       
     4 
       
     5 .. module:: django.test
       
     6    :synopsis: Testing tools for Django applications.
       
     7 
       
     8 Automated testing is an extremely useful bug-killing tool for the modern
       
     9 Web developer. You can use a collection of tests -- a **test suite** -- to
       
    10 solve, or avoid, a number of problems:
       
    11 
       
    12     * When you're writing new code, you can use tests to validate your code
       
    13       works as expected.
       
    14 
       
    15     * When you're refactoring or modifying old code, you can use tests to
       
    16       ensure your changes haven't affected your application's behavior
       
    17       unexpectedly.
       
    18 
       
    19 Testing a Web application is a complex task, because a Web application is made
       
    20 of several layers of logic -- from HTTP-level request handling, to form
       
    21 validation and processing, to template rendering. With Django's test-execution
       
    22 framework and assorted utilities, you can simulate requests, insert test data,
       
    23 inspect your application's output and generally verify your code is doing what
       
    24 it should be doing.
       
    25 
       
    26 The best part is, it's really easy.
       
    27 
       
    28 This document is split into two primary sections. First, we explain how to
       
    29 write tests with Django. Then, we explain how to run them.
       
    30 
       
    31 Writing tests
       
    32 =============
       
    33 
       
    34 There are two primary ways to write tests with Django, corresponding to the
       
    35 two test frameworks that ship in the Python standard library. The two
       
    36 frameworks are:
       
    37 
       
    38     * **Doctests** -- tests that are embedded in your functions' docstrings and
       
    39       are written in a way that emulates a session of the Python interactive
       
    40       interpreter. For example::
       
    41 
       
    42           def my_func(a_list, idx):
       
    43               """
       
    44               >>> a = ['larry', 'curly', 'moe']
       
    45               >>> my_func(a, 0)
       
    46               'larry'
       
    47               >>> my_func(a, 1)
       
    48               'curly'
       
    49               """
       
    50               return a_list[idx]
       
    51 
       
    52     * **Unit tests** -- tests that are expressed as methods on a Python class
       
    53       that subclasses ``unittest.TestCase``. For example::
       
    54 
       
    55           import unittest
       
    56 
       
    57           class MyFuncTestCase(unittest.TestCase):
       
    58               def testBasic(self):
       
    59                   a = ['larry', 'curly', 'moe']
       
    60                   self.assertEquals(my_func(a, 0), 'larry')
       
    61                   self.assertEquals(my_func(a, 1), 'curly')
       
    62 
       
    63 You can choose the test framework you like, depending on which syntax you
       
    64 prefer, or you can mix and match, using one framework for some of your code and
       
    65 the other framework for other code. You can also use any *other* Python test
       
    66 frameworks, as we'll explain in a bit.
       
    67 
       
    68 Writing doctests
       
    69 ----------------
       
    70 
       
    71 Doctests use Python's standard doctest_ module, which searches your docstrings
       
    72 for statements that resemble a session of the Python interactive interpreter.
       
    73 A full explanation of how doctest works is out of the scope of this document;
       
    74 read Python's official documentation for the details.
       
    75 
       
    76 .. admonition:: What's a **docstring**?
       
    77 
       
    78     A good explanation of docstrings (and some guidelines for using them
       
    79     effectively) can be found in :pep:`257`:
       
    80 
       
    81         A docstring is a string literal that occurs as the first statement in
       
    82         a module, function, class, or method definition.  Such a docstring
       
    83         becomes the ``__doc__`` special attribute of that object.
       
    84 
       
    85     For example, this function has a docstring that describes what it does::
       
    86 
       
    87         def add_two(num):
       
    88           "Return the result of adding two to the provided number."
       
    89            return num + 2
       
    90 
       
    91     Because tests often make great documentation, putting tests directly in
       
    92     your docstrings is an effective way to document *and* test your code.
       
    93 
       
    94 For a given Django application, the test runner looks for doctests in two
       
    95 places:
       
    96 
       
    97     * The ``models.py`` file. You can define module-level doctests and/or a
       
    98       doctest for individual models. It's common practice to put
       
    99       application-level doctests in the module docstring and model-level
       
   100       doctests in the model docstrings.
       
   101 
       
   102     * A file called ``tests.py`` in the application directory -- i.e., the
       
   103       directory that holds ``models.py``. This file is a hook for any and all
       
   104       doctests you want to write that aren't necessarily related to models.
       
   105 
       
   106 Here is an example model doctest::
       
   107 
       
   108     # models.py
       
   109 
       
   110     from django.db import models
       
   111 
       
   112     class Animal(models.Model):
       
   113         """
       
   114         An animal that knows how to make noise
       
   115 
       
   116         # Create some animals
       
   117         >>> lion = Animal.objects.create(name="lion", sound="roar")
       
   118         >>> cat = Animal.objects.create(name="cat", sound="meow")
       
   119 
       
   120         # Make 'em speak
       
   121         >>> lion.speak()
       
   122         'The lion says "roar"'
       
   123         >>> cat.speak()
       
   124         'The cat says "meow"'
       
   125         """
       
   126         name = models.CharField(max_length=20)
       
   127         sound = models.CharField(max_length=20)
       
   128 
       
   129         def speak(self):
       
   130             return 'The %s says "%s"' % (self.name, self.sound)
       
   131 
       
   132 When you :ref:`run your tests <running-tests>`, the test runner will find this
       
   133 docstring, notice that portions of it look like an interactive Python session,
       
   134 and execute those lines while checking that the results match.
       
   135 
       
   136 In the case of model tests, note that the test runner takes care of creating
       
   137 its own test database. That is, any test that accesses a database -- by
       
   138 creating and saving model instances, for example -- will not affect your
       
   139 production database. However, the database is not refreshed between doctests,
       
   140 so if your doctest requires a certain state you should consider flushing the
       
   141 database or loading a fixture. (See the section on fixtures, below, for more
       
   142 on this.) Note that to use this feature, the database user Django is connecting
       
   143 as must have ``CREATE DATABASE`` rights.
       
   144 
       
   145 For more details about how doctest works, see the `standard library
       
   146 documentation for doctest`_.
       
   147 
       
   148 .. _doctest: http://docs.python.org/library/doctest.html
       
   149 .. _standard library documentation for doctest: doctest_
       
   150 
       
   151 Writing unit tests
       
   152 ------------------
       
   153 
       
   154 Like doctests, Django's unit tests use a standard library module: unittest_.
       
   155 This module uses a different way of defining tests, taking a class-based
       
   156 approach.
       
   157 
       
   158 As with doctests, for a given Django application, the test runner looks for
       
   159 unit tests in two places:
       
   160 
       
   161     * The ``models.py`` file. The test runner looks for any subclass of
       
   162       ``unittest.TestCase`` in this module.
       
   163 
       
   164     * A file called ``tests.py`` in the application directory -- i.e., the
       
   165       directory that holds ``models.py``. Again, the test runner looks for any
       
   166       subclass of ``unittest.TestCase`` in this module.
       
   167 
       
   168 This example ``unittest.TestCase`` subclass is equivalent to the example given
       
   169 in the doctest section above::
       
   170 
       
   171     import unittest
       
   172     from myapp.models import Animal
       
   173 
       
   174     class AnimalTestCase(unittest.TestCase):
       
   175         def setUp(self):
       
   176             self.lion = Animal.objects.create(name="lion", sound="roar")
       
   177             self.cat = Animal.objects.create(name="cat", sound="meow")
       
   178 
       
   179         def testSpeaking(self):
       
   180             self.assertEquals(self.lion.speak(), 'The lion says "roar"')
       
   181             self.assertEquals(self.cat.speak(), 'The cat says "meow"')
       
   182 
       
   183 When you :ref:`run your tests <running-tests>`, the default behavior of the
       
   184 test utility is to find all the test cases (that is, subclasses of
       
   185 ``unittest.TestCase``) in ``models.py`` and ``tests.py``, automatically build a
       
   186 test suite out of those test cases, and run that suite.
       
   187 
       
   188 There is a second way to define the test suite for a module: if you define a
       
   189 function called ``suite()`` in either ``models.py`` or ``tests.py``, the
       
   190 Django test runner will use that function to construct the test suite for that
       
   191 module. This follows the `suggested organization`_ for unit tests. See the
       
   192 Python documentation for more details on how to construct a complex test
       
   193 suite.
       
   194 
       
   195 For more details about ``unittest``, see the `standard library unittest
       
   196 documentation`_.
       
   197 
       
   198 .. _unittest: http://docs.python.org/library/unittest.html
       
   199 .. _standard library unittest documentation: unittest_
       
   200 .. _suggested organization: http://docs.python.org/library/unittest.html#organizing-tests
       
   201 
       
   202 Which should I use?
       
   203 -------------------
       
   204 
       
   205 Because Django supports both of the standard Python test frameworks, it's up to
       
   206 you and your tastes to decide which one to use. You can even decide to use
       
   207 *both*.
       
   208 
       
   209 For developers new to testing, however, this choice can seem confusing. Here,
       
   210 then, are a few key differences to help you decide which approach is right for
       
   211 you:
       
   212 
       
   213     * If you've been using Python for a while, ``doctest`` will probably feel
       
   214       more "pythonic". It's designed to make writing tests as easy as possible,
       
   215       so it requires no overhead of writing classes or methods. You simply put
       
   216       tests in docstrings. This has the added advantage of serving as
       
   217       documentation (and correct documentation, at that!).
       
   218 
       
   219       If you're just getting started with testing, using doctests will probably
       
   220       get you started faster.
       
   221 
       
   222     * The ``unittest`` framework will probably feel very familiar to developers
       
   223       coming from Java. ``unittest`` is inspired by Java's JUnit, so you'll
       
   224       feel at home with this method if you've used JUnit or any test framework
       
   225       inspired by JUnit.
       
   226 
       
   227     * If you need to write a bunch of tests that share similar code, then
       
   228       you'll appreciate the ``unittest`` framework's organization around
       
   229       classes and methods. This makes it easy to abstract common tasks into
       
   230       common methods. The framework also supports explicit setup and/or cleanup
       
   231       routines, which give you a high level of control over the environment
       
   232       in which your test cases are run.
       
   233 
       
   234 Again, remember that you can use both systems side-by-side (even in the same
       
   235 app). In the end, most projects will eventually end up using both. Each shines
       
   236 in different circumstances.
       
   237 
       
   238 .. _running-tests:
       
   239 
       
   240 Running tests
       
   241 =============
       
   242 
       
   243 Once you've written tests, run them using the :djadmin:`test` command of
       
   244 your project's ``manage.py`` utility::
       
   245 
       
   246     $ ./manage.py test
       
   247 
       
   248 By default, this will run every test in every application in
       
   249 :setting:`INSTALLED_APPS`. If you only want to run tests for a particular
       
   250 application, add the application name to the command line. For example, if your
       
   251 :setting:`INSTALLED_APPS` contains ``'myproject.polls'`` and
       
   252 ``'myproject.animals'``, you can run the ``myproject.animals`` unit tests alone
       
   253 with this command::
       
   254 
       
   255     $ ./manage.py test animals
       
   256 
       
   257 Note that we used ``animals``, not ``myproject.animals``.
       
   258 
       
   259 .. versionadded:: 1.0
       
   260    You can now choose which test to run.
       
   261 
       
   262 You can be even *more* specific by naming an individual test case. To
       
   263 run a single test case in an application (for example, the
       
   264 ``AnimalTestCase`` described in the "Writing unit tests" section), add
       
   265 the name of the test case to the label on the command line::
       
   266 
       
   267     $ ./manage.py test animals.AnimalTestCase
       
   268 
       
   269 And it gets even more granular than that! To run a *single* test
       
   270 method inside a test case, add the name of the test method to the
       
   271 label::
       
   272 
       
   273     $ ./manage.py test animals.AnimalTestCase.testFluffyAnimals
       
   274 
       
   275 .. versionadded:: 1.2
       
   276    The ability to select individual doctests was added.
       
   277 
       
   278 You can use the same rules if you're using doctests. Django will use the
       
   279 test label as a path to the test method or class that you want to run.
       
   280 If your ``models.py`` or ``tests.py`` has a function with a doctest, or
       
   281 class with a class-level doctest, you can invoke that test by appending the
       
   282 name of the test method or class to the label::
       
   283 
       
   284     $ ./manage.py test animals.classify
       
   285 
       
   286 If you want to run the doctest for a specific method in a class, add the
       
   287 name of the method to the label::
       
   288 
       
   289     $ ./manage.py test animals.Classifier.run
       
   290 
       
   291 If you're using a ``__test__`` dictionary to specify doctests for a
       
   292 module, Django will use the label as a key in the ``__test__`` dictionary
       
   293 for defined in ``models.py`` and ``tests.py``.
       
   294 
       
   295 .. versionadded:: 1.2
       
   296    You can now trigger a graceful exit from a test run by pressing ``Ctrl-C``.
       
   297 
       
   298 If you press ``Ctrl-C`` while the tests are running, the test runner will
       
   299 wait for the currently running test to complete and then exit gracefully.
       
   300 During a graceful exit the test runner will output details of any test
       
   301 failures, report on how many tests were run and how many errors and failures
       
   302 were encountered, and destroy any test databases as usual. Thus pressing
       
   303 ``Ctrl-C`` can be very useful if you forget to pass the :djadminopt:`--failfast`
       
   304 option, notice that some tests are unexpectedly failing, and want to get details
       
   305 on the failures without waiting for the full test run to complete.
       
   306 
       
   307 If you do not want to wait for the currently running test to finish, you
       
   308 can press ``Ctrl-C`` a second time and the test run will halt immediately,
       
   309 but not gracefully. No details of the tests run before the interruption will
       
   310 be reported, and any test databases created by the run will not be destroyed.
       
   311 
       
   312 .. admonition:: Test with warnings enabled
       
   313 
       
   314     It is a good idea to run your tests with ``python -Wall manage.py
       
   315     test``. This will allow you to catch any deprecation warnings that
       
   316     might be in your code. Django (as well as many other libraries) use
       
   317     warnings to flag when features are deprecated. It can also flag
       
   318     areas in your code that are not strictly wrong, but may benefit
       
   319     from a better implementation.
       
   320 
       
   321 Running tests outside the test runner
       
   322 -------------------------------------
       
   323 
       
   324 If you want to run tests outside of ``./manage.py test`` -- for example,
       
   325 from a shell prompt -- you will need to set up the test
       
   326 environment first. Django provides a convenience method to do this::
       
   327 
       
   328     >>> from django.test.utils import setup_test_environment
       
   329     >>> setup_test_environment()
       
   330 
       
   331 This convenience method sets up the test database, and puts other
       
   332 Django features into modes that allow for repeatable testing.
       
   333 
       
   334 The call to :meth:`~django.test.utils.setup_test_environment` is made
       
   335 automatically as part of the setup of `./manage.py test`. You only
       
   336 need to manually invoke this method if you're not using running your
       
   337 tests via Django's test runner.
       
   338 
       
   339 The test database
       
   340 -----------------
       
   341 
       
   342 Tests that require a database (namely, model tests) will not use your "real"
       
   343 (production) database. Separate, blank databases are created for the tests.
       
   344 
       
   345 Regardless of whether the tests pass or fail, the test databases are destroyed
       
   346 when all the tests have been executed.
       
   347 
       
   348 By default the test databases get their names by prepending ``test_``
       
   349 to the value of the :setting:`NAME` settings for the databases
       
   350 defined in :setting:`DATABASES`. When using the SQLite database engine
       
   351 the tests will by default use an in-memory database (i.e., the
       
   352 database will be created in memory, bypassing the filesystem
       
   353 entirely!). If you want to use a different database name, specify
       
   354 :setting:`TEST_NAME` in the dictionary for any given database in
       
   355 :setting:`DATABASES`.
       
   356 
       
   357 Aside from using a separate database, the test runner will otherwise
       
   358 use all of the same database settings you have in your settings file:
       
   359 :setting:`ENGINE`, :setting:`USER`, :setting:`HOST`, etc. The test
       
   360 database is created by the user specified by ``USER``, so you'll need
       
   361 to make sure that the given user account has sufficient privileges to
       
   362 create a new database on the system.
       
   363 
       
   364 .. versionadded:: 1.0
       
   365 
       
   366 For fine-grained control over the character encoding of your test
       
   367 database, use the :setting:`TEST_CHARSET` option. If you're using
       
   368 MySQL, you can also use the :setting:`TEST_COLLATION` option to
       
   369 control the particular collation used by the test database. See the
       
   370 :doc:`settings documentation </ref/settings>` for details of these
       
   371 advanced settings.
       
   372 
       
   373 .. _topics-testing-masterslave:
       
   374 
       
   375 Testing master/slave configurations
       
   376 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   377 
       
   378 .. versionadded:: 1.2
       
   379 
       
   380 If you're testing a multiple database configuration with master/slave
       
   381 replication, this strategy of creating test databases poses a problem.
       
   382 When the test databases are created, there won't be any replication,
       
   383 and as a result, data created on the master won't be seen on the
       
   384 slave.
       
   385 
       
   386 To compensate for this, Django allows you to define that a database is
       
   387 a *test mirror*. Consider the following (simplified) example database
       
   388 configuration::
       
   389 
       
   390     DATABASES = {
       
   391         'default': {
       
   392             'ENGINE': 'django.db.backends.mysql',
       
   393             'NAME': 'myproject',
       
   394             'HOST': 'dbmaster',
       
   395              # ... plus some other settings
       
   396         },
       
   397         'slave': {
       
   398             'ENGINE': 'django.db.backends.mysql',
       
   399             'NAME': 'myproject',
       
   400             'HOST': 'dbslave',
       
   401             'TEST_MIRROR': 'default'
       
   402             # ... plus some other settings
       
   403         }
       
   404     }
       
   405 
       
   406 In this setup, we have two database servers: ``dbmaster``, described
       
   407 by the database alias ``default``, and ``dbslave`` described by the
       
   408 alias ``slave``. As you might expect, ``dbslave`` has been configured
       
   409 by the database administrator as a read slave of ``dbmaster``, so in
       
   410 normal activity, any write to ``default`` will appear on ``slave``.
       
   411 
       
   412 If Django created two independent test databases, this would break any
       
   413 tests that expected replication to occur. However, the ``slave``
       
   414 database has been configured as a test mirror (using the
       
   415 :setting:`TEST_MIRROR` setting), indicating that under testing,
       
   416 ``slave`` should be treated as a mirror of ``default``.
       
   417 
       
   418 When the test environment is configured, a test version of ``slave``
       
   419 will *not* be created. Instead the connection to ``slave``
       
   420 will be redirected to point at ``default``. As a result, writes to
       
   421 ``default`` will appear on ``slave`` -- but because they are actually
       
   422 the same database, not because there is data replication between the
       
   423 two databases.
       
   424 
       
   425 .. _topics-testing-creation-dependencies:
       
   426 
       
   427 Controlling creation order for test databases
       
   428 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   429 
       
   430 .. versionadded:: 1.2.4
       
   431 
       
   432 By default, Django will always create the ``default`` database first.
       
   433 However, no guarantees are made on the creation order of any other
       
   434 databases in your test setup.
       
   435 
       
   436 If your database configuration requires a specific creation order, you
       
   437 can specify the dependencies that exist using the
       
   438 :setting:`TEST_DEPENDENCIES` setting. Consider the following
       
   439 (simplified) example database configuration::
       
   440 
       
   441     DATABASES = {
       
   442         'default': {
       
   443              # ... db settings
       
   444              'TEST_DEPENDENCIES': ['diamonds']
       
   445         },
       
   446         'diamonds': {
       
   447             # ... db settings
       
   448         },
       
   449         'clubs': {
       
   450             # ... db settings
       
   451             'TEST_DEPENDENCIES': ['diamonds']
       
   452         },
       
   453         'spades': {
       
   454             # ... db settings
       
   455             'TEST_DEPENDENCIES': ['diamonds','hearts']
       
   456         },
       
   457         'hearts': {
       
   458             # ... db settings
       
   459             'TEST_DEPENDENCIES': ['diamonds','clubs']
       
   460         }
       
   461     }
       
   462 
       
   463 Under this configuration, the ``diamonds`` database will be created first,
       
   464 as it is the only database alias without dependencies. The ``default``` and
       
   465 ``clubs`` alias will be created next (although the order of creation of this
       
   466 pair is not guaranteed); then ``hearts``; and finally ``spades``.
       
   467 
       
   468 If there are any circular dependencies in the
       
   469 :setting:`TEST_DEPENDENCIES` definition, an ``ImproperlyConfigured``
       
   470 exception will be raised.
       
   471 
       
   472 Other test conditions
       
   473 ---------------------
       
   474 
       
   475 Regardless of the value of the :setting:`DEBUG` setting in your configuration
       
   476 file, all Django tests run with :setting:`DEBUG`\=False. This is to ensure that
       
   477 the observed output of your code matches what will be seen in a production
       
   478 setting.
       
   479 
       
   480 Understanding the test output
       
   481 -----------------------------
       
   482 
       
   483 When you run your tests, you'll see a number of messages as the test runner
       
   484 prepares itself. You can control the level of detail of these messages with the
       
   485 ``verbosity`` option on the command line::
       
   486 
       
   487     Creating test database...
       
   488     Creating table myapp_animal
       
   489     Creating table myapp_mineral
       
   490     Loading 'initial_data' fixtures...
       
   491     No fixtures found.
       
   492 
       
   493 This tells you that the test runner is creating a test database, as described
       
   494 in the previous section.
       
   495 
       
   496 Once the test database has been created, Django will run your tests.
       
   497 If everything goes well, you'll see something like this::
       
   498 
       
   499     ----------------------------------------------------------------------
       
   500     Ran 22 tests in 0.221s
       
   501 
       
   502     OK
       
   503 
       
   504 If there are test failures, however, you'll see full details about which tests
       
   505 failed::
       
   506 
       
   507     ======================================================================
       
   508     FAIL: Doctest: ellington.core.throttle.models
       
   509     ----------------------------------------------------------------------
       
   510     Traceback (most recent call last):
       
   511       File "/dev/django/test/doctest.py", line 2153, in runTest
       
   512         raise self.failureException(self.format_failure(new.getvalue()))
       
   513     AssertionError: Failed doctest test for myapp.models
       
   514       File "/dev/myapp/models.py", line 0, in models
       
   515 
       
   516     ----------------------------------------------------------------------
       
   517     File "/dev/myapp/models.py", line 14, in myapp.models
       
   518     Failed example:
       
   519         throttle.check("actor A", "action one", limit=2, hours=1)
       
   520     Expected:
       
   521         True
       
   522     Got:
       
   523         False
       
   524 
       
   525     ----------------------------------------------------------------------
       
   526     Ran 2 tests in 0.048s
       
   527 
       
   528     FAILED (failures=1)
       
   529 
       
   530 A full explanation of this error output is beyond the scope of this document,
       
   531 but it's pretty intuitive. You can consult the documentation of Python's
       
   532 ``unittest`` library for details.
       
   533 
       
   534 Note that the return code for the test-runner script is the total number of
       
   535 failed and erroneous tests. If all the tests pass, the return code is 0. This
       
   536 feature is useful if you're using the test-runner script in a shell script and
       
   537 need to test for success or failure at that level.
       
   538 
       
   539 Testing tools
       
   540 =============
       
   541 
       
   542 Django provides a small set of tools that come in handy when writing tests.
       
   543 
       
   544 The test client
       
   545 ---------------
       
   546 
       
   547 .. module:: django.test.client
       
   548    :synopsis: Django's test client.
       
   549 
       
   550 The test client is a Python class that acts as a dummy Web browser, allowing
       
   551 you to test your views and interact with your Django-powered application
       
   552 programmatically.
       
   553 
       
   554 Some of the things you can do with the test client are:
       
   555 
       
   556     * Simulate GET and POST requests on a URL and observe the response --
       
   557       everything from low-level HTTP (result headers and status codes) to
       
   558       page content.
       
   559 
       
   560     * Test that the correct view is executed for a given URL.
       
   561 
       
   562     * Test that a given request is rendered by a given Django template, with
       
   563       a template context that contains certain values.
       
   564 
       
   565 Note that the test client is not intended to be a replacement for Twill_,
       
   566 Selenium_, or other "in-browser" frameworks. Django's test client has
       
   567 a different focus. In short:
       
   568 
       
   569     * Use Django's test client to establish that the correct view is being
       
   570       called and that the view is collecting the correct context data.
       
   571 
       
   572     * Use in-browser frameworks such as Twill and Selenium to test *rendered*
       
   573       HTML and the *behavior* of Web pages, namely JavaScript functionality.
       
   574 
       
   575 A comprehensive test suite should use a combination of both test types.
       
   576 
       
   577 .. _Twill: http://twill.idyll.org/
       
   578 .. _Selenium: http://seleniumhq.org/
       
   579 
       
   580 Overview and a quick example
       
   581 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   582 
       
   583 To use the test client, instantiate ``django.test.client.Client`` and retrieve
       
   584 Web pages::
       
   585 
       
   586     >>> from django.test.client import Client
       
   587     >>> c = Client()
       
   588     >>> response = c.post('/login/', {'username': 'john', 'password': 'smith'})
       
   589     >>> response.status_code
       
   590     200
       
   591     >>> response = c.get('/customer/details/')
       
   592     >>> response.content
       
   593     '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...'
       
   594 
       
   595 As this example suggests, you can instantiate ``Client`` from within a session
       
   596 of the Python interactive interpreter.
       
   597 
       
   598 Note a few important things about how the test client works:
       
   599 
       
   600     * The test client does *not* require the Web server to be running. In fact,
       
   601       it will run just fine with no Web server running at all! That's because
       
   602       it avoids the overhead of HTTP and deals directly with the Django
       
   603       framework. This helps make the unit tests run quickly.
       
   604 
       
   605     * When retrieving pages, remember to specify the *path* of the URL, not the
       
   606       whole domain. For example, this is correct::
       
   607 
       
   608           >>> c.get('/login/')
       
   609 
       
   610       This is incorrect::
       
   611 
       
   612           >>> c.get('http://www.example.com/login/')
       
   613 
       
   614       The test client is not capable of retrieving Web pages that are not
       
   615       powered by your Django project. If you need to retrieve other Web pages,
       
   616       use a Python standard library module such as urllib_ or urllib2_.
       
   617 
       
   618     * To resolve URLs, the test client uses whatever URLconf is pointed-to by
       
   619       your :setting:`ROOT_URLCONF` setting.
       
   620 
       
   621     * Although the above example would work in the Python interactive
       
   622       interpreter, some of the test client's functionality, notably the
       
   623       template-related functionality, is only available *while tests are
       
   624       running*.
       
   625 
       
   626       The reason for this is that Django's test runner performs a bit of black
       
   627       magic in order to determine which template was loaded by a given view.
       
   628       This black magic (essentially a patching of Django's template system in
       
   629       memory) only happens during test running.
       
   630 
       
   631     * By default, the test client will disable any CSRF checks
       
   632       performed by your site.
       
   633 
       
   634       .. versionadded:: 1.2.2
       
   635 
       
   636       If, for some reason, you *want* the test client to perform CSRF
       
   637       checks, you can create an instance of the test client that
       
   638       enforces CSRF checks. To do this, pass in the
       
   639       ``enforce_csrf_checks`` argument when you construct your
       
   640       client::
       
   641 
       
   642           >>> from django.test import Client
       
   643           >>> csrf_client = Client(enforce_csrf_checks=True)
       
   644 
       
   645 
       
   646 .. _urllib: http://docs.python.org/library/urllib.html
       
   647 .. _urllib2: http://docs.python.org/library/urllib2.html
       
   648 
       
   649 Making requests
       
   650 ~~~~~~~~~~~~~~~
       
   651 
       
   652 Use the ``django.test.client.Client`` class to make requests. It requires no
       
   653 arguments at time of construction:
       
   654 
       
   655 .. class:: Client()
       
   656 
       
   657     Once you have a ``Client`` instance, you can call any of the following
       
   658     methods:
       
   659 
       
   660     .. method:: Client.get(path, data={}, follow=False, **extra)
       
   661 
       
   662 
       
   663         Makes a GET request on the provided ``path`` and returns a ``Response``
       
   664         object, which is documented below.
       
   665 
       
   666         The key-value pairs in the ``data`` dictionary are used to create a GET
       
   667         data payload. For example::
       
   668 
       
   669             >>> c = Client()
       
   670             >>> c.get('/customers/details/', {'name': 'fred', 'age': 7})
       
   671 
       
   672         ...will result in the evaluation of a GET request equivalent to::
       
   673 
       
   674             /customers/details/?name=fred&age=7
       
   675 
       
   676         The ``extra`` keyword arguments parameter can be used to specify
       
   677         headers to be sent in the request. For example::
       
   678 
       
   679             >>> c = Client()
       
   680             >>> c.get('/customers/details/', {'name': 'fred', 'age': 7},
       
   681             ...       HTTP_X_REQUESTED_WITH='XMLHttpRequest')
       
   682 
       
   683         ...will send the HTTP header ``HTTP_X_REQUESTED_WITH`` to the
       
   684         details	view, which is a good way to test code paths that use the
       
   685         :meth:`django.http.HttpRequest.is_ajax()` method.
       
   686 
       
   687         .. versionadded:: 1.1
       
   688 
       
   689         If you already have the GET arguments in URL-encoded form, you can
       
   690         use that encoding instead of using the data argument. For example,
       
   691         the previous GET request could also be posed as::
       
   692 
       
   693         >>> c = Client()
       
   694         >>> c.get('/customers/details/?name=fred&age=7')
       
   695 
       
   696         If you provide a URL with both an encoded GET data and a data argument,
       
   697         the data argument will take precedence.
       
   698 
       
   699         If you set ``follow`` to ``True`` the client will follow any redirects
       
   700         and a ``redirect_chain`` attribute will be set in the response object
       
   701         containing tuples of the intermediate urls and status codes.
       
   702 
       
   703         If you had an url ``/redirect_me/`` that redirected to ``/next/``, that
       
   704         redirected to ``/final/``, this is what you'd see::
       
   705 
       
   706             >>> response = c.get('/redirect_me/', follow=True)
       
   707             >>> response.redirect_chain
       
   708             [(u'http://testserver/next/', 302), (u'http://testserver/final/', 302)]
       
   709 
       
   710     .. method:: Client.post(path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra)
       
   711 
       
   712         Makes a POST request on the provided ``path`` and returns a
       
   713         ``Response`` object, which is documented below.
       
   714 
       
   715         The key-value pairs in the ``data`` dictionary are used to submit POST
       
   716         data. For example::
       
   717 
       
   718             >>> c = Client()
       
   719             >>> c.post('/login/', {'name': 'fred', 'passwd': 'secret'})
       
   720 
       
   721         ...will result in the evaluation of a POST request to this URL::
       
   722 
       
   723             /login/
       
   724 
       
   725         ...with this POST data::
       
   726 
       
   727             name=fred&passwd=secret
       
   728 
       
   729         If you provide ``content_type`` (e.g., ``text/xml`` for an XML
       
   730         payload), the contents of ``data`` will be sent as-is in the POST
       
   731         request, using ``content_type`` in the HTTP ``Content-Type`` header.
       
   732 
       
   733         If you don't provide a value for ``content_type``, the values in
       
   734         ``data`` will be transmitted with a content type of
       
   735         ``multipart/form-data``. In this case, the key-value pairs in ``data``
       
   736         will be encoded as a multipart message and used to create the POST data
       
   737         payload.
       
   738 
       
   739         To submit multiple values for a given key -- for example, to specify
       
   740         the selections for a ``<select multiple>`` -- provide the values as a
       
   741         list or tuple for the required key. For example, this value of ``data``
       
   742         would submit three selected values for the field named ``choices``::
       
   743 
       
   744             {'choices': ('a', 'b', 'd')}
       
   745 
       
   746         Submitting files is a special case. To POST a file, you need only
       
   747         provide the file field name as a key, and a file handle to the file you
       
   748         wish to upload as a value. For example::
       
   749 
       
   750             >>> c = Client()
       
   751             >>> f = open('wishlist.doc')
       
   752             >>> c.post('/customers/wishes/', {'name': 'fred', 'attachment': f})
       
   753             >>> f.close()
       
   754 
       
   755         (The name ``attachment`` here is not relevant; use whatever name your
       
   756         file-processing code expects.)
       
   757 
       
   758         Note that if you wish to use the same file handle for multiple
       
   759         ``post()`` calls then you will need to manually reset the file
       
   760         pointer between posts. The easiest way to do this is to
       
   761         manually close the file after it has been provided to
       
   762         ``post()``, as demonstrated above.
       
   763 
       
   764         You should also ensure that the file is opened in a way that
       
   765         allows the data to be read. If your file contains binary data
       
   766         such as an image, this means you will need to open the file in
       
   767         ``rb`` (read binary) mode.
       
   768 
       
   769         The ``extra`` argument acts the same as for :meth:`Client.get`.
       
   770 
       
   771         .. versionchanged:: 1.1
       
   772 
       
   773         If the URL you request with a POST contains encoded parameters, these
       
   774         parameters will be made available in the request.GET data. For example,
       
   775         if you were to make the request::
       
   776 
       
   777         >>> c.post('/login/?visitor=true', {'name': 'fred', 'passwd': 'secret'})
       
   778 
       
   779         ... the view handling this request could interrogate request.POST
       
   780         to retrieve the username and password, and could interrogate request.GET
       
   781         to determine if the user was a visitor.
       
   782 
       
   783         If you set ``follow`` to ``True`` the client will follow any redirects
       
   784         and a ``redirect_chain`` attribute will be set in the response object
       
   785         containing tuples of the intermediate urls and status codes.
       
   786 
       
   787     .. method:: Client.head(path, data={}, follow=False, **extra)
       
   788 
       
   789         .. versionadded:: 1.1
       
   790 
       
   791         Makes a HEAD request on the provided ``path`` and returns a ``Response``
       
   792         object. Useful for testing RESTful interfaces. Acts just like
       
   793         :meth:`Client.get` except it does not return a message body.
       
   794 
       
   795         If you set ``follow`` to ``True`` the client will follow any redirects
       
   796         and a ``redirect_chain`` attribute will be set in the response object
       
   797         containing tuples of the intermediate urls and status codes.
       
   798 
       
   799     .. method:: Client.options(path, data={}, follow=False, **extra)
       
   800 
       
   801         .. versionadded:: 1.1
       
   802 
       
   803         Makes an OPTIONS request on the provided ``path`` and returns a
       
   804         ``Response`` object. Useful for testing RESTful interfaces.
       
   805 
       
   806         If you set ``follow`` to ``True`` the client will follow any redirects
       
   807         and a ``redirect_chain`` attribute will be set in the response object
       
   808         containing tuples of the intermediate urls and status codes.
       
   809 
       
   810         The ``extra`` argument acts the same as for :meth:`Client.get`.
       
   811 
       
   812     .. method:: Client.put(path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra)
       
   813 
       
   814         .. versionadded:: 1.1
       
   815 
       
   816         Makes a PUT request on the provided ``path`` and returns a
       
   817         ``Response`` object. Useful for testing RESTful interfaces. Acts just
       
   818         like :meth:`Client.post` except with the PUT request method.
       
   819 
       
   820         If you set ``follow`` to ``True`` the client will follow any redirects
       
   821         and a ``redirect_chain`` attribute will be set in the response object
       
   822         containing tuples of the intermediate urls and status codes.
       
   823 
       
   824     .. method:: Client.delete(path, follow=False, **extra)
       
   825 
       
   826         .. versionadded:: 1.1
       
   827 
       
   828         Makes an DELETE request on the provided ``path`` and returns a
       
   829         ``Response`` object. Useful for testing RESTful interfaces.
       
   830 
       
   831         If you set ``follow`` to ``True`` the client will follow any redirects
       
   832         and a ``redirect_chain`` attribute will be set in the response object
       
   833         containing tuples of the intermediate urls and status codes.
       
   834 
       
   835         The ``extra`` argument acts the same as for :meth:`Client.get`.
       
   836 
       
   837     .. method:: Client.login(**credentials)
       
   838 
       
   839         .. versionadded:: 1.0
       
   840 
       
   841         If your site uses Django's :doc:`authentication system</topics/auth>`
       
   842         and you deal with logging in users, you can use the test client's
       
   843         ``login()`` method to simulate the effect of a user logging into the
       
   844         site.
       
   845 
       
   846         After you call this method, the test client will have all the cookies
       
   847         and session data required to pass any login-based tests that may form
       
   848         part of a view.
       
   849 
       
   850         The format of the ``credentials`` argument depends on which
       
   851         :ref:`authentication backend <authentication-backends>` you're using
       
   852         (which is configured by your :setting:`AUTHENTICATION_BACKENDS`
       
   853         setting). If you're using the standard authentication backend provided
       
   854         by Django (``ModelBackend``), ``credentials`` should be the user's
       
   855         username and password, provided as keyword arguments::
       
   856 
       
   857             >>> c = Client()
       
   858             >>> c.login(username='fred', password='secret')
       
   859 
       
   860             # Now you can access a view that's only available to logged-in users.
       
   861 
       
   862         If you're using a different authentication backend, this method may
       
   863         require different credentials. It requires whichever credentials are
       
   864         required by your backend's ``authenticate()`` method.
       
   865 
       
   866         ``login()`` returns ``True`` if it the credentials were accepted and
       
   867         login was successful.
       
   868 
       
   869         Finally, you'll need to remember to create user accounts before you can
       
   870         use this method. As we explained above, the test runner is executed
       
   871         using a test database, which contains no users by default. As a result,
       
   872         user accounts that are valid on your production site will not work
       
   873         under test conditions. You'll need to create users as part of the test
       
   874         suite -- either manually (using the Django model API) or with a test
       
   875         fixture. Remember that if you want your test user to have a password,
       
   876         you can't set the user's password by setting the password attribute
       
   877         directly -- you must use the
       
   878         :meth:`~django.contrib.auth.models.User.set_password()` function to
       
   879         store a correctly hashed password. Alternatively, you can use the
       
   880         :meth:`~django.contrib.auth.models.UserManager.create_user` helper
       
   881         method to create a new user with a correctly hashed password.
       
   882 
       
   883     .. method:: Client.logout()
       
   884 
       
   885         .. versionadded:: 1.0
       
   886 
       
   887         If your site uses Django's :doc:`authentication system</topics/auth>`,
       
   888         the ``logout()`` method can be used to simulate the effect of a user
       
   889         logging out of your site.
       
   890 
       
   891         After you call this method, the test client will have all the cookies
       
   892         and session data cleared to defaults. Subsequent requests will appear
       
   893         to come from an AnonymousUser.
       
   894 
       
   895 Testing responses
       
   896 ~~~~~~~~~~~~~~~~~
       
   897 
       
   898 The ``get()`` and ``post()`` methods both return a ``Response`` object. This
       
   899 ``Response`` object is *not* the same as the ``HttpResponse`` object returned
       
   900 Django views; the test response object has some additional data useful for
       
   901 test code to verify.
       
   902 
       
   903 Specifically, a ``Response`` object has the following attributes:
       
   904 
       
   905 .. class:: Response()
       
   906 
       
   907     .. attribute:: client
       
   908 
       
   909         The test client that was used to make the request that resulted in the
       
   910         response.
       
   911 
       
   912     .. attribute:: content
       
   913 
       
   914         The body of the response, as a string. This is the final page content as
       
   915         rendered by the view, or any error message.
       
   916 
       
   917     .. attribute:: context
       
   918 
       
   919         The template ``Context`` instance that was used to render the template that
       
   920         produced the response content.
       
   921 
       
   922         If the rendered page used multiple templates, then ``context`` will be a
       
   923         list of ``Context`` objects, in the order in which they were rendered.
       
   924 
       
   925         .. versionadded:: 1.1
       
   926 
       
   927         Regardless of the number of templates used during rendering, you can
       
   928         retrieve context values using the ``[]`` operator. For example, the
       
   929         context variable ``name`` could be retrieved using::
       
   930 
       
   931             >>> response = client.get('/foo/')
       
   932             >>> response.context['name']
       
   933             'Arthur'
       
   934 
       
   935     .. attribute:: request
       
   936 
       
   937         The request data that stimulated the response.
       
   938 
       
   939     .. attribute:: status_code
       
   940 
       
   941         The HTTP status of the response, as an integer. See RFC2616_ for a full
       
   942         list of HTTP status codes.
       
   943 
       
   944     .. attribute:: template
       
   945 
       
   946         The ``Template`` instance that was used to render the final content. Use
       
   947         ``template.name`` to get the template's file name, if the template was
       
   948         loaded from a file. (The name is a string such as ``'admin/index.html'``.)
       
   949 
       
   950         If the rendered page used multiple templates -- e.g., using :ref:`template
       
   951         inheritance<template-inheritance>` -- then ``template`` will be a list of
       
   952         ``Template`` instances, in the order in which they were rendered.
       
   953 
       
   954 You can also use dictionary syntax on the response object to query the value
       
   955 of any settings in the HTTP headers. For example, you could determine the
       
   956 content type of a response using ``response['Content-Type']``.
       
   957 
       
   958 .. _RFC2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
       
   959 
       
   960 Exceptions
       
   961 ~~~~~~~~~~
       
   962 
       
   963 If you point the test client at a view that raises an exception, that exception
       
   964 will be visible in the test case. You can then use a standard ``try...except``
       
   965 block or ``unittest.TestCase.assertRaises()`` to test for exceptions.
       
   966 
       
   967 The only exceptions that are not visible to the test client are ``Http404``,
       
   968 ``PermissionDenied`` and ``SystemExit``. Django catches these exceptions
       
   969 internally and converts them into the appropriate HTTP response codes. In these
       
   970 cases, you can check ``response.status_code`` in your test.
       
   971 
       
   972 Persistent state
       
   973 ~~~~~~~~~~~~~~~~
       
   974 
       
   975 The test client is stateful. If a response returns a cookie, then that cookie
       
   976 will be stored in the test client and sent with all subsequent ``get()`` and
       
   977 ``post()`` requests.
       
   978 
       
   979 Expiration policies for these cookies are not followed. If you want a cookie
       
   980 to expire, either delete it manually or create a new ``Client`` instance (which
       
   981 will effectively delete all cookies).
       
   982 
       
   983 A test client has two attributes that store persistent state information. You
       
   984 can access these properties as part of a test condition.
       
   985 
       
   986 .. attribute:: Client.cookies
       
   987 
       
   988     A Python ``SimpleCookie`` object, containing the current values of all the
       
   989     client cookies. See the `Cookie module documentation`_ for more.
       
   990 
       
   991 .. attribute:: Client.session
       
   992 
       
   993     A dictionary-like object containing session information. See the
       
   994     :doc:`session documentation</topics/http/sessions>` for full details.
       
   995 
       
   996     To modify the session and then save it, it must be stored in a variable
       
   997     first (because a new ``SessionStore`` is created every time this property
       
   998     is accessed)::
       
   999 
       
  1000         def test_something(self):
       
  1001             session = self.client.session
       
  1002             session['somekey'] = 'test'
       
  1003             session.save()
       
  1004 
       
  1005 .. _Cookie module documentation: http://docs.python.org/library/cookie.html
       
  1006 
       
  1007 Example
       
  1008 ~~~~~~~
       
  1009 
       
  1010 The following is a simple unit test using the test client::
       
  1011 
       
  1012     import unittest
       
  1013     from django.test.client import Client
       
  1014 
       
  1015     class SimpleTest(unittest.TestCase):
       
  1016         def setUp(self):
       
  1017             # Every test needs a client.
       
  1018             self.client = Client()
       
  1019 
       
  1020         def test_details(self):
       
  1021             # Issue a GET request.
       
  1022             response = self.client.get('/customer/details/')
       
  1023 
       
  1024             # Check that the response is 200 OK.
       
  1025             self.failUnlessEqual(response.status_code, 200)
       
  1026 
       
  1027             # Check that the rendered context contains 5 customers.
       
  1028             self.failUnlessEqual(len(response.context['customers']), 5)
       
  1029 
       
  1030 TestCase
       
  1031 --------
       
  1032 
       
  1033 .. currentmodule:: django.test
       
  1034 
       
  1035 Normal Python unit test classes extend a base class of ``unittest.TestCase``.
       
  1036 Django provides an extension of this base class:
       
  1037 
       
  1038 .. class:: TestCase()
       
  1039 
       
  1040 This class provides some additional capabilities that can be useful for testing
       
  1041 Web sites.
       
  1042 
       
  1043 Converting a normal ``unittest.TestCase`` to a Django ``TestCase`` is easy:
       
  1044 just change the base class of your test from ``unittest.TestCase`` to
       
  1045 ``django.test.TestCase``. All of the standard Python unit test functionality
       
  1046 will continue to be available, but it will be augmented with some useful
       
  1047 additions.
       
  1048 
       
  1049 .. versionadded:: 1.1
       
  1050 
       
  1051 .. class:: TransactionTestCase()
       
  1052 
       
  1053 Django ``TestCase`` classes make use of database transaction facilities, if
       
  1054 available, to speed up the process of resetting the database to a known state
       
  1055 at the beginning of each test. A consequence of this, however, is that the
       
  1056 effects of transaction commit and rollback cannot be tested by a Django
       
  1057 ``TestCase`` class. If your test requires testing of such transactional
       
  1058 behavior, you should use a Django ``TransactionTestCase``.
       
  1059 
       
  1060 ``TransactionTestCase`` and ``TestCase`` are identical except for the manner
       
  1061 in which the database is reset to a known state and the ability for test code
       
  1062 to test the effects of commit and rollback. A ``TransactionTestCase`` resets
       
  1063 the database before the test runs by truncating all tables and reloading
       
  1064 initial data. A ``TransactionTestCase`` may call commit and rollback and
       
  1065 observe the effects of these calls on the database.
       
  1066 
       
  1067 A ``TestCase``, on the other hand, does not truncate tables and reload initial
       
  1068 data at the beginning of a test. Instead, it encloses the test code in a
       
  1069 database transaction that is rolled back at the end of the test.  It also
       
  1070 prevents the code under test from issuing any commit or rollback operations
       
  1071 on the database, to ensure that the rollback at the end of the test restores
       
  1072 the database to its initial state. In order to guarantee that all ``TestCase``
       
  1073 code starts with a clean database, the Django test runner runs all ``TestCase``
       
  1074 tests first, before any other tests (e.g. doctests) that may alter the
       
  1075 database without restoring it to its original state.
       
  1076 
       
  1077 When running on a database that does not support rollback (e.g. MySQL with the
       
  1078 MyISAM storage engine), ``TestCase`` falls back to initializing the database
       
  1079 by truncating tables and reloading initial data.
       
  1080 
       
  1081 
       
  1082 .. note::
       
  1083     The ``TestCase`` use of rollback to un-do the effects of the test code
       
  1084     may reveal previously-undetected errors in test code.  For example,
       
  1085     test code that assumes primary keys values will be assigned starting at
       
  1086     one may find that assumption no longer holds true when rollbacks instead
       
  1087     of table truncation are being used to reset the database.  Similarly,
       
  1088     the reordering of tests so that all ``TestCase`` classes run first may
       
  1089     reveal unexpected dependencies on test case ordering.  In such cases a
       
  1090     quick fix is to switch the ``TestCase`` to a ``TransactionTestCase``.
       
  1091     A better long-term fix, that allows the test to take advantage of the
       
  1092     speed benefit of ``TestCase``, is to fix the underlying test problem.
       
  1093 
       
  1094 
       
  1095 Default test client
       
  1096 ~~~~~~~~~~~~~~~~~~~
       
  1097 
       
  1098 .. versionadded:: 1.0
       
  1099 
       
  1100 .. attribute:: TestCase.client
       
  1101 
       
  1102 Every test case in a ``django.test.TestCase`` instance has access to an
       
  1103 instance of a Django test client. This client can be accessed as
       
  1104 ``self.client``. This client is recreated for each test, so you don't have to
       
  1105 worry about state (such as cookies) carrying over from one test to another.
       
  1106 
       
  1107 This means, instead of instantiating a ``Client`` in each test::
       
  1108 
       
  1109     import unittest
       
  1110     from django.test.client import Client
       
  1111 
       
  1112     class SimpleTest(unittest.TestCase):
       
  1113         def test_details(self):
       
  1114             client = Client()
       
  1115             response = client.get('/customer/details/')
       
  1116             self.failUnlessEqual(response.status_code, 200)
       
  1117 
       
  1118         def test_index(self):
       
  1119             client = Client()
       
  1120             response = client.get('/customer/index/')
       
  1121             self.failUnlessEqual(response.status_code, 200)
       
  1122 
       
  1123 ...you can just refer to ``self.client``, like so::
       
  1124 
       
  1125     from django.test import TestCase
       
  1126 
       
  1127     class SimpleTest(TestCase):
       
  1128         def test_details(self):
       
  1129             response = self.client.get('/customer/details/')
       
  1130             self.failUnlessEqual(response.status_code, 200)
       
  1131 
       
  1132         def test_index(self):
       
  1133             response = self.client.get('/customer/index/')
       
  1134             self.failUnlessEqual(response.status_code, 200)
       
  1135 
       
  1136 .. _topics-testing-fixtures:
       
  1137 
       
  1138 Fixture loading
       
  1139 ~~~~~~~~~~~~~~~
       
  1140 
       
  1141 .. attribute:: TestCase.fixtures
       
  1142 
       
  1143 A test case for a database-backed Web site isn't much use if there isn't any
       
  1144 data in the database. To make it easy to put test data into the database,
       
  1145 Django's custom ``TestCase`` class provides a way of loading **fixtures**.
       
  1146 
       
  1147 A fixture is a collection of data that Django knows how to import into a
       
  1148 database. For example, if your site has user accounts, you might set up a
       
  1149 fixture of fake user accounts in order to populate your database during tests.
       
  1150 
       
  1151 The most straightforward way of creating a fixture is to use the
       
  1152 :djadmin:`manage.py dumpdata <dumpdata>` command. This assumes you
       
  1153 already have some data in your database. See the :djadmin:`dumpdata
       
  1154 documentation<dumpdata>` for more details.
       
  1155 
       
  1156 .. note::
       
  1157     If you've ever run :djadmin:`manage.py syncdb<syncdb>`, you've
       
  1158     already used a fixture without even knowing it! When you call
       
  1159     :djadmin:`syncdb` in the database for the first time, Django
       
  1160     installs a fixture called ``initial_data``. This gives you a way
       
  1161     of populating a new database with any initial data, such as a
       
  1162     default set of categories.
       
  1163 
       
  1164     Fixtures with other names can always be installed manually using
       
  1165     the :djadmin:`manage.py loaddata<loaddata>` command.
       
  1166 
       
  1167 Once you've created a fixture and placed it in a ``fixtures`` directory in one
       
  1168 of your :setting:`INSTALLED_APPS`, you can use it in your unit tests by
       
  1169 specifying a ``fixtures`` class attribute on your :class:`django.test.TestCase`
       
  1170 subclass::
       
  1171 
       
  1172     from django.test import TestCase
       
  1173     from myapp.models import Animal
       
  1174 
       
  1175     class AnimalTestCase(TestCase):
       
  1176         fixtures = ['mammals.json', 'birds']
       
  1177 
       
  1178         def setUp(self):
       
  1179             # Test definitions as before.
       
  1180             call_setup_methods()
       
  1181 
       
  1182         def testFluffyAnimals(self):
       
  1183             # A test that uses the fixtures.
       
  1184             call_some_test_code()
       
  1185 
       
  1186 Here's specifically what will happen:
       
  1187 
       
  1188     * At the start of each test case, before ``setUp()`` is run, Django will
       
  1189       flush the database, returning the database to the state it was in
       
  1190       directly after :djadmin:`syncdb` was called.
       
  1191 
       
  1192     * Then, all the named fixtures are installed. In this example, Django will
       
  1193       install any JSON fixture named ``mammals``, followed by any fixture named
       
  1194       ``birds``. See the :djadmin:`loaddata` documentation for more
       
  1195       details on defining and installing fixtures.
       
  1196 
       
  1197 This flush/load procedure is repeated for each test in the test case, so you
       
  1198 can be certain that the outcome of a test will not be affected by another test,
       
  1199 or by the order of test execution.
       
  1200 
       
  1201 URLconf configuration
       
  1202 ~~~~~~~~~~~~~~~~~~~~~
       
  1203 
       
  1204 .. versionadded:: 1.0
       
  1205 
       
  1206 .. attribute:: TestCase.urls
       
  1207 
       
  1208 If your application provides views, you may want to include tests that use the
       
  1209 test client to exercise those views. However, an end user is free to deploy the
       
  1210 views in your application at any URL of their choosing. This means that your
       
  1211 tests can't rely upon the fact that your views will be available at a
       
  1212 particular URL.
       
  1213 
       
  1214 In order to provide a reliable URL space for your test,
       
  1215 ``django.test.TestCase`` provides the ability to customize the URLconf
       
  1216 configuration for the duration of the execution of a test suite. If your
       
  1217 ``TestCase`` instance defines an ``urls`` attribute, the ``TestCase`` will use
       
  1218 the value of that attribute as the ``ROOT_URLCONF`` for the duration of that
       
  1219 test.
       
  1220 
       
  1221 For example::
       
  1222 
       
  1223     from django.test import TestCase
       
  1224 
       
  1225     class TestMyViews(TestCase):
       
  1226         urls = 'myapp.test_urls'
       
  1227 
       
  1228         def testIndexPageView(self):
       
  1229             # Here you'd test your view using ``Client``.
       
  1230             call_some_test_code()
       
  1231 
       
  1232 This test case will use the contents of ``myapp.test_urls`` as the
       
  1233 URLconf for the duration of the test case.
       
  1234 
       
  1235 .. _emptying-test-outbox:
       
  1236 
       
  1237 Multi-database support
       
  1238 ~~~~~~~~~~~~~~~~~~~~~~
       
  1239 
       
  1240 .. attribute:: TestCase.multi_db
       
  1241 
       
  1242 .. versionadded:: 1.2
       
  1243 
       
  1244 Django sets up a test database corresponding to every database that is
       
  1245 defined in the :setting:`DATABASES` definition in your settings
       
  1246 file. However, a big part of the time taken to run a Django TestCase
       
  1247 is consumed by the call to ``flush`` that ensures that you have a
       
  1248 clean database at the start of each test run. If you have multiple
       
  1249 databases, multiple flushes are required (one for each database),
       
  1250 which can be a time consuming activity -- especially if your tests
       
  1251 don't need to test multi-database activity.
       
  1252 
       
  1253 As an optimization, Django only flushes the ``default`` database at
       
  1254 the start of each test run. If your setup contains multiple databases,
       
  1255 and you have a test that requires every database to be clean, you can
       
  1256 use the ``multi_db`` attribute on the test suite to request a full
       
  1257 flush.
       
  1258 
       
  1259 For example::
       
  1260 
       
  1261     class TestMyViews(TestCase):
       
  1262         multi_db = True
       
  1263 
       
  1264         def testIndexPageView(self):
       
  1265             call_some_test_code()
       
  1266 
       
  1267 This test case will flush *all* the test databases before running
       
  1268 ``testIndexPageView``.
       
  1269 
       
  1270 Emptying the test outbox
       
  1271 ~~~~~~~~~~~~~~~~~~~~~~~~
       
  1272 
       
  1273 .. versionadded:: 1.0
       
  1274 
       
  1275 If you use Django's custom ``TestCase`` class, the test runner will clear the
       
  1276 contents of the test e-mail outbox at the start of each test case.
       
  1277 
       
  1278 For more detail on e-mail services during tests, see `E-mail services`_.
       
  1279 
       
  1280 Assertions
       
  1281 ~~~~~~~~~~
       
  1282 
       
  1283 .. versionadded:: 1.0
       
  1284 
       
  1285 .. versionchanged:: 1.2
       
  1286     Addded ``msg_prefix`` argument.
       
  1287 
       
  1288 As Python's normal ``unittest.TestCase`` class implements assertion methods
       
  1289 such as ``assertTrue`` and ``assertEquals``, Django's custom ``TestCase`` class
       
  1290 provides a number of custom assertion methods that are useful for testing Web
       
  1291 applications:
       
  1292 
       
  1293 The failure messages given by the assertion methods can be customized
       
  1294 with the ``msg_prefix`` argument. This string will be prefixed to any
       
  1295 failure message generated by the assertion. This allows you to provide
       
  1296 additional details that may help you to identify the location and
       
  1297 cause of an failure in your test suite.
       
  1298 
       
  1299 .. method:: TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='')
       
  1300 
       
  1301     Asserts that a ``Response`` instance produced the given ``status_code`` and
       
  1302     that ``text`` appears in the content of the response. If ``count`` is
       
  1303     provided, ``text`` must occur exactly ``count`` times in the response.
       
  1304 
       
  1305 .. method:: TestCase.assertNotContains(response, text, status_code=200, msg_prefix='')
       
  1306 
       
  1307     Asserts that a ``Response`` instance produced the given ``status_code`` and
       
  1308     that ``text`` does not appears in the content of the response.
       
  1309 
       
  1310 .. method:: TestCase.assertFormError(response, form, field, errors, msg_prefix='')
       
  1311 
       
  1312     Asserts that a field on a form raises the provided list of errors when
       
  1313     rendered on the form.
       
  1314 
       
  1315     ``form`` is the name the ``Form`` instance was given in the template
       
  1316     context.
       
  1317 
       
  1318     ``field`` is the name of the field on the form to check. If ``field``
       
  1319     has a value of ``None``, non-field errors (errors you can access via
       
  1320     ``form.non_field_errors()``) will be checked.
       
  1321 
       
  1322     ``errors`` is an error string, or a list of error strings, that are
       
  1323     expected as a result of form validation.
       
  1324 
       
  1325 .. method:: TestCase.assertTemplateUsed(response, template_name, msg_prefix='')
       
  1326 
       
  1327     Asserts that the template with the given name was used in rendering the
       
  1328     response.
       
  1329 
       
  1330     The name is a string such as ``'admin/index.html'``.
       
  1331 
       
  1332 .. method:: TestCase.assertTemplateNotUsed(response, template_name, msg_prefix='')
       
  1333 
       
  1334     Asserts that the template with the given name was *not* used in rendering
       
  1335     the response.
       
  1336 
       
  1337 .. method:: TestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, msg_prefix='')
       
  1338 
       
  1339     Asserts that the response return a ``status_code`` redirect status, it
       
  1340     redirected to ``expected_url`` (including any GET data), and the final
       
  1341     page was received with ``target_status_code``.
       
  1342 
       
  1343     .. versionadded:: 1.1
       
  1344 
       
  1345     If your request used the ``follow`` argument, the ``expected_url`` and
       
  1346     ``target_status_code`` will be the url and status code for the final
       
  1347     point of the redirect chain.
       
  1348 
       
  1349 .. _topics-testing-email:
       
  1350 
       
  1351 E-mail services
       
  1352 ---------------
       
  1353 
       
  1354 .. versionadded:: 1.0
       
  1355 
       
  1356 If any of your Django views send e-mail using :doc:`Django's e-mail
       
  1357 functionality </topics/email>`, you probably don't want to send e-mail each time
       
  1358 you run a test using that view. For this reason, Django's test runner
       
  1359 automatically redirects all Django-sent e-mail to a dummy outbox. This lets you
       
  1360 test every aspect of sending e-mail -- from the number of messages sent to the
       
  1361 contents of each message -- without actually sending the messages.
       
  1362 
       
  1363 The test runner accomplishes this by transparently replacing the normal
       
  1364 email backend with a testing backend.
       
  1365 (Don't worry -- this has no effect on any other e-mail senders outside of
       
  1366 Django, such as your machine's mail server, if you're running one.)
       
  1367 
       
  1368 .. currentmodule:: django.core.mail
       
  1369 
       
  1370 .. data:: django.core.mail.outbox
       
  1371 
       
  1372 During test running, each outgoing e-mail is saved in
       
  1373 ``django.core.mail.outbox``. This is a simple list of all
       
  1374 :class:`~django.core.mail.EmailMessage` instances that have been sent.
       
  1375 The ``outbox`` attribute is a special attribute that is created *only* when
       
  1376 the ``locmem`` e-mail backend is used. It doesn't normally exist as part of the
       
  1377 :mod:`django.core.mail` module and you can't import it directly. The code
       
  1378 below shows how to access this attribute correctly.
       
  1379 
       
  1380 Here's an example test that examines ``django.core.mail.outbox`` for length
       
  1381 and contents::
       
  1382 
       
  1383     from django.core import mail
       
  1384     from django.test import TestCase
       
  1385 
       
  1386     class EmailTest(TestCase):
       
  1387         def test_send_email(self):
       
  1388             # Send message.
       
  1389             mail.send_mail('Subject here', 'Here is the message.',
       
  1390                 'from@example.com', ['to@example.com'],
       
  1391                 fail_silently=False)
       
  1392 
       
  1393             # Test that one message has been sent.
       
  1394             self.assertEquals(len(mail.outbox), 1)
       
  1395 
       
  1396             # Verify that the subject of the first message is correct.
       
  1397             self.assertEquals(mail.outbox[0].subject, 'Subject here')
       
  1398 
       
  1399 As noted :ref:`previously <emptying-test-outbox>`, the test outbox is emptied
       
  1400 at the start of every test in a Django ``TestCase``. To empty the outbox
       
  1401 manually, assign the empty list to ``mail.outbox``::
       
  1402 
       
  1403     from django.core import mail
       
  1404 
       
  1405     # Empty the test outbox
       
  1406     mail.outbox = []
       
  1407 
       
  1408 Using different testing frameworks
       
  1409 ==================================
       
  1410 
       
  1411 Clearly, ``doctest`` and ``unittest`` are not the only Python testing
       
  1412 frameworks. While Django doesn't provide explicit support for alternative
       
  1413 frameworks, it does provide a way to invoke tests constructed for an
       
  1414 alternative framework as if they were normal Django tests.
       
  1415 
       
  1416 When you run ``./manage.py test``, Django looks at the :setting:`TEST_RUNNER`
       
  1417 setting to determine what to do. By default, :setting:`TEST_RUNNER` points to
       
  1418 ``'django.test.simple.DjangoTestSuiteRunner'``. This class defines the default Django
       
  1419 testing behavior. This behavior involves:
       
  1420 
       
  1421     #. Performing global pre-test setup.
       
  1422 
       
  1423     #. Looking for unit tests and doctests in the ``models.py`` and
       
  1424        ``tests.py`` files in each installed application.
       
  1425 
       
  1426     #. Creating the test databases.
       
  1427 
       
  1428     #. Running ``syncdb`` to install models and initial data into the test
       
  1429        databases.
       
  1430 
       
  1431     #. Running the unit tests and doctests that are found.
       
  1432 
       
  1433     #. Destroying the test databases.
       
  1434 
       
  1435     #. Performing global post-test teardown.
       
  1436 
       
  1437 If you define your own test runner class and point :setting:`TEST_RUNNER` at
       
  1438 that class, Django will execute your test runner whenever you run
       
  1439 ``./manage.py test``. In this way, it is possible to use any test framework
       
  1440 that can be executed from Python code, or to modify the Django test execution
       
  1441 process to satisfy whatever testing requirements you may have.
       
  1442 
       
  1443 .. _topics-testing-test_runner:
       
  1444 
       
  1445 Defining a test runner
       
  1446 ----------------------
       
  1447 
       
  1448 .. versionchanged:: 1.2
       
  1449    Prior to 1.2, test runners were a single function, not a class.
       
  1450 
       
  1451 .. currentmodule:: django.test.simple
       
  1452 
       
  1453 A test runner is a class defining a ``run_tests()`` method. Django ships
       
  1454 with a ``DjangoTestSuiteRunner`` class that defines the default Django
       
  1455 testing behavior. This class defines the ``run_tests()`` entry point,
       
  1456 plus a selection of other methods that are used to by ``run_tests()`` to
       
  1457 set up, execute and tear down the test suite.
       
  1458 
       
  1459 .. class:: DjangoTestSuiteRunner(verbosity=1, interactive=True, failfast=True, **kwargs)
       
  1460 
       
  1461     ``verbosity`` determines the amount of notification and debug information
       
  1462     that will be printed to the console; ``0`` is no output, ``1`` is normal
       
  1463     output, and ``2`` is verbose output.
       
  1464 
       
  1465     If ``interactive`` is ``True``, the test suite has permission to ask the
       
  1466     user for instructions when the test suite is executed. An example of this
       
  1467     behavior would be asking for permission to delete an existing test
       
  1468     database. If ``interactive`` is ``False``, the test suite must be able to
       
  1469     run without any manual intervention.
       
  1470 
       
  1471     If ``failfast`` is ``True``, the test suite will stop running after the
       
  1472     first test failure is detected.
       
  1473 
       
  1474     Django will, from time to time, extend the capabilities of
       
  1475     the test runner by adding new arguments. The ``**kwargs`` declaration
       
  1476     allows for this expansion. If you subclass ``DjangoTestSuiteRunner`` or
       
  1477     write your own test runner, ensure accept and handle the ``**kwargs``
       
  1478     parameter.
       
  1479 
       
  1480 .. method:: DjangoTestSuiteRunner.run_tests(test_labels, extra_tests=None, **kwargs)
       
  1481 
       
  1482     Run the test suite.
       
  1483 
       
  1484     ``test_labels`` is a list of strings describing the tests to be run. A test
       
  1485     label can take one of three forms:
       
  1486 
       
  1487         * ``app.TestCase.test_method`` -- Run a single test method in a test
       
  1488           case.
       
  1489         * ``app.TestCase`` -- Run all the test methods in a test case.
       
  1490         * ``app`` -- Search for and run all tests in the named application.
       
  1491 
       
  1492     If ``test_labels`` has a value of ``None``, the test runner should run
       
  1493     search for tests in all the applications in :setting:`INSTALLED_APPS`.
       
  1494 
       
  1495     ``extra_tests`` is a list of extra ``TestCase`` instances to add to the
       
  1496     suite that is executed by the test runner. These extra tests are run
       
  1497     in addition to those discovered in the modules listed in ``test_labels``.
       
  1498 
       
  1499     This method should return the number of tests that failed.
       
  1500 
       
  1501 .. method:: DjangoTestSuiteRunner.setup_test_environment(**kwargs)
       
  1502 
       
  1503     Sets up the test environment ready for testing.
       
  1504 
       
  1505 .. method:: DjangoTestSuiteRunner.build_suite(test_labels, extra_tests=None, **kwargs)
       
  1506 
       
  1507     Constructs a test suite that matches the test labels provided.
       
  1508 
       
  1509     ``test_labels`` is a list of strings describing the tests to be run. A test
       
  1510     label can take one of three forms:
       
  1511 
       
  1512         * ``app.TestCase.test_method`` -- Run a single test method in a test
       
  1513           case.
       
  1514         * ``app.TestCase`` -- Run all the test methods in a test case.
       
  1515         * ``app`` -- Search for and run all tests in the named application.
       
  1516 
       
  1517     If ``test_labels`` has a value of ``None``, the test runner should run
       
  1518     search for tests in all the applications in :setting:`INSTALLED_APPS`.
       
  1519 
       
  1520     ``extra_tests`` is a list of extra ``TestCase`` instances to add to the
       
  1521     suite that is executed by the test runner. These extra tests are run
       
  1522     in addition to those discovered in the modules listed in ``test_labels``.
       
  1523 
       
  1524     Returns a ``TestSuite`` instance ready to be run.
       
  1525 
       
  1526 .. method:: DjangoTestSuiteRunner.setup_databases(**kwargs)
       
  1527 
       
  1528     Creates the test databases.
       
  1529 
       
  1530     Returns a data structure that provides enough detail to undo the changes
       
  1531     that have been made. This data will be provided to the ``teardown_databases()``
       
  1532     function at the conclusion of testing.
       
  1533 
       
  1534 .. method:: DjangoTestSuiteRunner.run_suite(suite, **kwargs)
       
  1535 
       
  1536     Runs the test suite.
       
  1537 
       
  1538     Returns the result produced by the running the test suite.
       
  1539 
       
  1540 .. method:: DjangoTestSuiteRunner.teardown_databases(old_config, **kwargs)
       
  1541 
       
  1542     Destroys the test databases, restoring pre-test conditions.
       
  1543 
       
  1544     ``old_config`` is a data structure defining the changes in the
       
  1545     database configuration that need to be reversed. It is the return
       
  1546     value of the ``setup_databases()`` method.
       
  1547 
       
  1548 .. method:: DjangoTestSuiteRunner.teardown_test_environment(**kwargs)
       
  1549 
       
  1550     Restores the pre-test environment.
       
  1551 
       
  1552 .. method:: DjangoTestSuiteRunner.suite_result(suite, result, **kwargs)
       
  1553 
       
  1554     Computes and returns a return code based on a test suite, and the result
       
  1555 	from that test suite.
       
  1556 
       
  1557 
       
  1558 Testing utilities
       
  1559 -----------------
       
  1560 
       
  1561 .. module:: django.test.utils
       
  1562    :synopsis: Helpers to write custom test runners.
       
  1563 
       
  1564 To assist in the creation of your own test runner, Django provides a number of
       
  1565 utility methods in the ``django.test.utils`` module.
       
  1566 
       
  1567 .. function:: setup_test_environment()
       
  1568 
       
  1569     Performs any global pre-test setup, such as the installing the
       
  1570     instrumentation of the template rendering system and setting up
       
  1571     the dummy ``SMTPConnection``.
       
  1572 
       
  1573 .. function:: teardown_test_environment()
       
  1574 
       
  1575     Performs any global post-test teardown, such as removing the black
       
  1576     magic hooks into the template system and restoring normal e-mail
       
  1577     services.
       
  1578 
       
  1579 The creation module of the database backend (``connection.creation``)
       
  1580 also provides some utilities that can be useful during testing.
       
  1581 
       
  1582 .. function:: create_test_db(verbosity=1, autoclobber=False)
       
  1583 
       
  1584     Creates a new test database and runs ``syncdb`` against it.
       
  1585 
       
  1586     ``verbosity`` has the same behavior as in ``run_tests()``.
       
  1587 
       
  1588     ``autoclobber`` describes the behavior that will occur if a
       
  1589     database with the same name as the test database is discovered:
       
  1590 
       
  1591         * If ``autoclobber`` is ``False``, the user will be asked to
       
  1592           approve destroying the existing database. ``sys.exit`` is
       
  1593           called if the user does not approve.
       
  1594 
       
  1595         * If autoclobber is ``True``, the database will be destroyed
       
  1596           without consulting the user.
       
  1597 
       
  1598     Returns the name of the test database that it created.
       
  1599 
       
  1600     ``create_test_db()`` has the side effect of modifying the value of
       
  1601     :setting:`NAME` in :setting:`DATABASES` to match the name of the test
       
  1602     database.
       
  1603 
       
  1604     .. versionchanged:: 1.0
       
  1605        ``create_test_db()`` now returns the name of the test database.
       
  1606 
       
  1607 .. function:: destroy_test_db(old_database_name, verbosity=1)
       
  1608 
       
  1609     Destroys the database whose name is in stored in :setting:`NAME` in the
       
  1610     :setting:`DATABASES`, and sets :setting:`NAME` to use the
       
  1611     provided name.
       
  1612 
       
  1613     ``verbosity`` has the same behavior as in ``run_tests()``.