2013-12-31 19:24:11 +08:00
|
|
|
=========================
|
|
|
|
Writing and running tests
|
|
|
|
=========================
|
2006-08-30 02:04:09 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. module:: django.test
|
|
|
|
:synopsis: Testing tools for Django applications.
|
|
|
|
|
2012-12-22 08:59:06 +08:00
|
|
|
.. seealso::
|
2007-05-06 11:59:37 +08:00
|
|
|
|
2013-12-31 19:24:11 +08:00
|
|
|
The :doc:`testing tutorial </intro/tutorial05>`, the :doc:`testing tools
|
|
|
|
reference </topics/testing/tools>`, and the :doc:`advanced testing topics
|
|
|
|
</topics/testing/advanced>`.
|
2007-05-06 11:59:37 +08:00
|
|
|
|
2012-12-22 08:59:06 +08:00
|
|
|
This document is split into two primary sections. First, we explain how to write
|
|
|
|
tests with Django. Then, we explain how to run them.
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2006-08-30 02:04:09 +08:00
|
|
|
Writing tests
|
|
|
|
=============
|
|
|
|
|
2011-09-05 05:17:30 +08:00
|
|
|
Django's unit tests use a Python standard library module: :mod:`unittest`. This
|
2013-05-11 11:08:45 +08:00
|
|
|
module defines tests using a class-based approach.
|
2011-01-17 03:22:36 +08:00
|
|
|
|
|
|
|
.. admonition:: unittest2
|
|
|
|
|
2013-07-01 19:53:06 +08:00
|
|
|
.. deprecated:: 1.7
|
2011-01-17 03:22:36 +08:00
|
|
|
|
2013-07-01 19:53:06 +08:00
|
|
|
Python 2.7 introduced some major changes to the ``unittest`` library,
|
|
|
|
adding some extremely useful features. To ensure that every Django project
|
|
|
|
could benefit from these new features, Django used to ship with a copy of
|
|
|
|
Python 2.7's ``unittest`` backported for Python 2.6 compatibility.
|
2011-01-17 03:22:36 +08:00
|
|
|
|
2013-07-01 19:53:06 +08:00
|
|
|
Since Django no longer supports Python versions older than 2.7,
|
|
|
|
``django.utils.unittest`` is deprecated. Simply use ``unittest``.
|
2011-01-17 03:22:36 +08:00
|
|
|
|
2013-12-09 01:39:26 +08:00
|
|
|
.. _unittest2: https://pypi.python.org/pypi/unittest2
|
2011-01-17 03:22:36 +08:00
|
|
|
|
2013-05-15 08:25:45 +08:00
|
|
|
Here is an example which subclasses from :class:`django.test.TestCase`,
|
|
|
|
which is a subclass of :class:`unittest.TestCase` that runs each test inside a
|
|
|
|
transaction to provide isolation::
|
2011-01-17 03:22:36 +08:00
|
|
|
|
2013-05-15 08:25:45 +08:00
|
|
|
from django.test import TestCase
|
2011-01-17 03:22:36 +08:00
|
|
|
from myapp.models import Animal
|
|
|
|
|
2013-05-15 08:25:45 +08:00
|
|
|
class AnimalTestCase(TestCase):
|
2011-01-17 03:22:36 +08:00
|
|
|
def setUp(self):
|
2013-05-15 08:25:45 +08:00
|
|
|
Animal.objects.create(name="lion", sound="roar")
|
|
|
|
Animal.objects.create(name="cat", sound="meow")
|
2011-01-17 03:22:36 +08:00
|
|
|
|
2011-12-17 10:03:56 +08:00
|
|
|
def test_animals_can_speak(self):
|
|
|
|
"""Animals that can speak are correctly identified"""
|
2013-05-15 08:25:45 +08:00
|
|
|
lion = Animal.objects.get(name="lion")
|
|
|
|
cat = Animal.objects.get(name="cat")
|
|
|
|
self.assertEqual(lion.speak(), 'The lion says "roar"')
|
|
|
|
self.assertEqual(cat.speak(), 'The cat says "meow"')
|
2011-01-17 03:22:36 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
When you :ref:`run your tests <running-tests>`, the default behavior of the
|
|
|
|
test utility is to find all the test cases (that is, subclasses of
|
|
|
|
:class:`unittest.TestCase`) in any file whose name begins with ``test``,
|
|
|
|
automatically build a test suite out of those test cases, and run that suite.
|
2011-01-17 03:22:36 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
For more details about :mod:`unittest`, see the Python documentation.
|
2006-08-30 02:04:09 +08:00
|
|
|
|
2012-12-18 19:39:23 +08:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
If your tests rely on database access such as creating or querying models,
|
|
|
|
be sure to create your test classes as subclasses of
|
|
|
|
:class:`django.test.TestCase` rather than :class:`unittest.TestCase`.
|
|
|
|
|
2013-05-15 08:25:45 +08:00
|
|
|
Using :class:`unittest.TestCase` avoids the cost of running each test in a
|
|
|
|
transaction and flushing the database, but if your tests interact with
|
|
|
|
the database their behavior will vary based on the order that the test
|
|
|
|
runner executes them. This can lead to unit tests that pass when run in
|
|
|
|
isolation but fail when run in a suite.
|
2012-12-18 19:39:23 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _running-tests:
|
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2007-08-15 13:24:18 +08:00
|
|
|
Running tests
|
|
|
|
=============
|
|
|
|
|
2010-10-24 00:37:51 +08:00
|
|
|
Once you've written tests, run them using the :djadmin:`test` command of
|
2010-01-01 02:48:28 +08:00
|
|
|
your project's ``manage.py`` utility::
|
2007-08-15 13:24:18 +08:00
|
|
|
|
|
|
|
$ ./manage.py test
|
|
|
|
|
2013-09-07 05:41:12 +08:00
|
|
|
Test discovery is based on the unittest module's :py:ref:`built-in test
|
|
|
|
discovery <unittest-test-discovery>`. By default, this will discover tests in
|
|
|
|
any file named "test*.py" under the current working directory.
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
You can specify particular tests to run by supplying any number of "test
|
|
|
|
labels" to ``./manage.py test``. Each test label can be a full Python dotted
|
|
|
|
path to a package, module, ``TestCase`` subclass, or test method. For instance::
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
# Run all the tests in the animals.tests module
|
|
|
|
$ ./manage.py test animals.tests
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
# Run all the tests found within the 'animals' package
|
|
|
|
$ ./manage.py test animals
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
# Run just one test case
|
|
|
|
$ ./manage.py test animals.tests.AnimalTestCase
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
# Run just one test method
|
|
|
|
$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
You can also provide a path to a directory to discover tests below that
|
|
|
|
directory::
|
2010-02-01 21:00:00 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
$ ./manage.py test animals/
|
2010-02-01 21:00:00 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
You can specify a custom filename pattern match using the ``-p`` (or
|
|
|
|
``--pattern``) option, if your test files are named differently from the
|
|
|
|
``test*.py`` pattern::
|
2010-02-01 21:00:00 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
$ ./manage.py test --pattern="tests_*.py"
|
2010-02-01 21:00:00 +08:00
|
|
|
|
2010-01-18 23:11:01 +08:00
|
|
|
If you press ``Ctrl-C`` while the tests are running, the test runner will
|
2010-01-01 03:23:23 +08:00
|
|
|
wait for the currently running test to complete and then exit gracefully.
|
2010-01-18 23:11:01 +08:00
|
|
|
During a graceful exit the test runner will output details of any test
|
2010-01-01 02:48:28 +08:00
|
|
|
failures, report on how many tests were run and how many errors and failures
|
2010-01-18 23:11:01 +08:00
|
|
|
were encountered, and destroy any test databases as usual. Thus pressing
|
2010-01-01 02:48:28 +08:00
|
|
|
``Ctrl-C`` can be very useful if you forget to pass the :djadminopt:`--failfast`
|
2010-01-18 23:11:01 +08:00
|
|
|
option, notice that some tests are unexpectedly failing, and want to get details
|
2010-01-01 02:48:28 +08:00
|
|
|
on the failures without waiting for the full test run to complete.
|
|
|
|
|
|
|
|
If you do not want to wait for the currently running test to finish, you
|
|
|
|
can press ``Ctrl-C`` a second time and the test run will halt immediately,
|
|
|
|
but not gracefully. No details of the tests run before the interruption will
|
|
|
|
be reported, and any test databases created by the run will not be destroyed.
|
|
|
|
|
2010-10-08 23:37:59 +08:00
|
|
|
.. admonition:: Test with warnings enabled
|
|
|
|
|
2010-10-09 04:30:02 +08:00
|
|
|
It's a good idea to run your tests with Python warnings enabled:
|
|
|
|
``python -Wall manage.py test``. The ``-Wall`` flag tells Python to
|
|
|
|
display deprecation warnings. Django, like many other Python libraries,
|
|
|
|
uses these warnings to flag when features are going away. It also might
|
|
|
|
flag areas in your code that aren't strictly wrong but could benefit
|
2010-10-08 23:37:59 +08:00
|
|
|
from a better implementation.
|
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2012-12-22 08:59:06 +08:00
|
|
|
.. _the-test-database:
|
2010-08-23 15:43:50 +08:00
|
|
|
|
2007-12-17 16:50:32 +08:00
|
|
|
The test database
|
|
|
|
-----------------
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2008-08-26 08:52:55 +08:00
|
|
|
Tests that require a database (namely, model tests) will not use your "real"
|
2009-12-22 23:18:51 +08:00
|
|
|
(production) database. Separate, blank databases are created for the tests.
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
Regardless of whether the tests pass or fail, the test databases are destroyed
|
2007-12-17 16:50:32 +08:00
|
|
|
when all the tests have been executed.
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
By default the test databases get their names by prepending ``test_``
|
2010-01-25 20:05:38 +08:00
|
|
|
to the value of the :setting:`NAME` settings for the databases
|
2009-12-22 23:18:51 +08:00
|
|
|
defined in :setting:`DATABASES`. When using the SQLite database engine
|
|
|
|
the tests will by default use an in-memory database (i.e., the
|
|
|
|
database will be created in memory, bypassing the filesystem
|
|
|
|
entirely!). If you want to use a different database name, specify
|
2014-03-09 14:33:33 +08:00
|
|
|
:setting:`NAME <TEST_NAME>` in the :setting:`TEST <DATABASE-TEST>`
|
|
|
|
dictionary for any given database in :setting:`DATABASES`.
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
Aside from using a separate database, the test runner will otherwise
|
|
|
|
use all of the same database settings you have in your settings file:
|
2012-12-25 04:37:36 +08:00
|
|
|
:setting:`ENGINE <DATABASE-ENGINE>`, :setting:`USER`, :setting:`HOST`, etc. The
|
|
|
|
test database is created by the user specified by :setting:`USER`, so you'll
|
|
|
|
need to make sure that the given user account has sufficient privileges to
|
2009-12-22 23:18:51 +08:00
|
|
|
create a new database on the system.
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
For fine-grained control over the character encoding of your test
|
2014-03-09 14:33:33 +08:00
|
|
|
database, use the :setting:`CHARSET <TEST_CHARSET>` TEST option. If you're using
|
|
|
|
MySQL, you can also use the :setting:`COLLATION <TEST_COLLATION>` option to
|
2009-12-22 23:18:51 +08:00
|
|
|
control the particular collation used by the test database. See the
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`settings documentation </ref/settings>` for details of these
|
2014-03-09 14:33:33 +08:00
|
|
|
and other advanced settings.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
The different options in the :setting:`TEST <DATABASE-TEST>` database
|
|
|
|
setting used to be separate options in the database settings dictionary,
|
|
|
|
prefixed with ``TEST_``.
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2012-11-03 04:33:07 +08:00
|
|
|
.. admonition:: Finding data from your production database when running tests?
|
|
|
|
|
|
|
|
If your code attempts to access the database when its modules are compiled,
|
|
|
|
this will occur *before* the test database is set up, with potentially
|
|
|
|
unexpected results. For example, if you have a database query in
|
|
|
|
module-level code and a real database exists, production data could pollute
|
|
|
|
your tests. *It is a bad idea to have such import-time database queries in
|
|
|
|
your code* anyway - rewrite your code so that it doesn't do this.
|
|
|
|
|
2014-02-16 00:27:12 +08:00
|
|
|
.. versionadded:: 1.7
|
|
|
|
|
|
|
|
This also applies to customized implementations of
|
|
|
|
:meth:`~django.apps.AppConfig.ready()`.
|
|
|
|
|
2012-12-22 08:59:06 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
The :ref:`advanced multi-db testing topics <topics-testing-advanced-multidb>`.
|
2010-12-05 08:44:34 +08:00
|
|
|
|
2013-06-12 04:56:09 +08:00
|
|
|
.. _order-of-tests:
|
|
|
|
|
2012-07-25 04:24:16 +08:00
|
|
|
Order in which tests are executed
|
|
|
|
---------------------------------
|
|
|
|
|
|
|
|
In order to guarantee that all ``TestCase`` code starts with a clean database,
|
|
|
|
the Django test runner reorders tests in the following way:
|
|
|
|
|
2013-06-27 21:02:00 +08:00
|
|
|
* All :class:`~django.test.TestCase` subclasses are run first.
|
|
|
|
|
2014-04-12 17:42:06 +08:00
|
|
|
* Then, all other Django-based tests (test cases based on
|
|
|
|
:class:`~django.test.SimpleTestCase`, including
|
2013-06-27 21:02:00 +08:00
|
|
|
:class:`~django.test.TransactionTestCase`) are run with no particular
|
|
|
|
ordering guaranteed nor enforced among them.
|
2012-07-25 04:24:16 +08:00
|
|
|
|
2014-04-12 17:42:06 +08:00
|
|
|
* Then any other :class:`unittest.TestCase` tests (including doctests) that may
|
|
|
|
alter the database without restoring it to its original state are run.
|
2012-07-25 04:24:16 +08:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The new ordering of tests may reveal unexpected dependencies on test case
|
|
|
|
ordering. This is the case with doctests that relied on state left in the
|
|
|
|
database by a given :class:`~django.test.TransactionTestCase` test, they
|
|
|
|
must be updated to be able to run independently.
|
|
|
|
|
2008-08-24 16:09:27 +08:00
|
|
|
Other test conditions
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
Regardless of the value of the :setting:`DEBUG` setting in your configuration
|
2010-08-23 16:07:35 +08:00
|
|
|
file, all Django tests run with :setting:`DEBUG`\=False. This is to ensure that
|
2008-08-24 16:09:27 +08:00
|
|
|
the observed output of your code matches what will be seen in a production
|
|
|
|
setting.
|
|
|
|
|
2012-09-22 19:08:40 +08:00
|
|
|
Caches are not cleared after each test, and running "manage.py test fooapp" can
|
|
|
|
insert data from the tests into the cache of a live system if you run your
|
|
|
|
tests in production because, unlike databases, a separate "test cache" is not
|
|
|
|
used. This behavior `may change`_ in the future.
|
|
|
|
|
|
|
|
.. _may change: https://code.djangoproject.com/ticket/11505
|
|
|
|
|
2007-12-17 16:50:32 +08:00
|
|
|
Understanding the test output
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
When you run your tests, you'll see a number of messages as the test runner
|
|
|
|
prepares itself. You can control the level of detail of these messages with the
|
|
|
|
``verbosity`` option on the command line::
|
|
|
|
|
|
|
|
Creating test database...
|
|
|
|
Creating table myapp_animal
|
|
|
|
Creating table myapp_mineral
|
|
|
|
Loading 'initial_data' fixtures...
|
|
|
|
No fixtures found.
|
|
|
|
|
|
|
|
This tells you that the test runner is creating a test database, as described
|
|
|
|
in the previous section.
|
|
|
|
|
2007-08-15 13:24:18 +08:00
|
|
|
Once the test database has been created, Django will run your tests.
|
|
|
|
If everything goes well, you'll see something like this::
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
Ran 22 tests in 0.221s
|
|
|
|
|
|
|
|
OK
|
|
|
|
|
|
|
|
If there are test failures, however, you'll see full details about which tests
|
|
|
|
failed::
|
|
|
|
|
|
|
|
======================================================================
|
2013-05-13 04:29:34 +08:00
|
|
|
FAIL: test_was_published_recently_with_future_poll (polls.tests.PollMethodTests)
|
2007-08-15 13:24:18 +08:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
Traceback (most recent call last):
|
2013-05-13 04:29:34 +08:00
|
|
|
File "/dev/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_poll
|
|
|
|
self.assertEqual(future_poll.was_published_recently(), False)
|
|
|
|
AssertionError: True != False
|
2007-08-15 13:24:18 +08:00
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
2013-05-13 04:29:34 +08:00
|
|
|
Ran 1 test in 0.003s
|
2007-08-15 13:24:18 +08:00
|
|
|
|
|
|
|
FAILED (failures=1)
|
|
|
|
|
|
|
|
A full explanation of this error output is beyond the scope of this document,
|
|
|
|
but it's pretty intuitive. You can consult the documentation of Python's
|
2011-09-05 05:17:30 +08:00
|
|
|
:mod:`unittest` library for details.
|
2007-08-15 13:24:18 +08:00
|
|
|
|
2011-08-07 02:54:21 +08:00
|
|
|
Note that the return code for the test-runner script is 1 for any number of
|
2007-08-15 13:24:18 +08:00
|
|
|
failed and erroneous tests. If all the tests pass, the return code is 0. This
|
|
|
|
feature is useful if you're using the test-runner script in a shell script and
|
|
|
|
need to test for success or failure at that level.
|
|
|
|
|
2012-06-10 02:41:46 +08:00
|
|
|
Speeding up the tests
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
In recent versions of Django, the default password hasher is rather slow by
|
|
|
|
design. If during your tests you are authenticating many users, you may want
|
|
|
|
to use a custom settings file and set the :setting:`PASSWORD_HASHERS` setting
|
|
|
|
to a faster hashing algorithm::
|
|
|
|
|
|
|
|
PASSWORD_HASHERS = (
|
|
|
|
'django.contrib.auth.hashers.MD5PasswordHasher',
|
|
|
|
)
|
|
|
|
|
2012-06-10 03:14:13 +08:00
|
|
|
Don't forget to also include in :setting:`PASSWORD_HASHERS` any hashing
|
|
|
|
algorithm used in fixtures, if any.
|