2011-05-27 10:49:47 +00:00
|
|
|
==========
|
|
|
|
Unit tests
|
|
|
|
==========
|
|
|
|
|
|
|
|
Django comes with a test suite of its own, in the ``tests`` directory of the
|
2011-07-19 13:16:09 +00:00
|
|
|
code base. It's our policy to make sure all tests pass at all times.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
The tests cover:
|
|
|
|
|
2013-02-26 14:00:16 +00:00
|
|
|
* Models, the database API and everything else in core Django core (``tests/``),
|
|
|
|
* :ref:`contrib-apps` (``django/contrib/<app>/tests`` or ``tests/<app>_...``).
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
We appreciate any and all contributions to the test suite!
|
|
|
|
|
|
|
|
The Django tests all use the testing infrastructure that ships with Django for
|
2013-12-31 11:24:11 +00:00
|
|
|
testing applications. See :doc:`/topics/testing/overview` for an explanation of
|
|
|
|
how to write new tests.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
.. _running-unit-tests:
|
|
|
|
|
|
|
|
Running the unit tests
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
Quickstart
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2014-11-29 16:45:06 +00:00
|
|
|
If you are on Python < 3.3, you'll first need to install a backport of the
|
|
|
|
``unittest.mock`` module that's available in Python 3.3+. See
|
|
|
|
:ref:`running-unit-tests-dependencies` for details on installing `mock`_ and
|
|
|
|
the other optional test dependencies.
|
|
|
|
|
2011-05-27 10:49:47 +00:00
|
|
|
Running the tests requires a Django settings module that defines the
|
2013-02-28 08:00:38 +00:00
|
|
|
databases to use. To make it easy to get started, Django provides and uses a
|
|
|
|
sample settings module that uses the SQLite database. To run the tests:
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-08-26 23:39:07 +00:00
|
|
|
$ git clone https://github.com/django/django.git django-repo
|
2013-12-25 19:54:14 +00:00
|
|
|
$ cd django-repo/tests
|
|
|
|
$ PYTHONPATH=..:$PYTHONPATH ./runtests.py
|
2013-02-28 08:00:38 +00:00
|
|
|
|
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
2014-10-23 05:04:47 +00:00
|
|
|
Older versions of Django required specifying a settings file:
|
2013-02-28 08:00:38 +00:00
|
|
|
|
2014-10-23 05:04:47 +00:00
|
|
|
.. code-block:: bash
|
2013-12-25 19:54:14 +00:00
|
|
|
|
2014-10-23 05:04:47 +00:00
|
|
|
$ PYTHONPATH=..:$PYTHONPATH python ./runtests.py --settings=test_sqlite
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2014-10-23 05:04:47 +00:00
|
|
|
``runtests.py`` now uses ``test_sqlite`` by default if settings aren't
|
|
|
|
provided through either ``--settings`` or :envvar:`DJANGO_SETTINGS_MODULE`.
|
2013-10-22 14:52:04 +00:00
|
|
|
|
|
|
|
You can avoid typing the ``PYTHONPATH`` bit each time by adding your Django
|
|
|
|
checkout to your ``PYTHONPATH`` or by installing the source checkout using pip.
|
|
|
|
See :ref:`installing-development-version`.
|
2013-02-28 08:00:38 +00:00
|
|
|
|
2014-03-16 09:02:55 +00:00
|
|
|
Having problems? See :ref:`troubleshooting-unit-tests` for some common issues.
|
|
|
|
|
2012-10-31 00:09:49 +00:00
|
|
|
.. _running-unit-tests-settings:
|
|
|
|
|
2011-05-27 10:49:47 +00:00
|
|
|
Using another ``settings`` module
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The included settings module allows you to run the test suite using
|
|
|
|
SQLite. If you want to test behavior using a different database (and
|
|
|
|
if you're proposing patches for Django, it's a good idea to test
|
|
|
|
across databases), you may need to define your own settings file.
|
|
|
|
|
2013-05-20 17:45:32 +00:00
|
|
|
To run the tests with different settings, ensure that the module is on your
|
|
|
|
``PYTHONPATH`` and pass the module with ``--settings``.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2013-05-20 17:45:32 +00:00
|
|
|
The :setting:`DATABASES` setting in any test settings module needs to define
|
2011-05-27 10:49:47 +00:00
|
|
|
two databases:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* A ``default`` database. This database should use the backend that
|
|
|
|
you want to use for primary testing
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2013-10-09 15:42:27 +00:00
|
|
|
* A database with the alias ``other``. The ``other`` database is used to
|
|
|
|
establish that queries can be directed to different databases. As a result,
|
|
|
|
this database can use any backend you want. It doesn't need to use the same
|
|
|
|
backend as the ``default`` database (although it can use the same backend if
|
|
|
|
you want to). It cannot be the same database as the ``default``.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
If you're using a backend that isn't SQLite, you will need to provide other
|
|
|
|
details for each database:
|
|
|
|
|
2013-09-25 16:20:33 +00:00
|
|
|
* The :setting:`USER` option needs to specify an existing user account
|
2013-11-09 08:41:03 +00:00
|
|
|
for the database. That user needs permission to execute ``CREATE DATABASE``
|
|
|
|
so that the test database can be created.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* The :setting:`PASSWORD` option needs to provide the password for
|
|
|
|
the :setting:`USER` that has been specified.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2013-11-09 08:41:03 +00:00
|
|
|
Test databases get their names by prepending ``test_`` to the value of the
|
|
|
|
:setting:`NAME` settings for the databases defined in :setting:`DATABASES`.
|
|
|
|
These test databases are deleted when the tests are finished.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.7
|
|
|
|
|
|
|
|
Before Django 1.7, the :setting:`NAME` setting was mandatory and had to
|
|
|
|
be the name of an existing database to which the given user had permission
|
|
|
|
to connect.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
You will also need to ensure that your database uses UTF-8 as the default
|
|
|
|
character set. If your database server doesn't use UTF-8 as a default charset,
|
2011-05-29 17:41:04 +00:00
|
|
|
you will need to include a value for :setting:`TEST_CHARSET` in the settings
|
2011-05-27 10:49:47 +00:00
|
|
|
dictionary for the applicable database.
|
|
|
|
|
2014-11-06 20:33:43 +00:00
|
|
|
.. _runtests-specifying-labels:
|
|
|
|
|
2011-05-27 10:49:47 +00:00
|
|
|
Running only some of the tests
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Django's entire test suite takes a while to run, and running every single test
|
|
|
|
could be redundant if, say, you just added a test to Django that you want to
|
|
|
|
run quickly without running everything else. You can run a subset of the unit
|
|
|
|
tests by appending the names of the test modules to ``runtests.py`` on the
|
|
|
|
command line.
|
|
|
|
|
|
|
|
For example, if you'd like to run tests only for generic relations and
|
|
|
|
internationalization, type:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
$ ./runtests.py --settings=path.to.settings generic_relations i18n
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2013-02-26 14:00:16 +00:00
|
|
|
How do you find out the names of individual tests? Look in ``tests/`` — each
|
|
|
|
directory name there is the name of a test. Contrib app names are also valid
|
|
|
|
test names.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
If you just want to run a particular class of tests, you can specify a list of
|
|
|
|
paths to individual test classes. For example, to run the ``TranslationTests``
|
|
|
|
of the ``i18n`` module, type:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
$ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
Going beyond that, you can specify an individual test method like this:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
$ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
|
2011-05-27 10:49:47 +00:00
|
|
|
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 08:33:58 +00:00
|
|
|
Running the Selenium tests
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Some admin tests require Selenium 2, Firefox and Python >= 2.6 to work via a
|
|
|
|
real Web browser. To allow those tests to run and not be skipped, you must
|
2013-02-25 17:14:42 +00:00
|
|
|
install the selenium_ package (version > 2.13) into your Python path and run
|
|
|
|
the tests with the ``--selenium`` option:
|
2013-02-23 16:10:48 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
$ ./runtests.py --settings=test_sqlite --selenium admin_inlines
|
2013-02-23 16:10:48 +00:00
|
|
|
|
|
|
|
|
2012-10-31 00:09:49 +00:00
|
|
|
.. _running-unit-tests-dependencies:
|
|
|
|
|
2011-05-27 10:49:47 +00:00
|
|
|
Running all the tests
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
If you want to run the full suite of tests, you'll need to install a number of
|
|
|
|
dependencies:
|
|
|
|
|
2013-07-10 16:01:17 +00:00
|
|
|
* bcrypt_
|
|
|
|
* docutils_
|
2013-12-02 17:21:48 +00:00
|
|
|
* numpy_
|
2013-07-01 17:58:04 +00:00
|
|
|
* Pillow_
|
2011-10-14 00:12:01 +00:00
|
|
|
* PyYAML_
|
2013-07-01 17:58:04 +00:00
|
|
|
* pytz_
|
2011-10-14 00:12:01 +00:00
|
|
|
* setuptools_
|
|
|
|
* memcached_, plus a :ref:`supported Python binding <memcached>`
|
2014-11-29 16:45:06 +00:00
|
|
|
* mock_ (for Python < 3.3)
|
2011-10-14 00:12:01 +00:00
|
|
|
* gettext_ (:ref:`gettext_on_windows`)
|
2013-07-01 17:58:04 +00:00
|
|
|
* selenium_
|
2014-04-26 08:22:48 +00:00
|
|
|
* sqlparse_
|
2013-07-01 17:58:04 +00:00
|
|
|
|
|
|
|
You can find these dependencies in `pip requirements files`_ inside the
|
|
|
|
``tests/requirements`` directory of the Django source tree and install them
|
2013-12-25 19:54:14 +00:00
|
|
|
like so:
|
2013-07-01 17:58:04 +00:00
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-11-29 16:45:06 +00:00
|
|
|
$ pip install -r tests/requirements/py3.txt # Python 2: py2.txt
|
2013-07-01 17:58:04 +00:00
|
|
|
|
|
|
|
You can also install the database adapter(s) of your choice using
|
|
|
|
``oracle.txt``, ``mysql.txt``, or ``postgres.txt``.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
|
|
|
If you want to test the memcached cache backend, you'll also need to define
|
|
|
|
a :setting:`CACHES` setting that points at your memcached instance.
|
|
|
|
|
2013-07-01 17:58:04 +00:00
|
|
|
To run the GeoDjango tests, you will need to :doc:`setup a spatial database
|
|
|
|
and install the Geospatial libraries</ref/contrib/gis/install/index>`.
|
|
|
|
|
2011-05-27 10:49:47 +00:00
|
|
|
Each of these dependencies is optional. If you're missing any of them, the
|
|
|
|
associated tests will be skipped.
|
|
|
|
|
2013-07-10 16:01:17 +00:00
|
|
|
.. _bcrypt: https://pypi.python.org/pypi/bcrypt
|
|
|
|
.. _docutils: https://pypi.python.org/pypi/docutils
|
2013-12-02 17:21:48 +00:00
|
|
|
.. _numpy: https://pypi.python.org/pypi/numpy
|
2013-07-01 17:58:04 +00:00
|
|
|
.. _Pillow: https://pypi.python.org/pypi/Pillow/
|
2011-05-27 10:49:47 +00:00
|
|
|
.. _PyYAML: http://pyyaml.org/wiki/PyYAML
|
2013-07-01 17:58:04 +00:00
|
|
|
.. _pytz: https://pypi.python.org/pypi/pytz/
|
2013-12-08 17:39:26 +00:00
|
|
|
.. _setuptools: https://pypi.python.org/pypi/setuptools/
|
2012-06-28 08:49:07 +00:00
|
|
|
.. _memcached: http://memcached.org/
|
2014-11-29 16:45:06 +00:00
|
|
|
.. _mock: https://pypi.python.org/pypi/mock
|
2011-05-27 10:49:47 +00:00
|
|
|
.. _gettext: http://www.gnu.org/software/gettext/manual/gettext.html
|
2013-12-08 17:39:26 +00:00
|
|
|
.. _selenium: https://pypi.python.org/pypi/selenium
|
2014-04-26 08:22:48 +00:00
|
|
|
.. _sqlparse: https://pypi.python.org/pypi/sqlparse
|
2014-08-05 12:23:34 +00:00
|
|
|
.. _pip requirements files: http://www.pip-installer.org/en/latest/user_guide.html#requirements-files
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2012-10-11 10:11:52 +00:00
|
|
|
Code coverage
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Contributors are encouraged to run coverage on the test suite to identify areas
|
|
|
|
that need additional tests. The coverage tool installation and use is described
|
|
|
|
in :ref:`testing code coverage<topics-testing-code-coverage>`.
|
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
To run coverage on the Django test suite using the standard test settings:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ coverage run ./runtests.py --settings=test_sqlite
|
2012-10-11 10:11:52 +00:00
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
After running coverage, generate the html report by running:
|
2012-10-11 10:11:52 +00:00
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
.. code-block:: bash
|
2012-10-11 10:11:52 +00:00
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
$ coverage html
|
2012-10-11 10:11:52 +00:00
|
|
|
|
|
|
|
When running coverage for the Django tests, the included ``.coveragerc``
|
|
|
|
settings file defines ``coverage_html`` as the output directory for the report
|
|
|
|
and also excludes several directories not relevant to the results
|
|
|
|
(test code or external code included in Django).
|
|
|
|
|
2011-07-19 13:16:09 +00:00
|
|
|
.. _contrib-apps:
|
|
|
|
|
2011-05-27 10:49:47 +00:00
|
|
|
Contrib apps
|
|
|
|
------------
|
|
|
|
|
2011-07-19 13:16:09 +00:00
|
|
|
Tests for contrib apps go in their respective directories under
|
|
|
|
``django/contrib``, in a ``tests.py`` file. You can split the tests over
|
|
|
|
multiple modules by using a ``tests`` directory in the normal Python way.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2011-07-19 13:16:09 +00:00
|
|
|
If you have URLs that need to be mapped, put them in ``tests/urls.py``.
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2012-12-24 22:14:06 +00:00
|
|
|
To run tests for just one contrib app (e.g. ``auth``), use the same
|
2013-12-25 19:54:14 +00:00
|
|
|
method as above:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2011-05-27 10:49:47 +00:00
|
|
|
|
2013-12-25 19:54:14 +00:00
|
|
|
$ ./runtests.py --settings=settings django.contrib.auth
|
2014-03-16 09:02:55 +00:00
|
|
|
|
|
|
|
.. _troubleshooting-unit-tests:
|
|
|
|
|
|
|
|
Troubleshooting
|
|
|
|
---------------
|
|
|
|
|
2014-11-06 20:33:43 +00:00
|
|
|
Many test failures with ``UnicodeEncodeError``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2014-03-16 09:02:55 +00:00
|
|
|
|
|
|
|
If the ``locales`` package is not installed, some tests will fail with a
|
|
|
|
``UnicodeEncodeError``.
|
|
|
|
|
|
|
|
You can resolve this on Debian-based systems, for example, by running:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ apt-get install locales
|
|
|
|
$ dpkg-reconfigure locales
|
2014-11-06 20:33:43 +00:00
|
|
|
|
|
|
|
Tests that only fail in combination
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
In case a test passes when run in isolation but fails within the whole suite,
|
|
|
|
we have some tools to help analyze the problem.
|
|
|
|
|
2014-11-22 16:54:42 +00:00
|
|
|
The ``--bisect`` option of ``runtests.py`` will run the failing test while
|
|
|
|
halving the test set it is run together with on each iteration, often making
|
|
|
|
it possible to identify a small number of tests that may be related to the
|
|
|
|
failure.
|
2014-11-06 20:33:43 +00:00
|
|
|
|
|
|
|
For example, suppose that the failing test that works on its own is
|
|
|
|
``ModelTest.test_eq``, then using:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2015-01-10 22:52:59 +00:00
|
|
|
$ ./runtests.py --bisect basic.tests.ModelTest.test_eq
|
2014-11-06 20:33:43 +00:00
|
|
|
|
|
|
|
will try to determine a test that interferes with the given one. First, the
|
|
|
|
test is run with the first half of the test suite. If a failure occurs, the
|
|
|
|
first half of the test suite is split in two groups and each group is then run
|
|
|
|
with the specified test. If there is no failure with the first half of the test
|
|
|
|
suite, the second half of the test suite is run with the specified test and
|
|
|
|
split appropriately as described earlier. The process repeats until the set of
|
|
|
|
failing tests is minimized.
|
|
|
|
|
|
|
|
The ``--pair`` option runs the given test alongside every other test from the
|
|
|
|
suite, letting you check if another test has side-effects that cause the
|
|
|
|
failure. So:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2015-01-10 22:52:59 +00:00
|
|
|
$ ./runtests.py --pair basic.tests.ModelTest.test_eq
|
2014-11-06 20:33:43 +00:00
|
|
|
|
|
|
|
will pair ``test_eq`` with every test label.
|
|
|
|
|
|
|
|
With both ``--bisect`` and ``--pair``, if you already suspect which cases
|
|
|
|
might be responsible for the failure, you may limit tests to be cross-analyzed
|
|
|
|
by :ref:`specifying further test labels <runtests-specifying-labels>` after
|
|
|
|
the first one:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2015-01-10 22:52:59 +00:00
|
|
|
$ ./runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
|
2014-11-22 16:59:05 +00:00
|
|
|
|
|
|
|
You can also try running any set of tests in reverse using the ``--reverse``
|
|
|
|
option in order to verify that executing tests in a different order does not
|
|
|
|
cause any trouble:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2015-01-10 22:52:59 +00:00
|
|
|
$ ./runtests.py basic --reverse
|
|
|
|
|
|
|
|
If you wish to examine the SQL being run in failing tests, you can turn on
|
|
|
|
:ref:`SQL logging <django-db-logger>` using the ``--debug-sql`` option. If you
|
|
|
|
combine this with ``--verbosity=2``, all SQL queries will be output.
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ ./runtests.py basic --debug-sql
|
2014-11-22 16:59:05 +00:00
|
|
|
|
|
|
|
.. versionadded:: 1.8
|
|
|
|
|
2015-01-10 22:52:59 +00:00
|
|
|
The ``--reverse`` and ``--debug-sql`` options were added.
|