2011-05-27 18:49:47 +08:00
|
|
|
==========
|
|
|
|
Unit tests
|
|
|
|
==========
|
|
|
|
|
|
|
|
Django comes with a test suite of its own, in the ``tests`` directory of the
|
2011-07-19 21:16:09 +08:00
|
|
|
code base. It's our policy to make sure all tests pass at all times.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
The tests cover:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Models and the database API (``tests/modeltests``),
|
|
|
|
* Everything else in core Django code (``tests/regressiontests``),
|
|
|
|
* :ref:`contrib-apps` (``django/contrib/<app>/tests``).
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
We appreciate any and all contributions to the test suite!
|
|
|
|
|
|
|
|
The Django tests all use the testing infrastructure that ships with Django for
|
|
|
|
testing applications. See :doc:`Testing Django applications </topics/testing>`
|
|
|
|
for an explanation of how to write new tests.
|
|
|
|
|
|
|
|
.. _running-unit-tests:
|
|
|
|
|
|
|
|
Running the unit tests
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
Quickstart
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
|
|
|
Running the tests requires a Django settings module that defines the
|
2011-05-31 17:42:19 +08:00
|
|
|
databases to use. To make it easy to get started, Django provides a
|
2011-05-27 18:49:47 +08:00
|
|
|
sample settings module that uses the SQLite database. To run the tests
|
|
|
|
with this sample ``settings`` module, ``cd`` into the Django
|
|
|
|
``tests/`` directory and run:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
./runtests.py --settings=test_sqlite
|
|
|
|
|
|
|
|
If you get an ``ImportError: No module named django.contrib`` error,
|
2012-06-08 00:48:29 +08:00
|
|
|
you need to add your install of Django to your ``PYTHONPATH``.
|
2011-05-27 18:49:47 +08: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.
|
|
|
|
|
|
|
|
To run the tests with different settings, ``cd`` to the ``tests/`` directory
|
|
|
|
and type:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
./runtests.py --settings=path.to.django.settings
|
|
|
|
|
|
|
|
The :setting:`DATABASES` setting in this test settings module needs to define
|
|
|
|
two databases:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* A ``default`` database. This database should use the backend that
|
|
|
|
you want to use for primary testing
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08: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).
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
If you're using a backend that isn't SQLite, you will need to provide other
|
|
|
|
details for each database:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The :setting:`USER` option for each of your databases needs to
|
|
|
|
specify an existing user account for the database.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The :setting:`PASSWORD` option needs to provide the password for
|
|
|
|
the :setting:`USER` that has been specified.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The :setting:`NAME` option must be the name of an existing database to
|
|
|
|
which the given user has permission to connect. The unit tests will not
|
|
|
|
touch this database; the test runner creates a new database whose name
|
|
|
|
is :setting:`NAME` prefixed with ``test_``, and this test database is
|
|
|
|
deleted when the tests are finished. This means your user account needs
|
|
|
|
permission to execute ``CREATE DATABASE``.
|
2011-05-27 18:49:47 +08: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-30 01:41:04 +08:00
|
|
|
you will need to include a value for :setting:`TEST_CHARSET` in the settings
|
2011-05-27 18:49:47 +08:00
|
|
|
dictionary for the applicable database.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
./runtests.py --settings=path.to.settings generic_relations i18n
|
|
|
|
|
|
|
|
How do you find out the names of individual tests? Look in
|
2011-07-19 21:16:09 +08:00
|
|
|
``tests/modeltests`` and ``tests/regressiontests`` — each directory name
|
|
|
|
there is the name of a test. Contrib app names are also valid test names.
|
2011-05-27 18:49:47 +08: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
|
|
|
|
|
|
|
|
./runtests.py --settings=path.to.settings i18n.TranslationTests
|
|
|
|
|
|
|
|
Going beyond that, you can specify an individual test method like this:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
./runtests.py --settings=path.to.settings i18n.TranslationTests.test_lazy_objects
|
|
|
|
|
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 16:33:58 +08: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
|
|
|
|
install the selenium_ package (version > 2.13) into your Python path.
|
|
|
|
|
|
|
|
Then, run the tests normally, for example:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
./runtests.py --settings=test_sqlite admin_inlines
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
Running all the tests
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
If you want to run the full suite of tests, you'll need to install a number of
|
|
|
|
dependencies:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* PyYAML_
|
|
|
|
* Markdown_
|
|
|
|
* Textile_
|
|
|
|
* Docutils_
|
|
|
|
* setuptools_
|
|
|
|
* memcached_, plus a :ref:`supported Python binding <memcached>`
|
|
|
|
* gettext_ (:ref:`gettext_on_windows`)
|
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 16:33:58 +08:00
|
|
|
* selenium_ (if also using Python >= 2.6)
|
2011-05-27 18:49:47 +08: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.
|
|
|
|
|
|
|
|
Each of these dependencies is optional. If you're missing any of them, the
|
|
|
|
associated tests will be skipped.
|
|
|
|
|
|
|
|
.. _PyYAML: http://pyyaml.org/wiki/PyYAML
|
|
|
|
.. _Markdown: http://pypi.python.org/pypi/Markdown/1.7
|
|
|
|
.. _Textile: http://pypi.python.org/pypi/textile
|
|
|
|
.. _docutils: http://pypi.python.org/pypi/docutils/0.4
|
|
|
|
.. _setuptools: http://pypi.python.org/pypi/setuptools/
|
2012-06-28 16:49:07 +08:00
|
|
|
.. _memcached: http://memcached.org/
|
2011-05-27 18:49:47 +08:00
|
|
|
.. _gettext: http://www.gnu.org/software/gettext/manual/gettext.html
|
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 16:33:58 +08:00
|
|
|
.. _selenium: http://pypi.python.org/pypi/selenium
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-07-19 21:16:09 +08:00
|
|
|
.. _contrib-apps:
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
Contrib apps
|
|
|
|
------------
|
|
|
|
|
2011-07-19 21:16:09 +08: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 18:49:47 +08:00
|
|
|
|
2011-07-19 21:16:09 +08:00
|
|
|
For the tests to be found, a ``models.py`` file must exist, even if it's empty.
|
|
|
|
If you have URLs that need to be mapped, put them in ``tests/urls.py``.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
To run tests for just one contrib app (e.g. ``markup``), use the same
|
|
|
|
method as above::
|
|
|
|
|
|
|
|
./runtests.py --settings=settings markup
|