2011-05-27 18:49:47 +08:00
|
|
|
==========
|
|
|
|
Unit tests
|
|
|
|
==========
|
|
|
|
|
2015-02-19 11:19:21 +08:00
|
|
|
.. highlight:: console
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
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
|
|
|
|
|
|
|
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 19:24:11 +08:00
|
|
|
testing applications. See :doc:`/topics/testing/overview` for an explanation of
|
|
|
|
how to write new tests.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
.. _running-unit-tests:
|
|
|
|
|
|
|
|
Running the unit tests
|
2016-01-03 18:56:22 +08:00
|
|
|
======================
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
Quickstart
|
2016-01-03 18:56:22 +08:00
|
|
|
----------
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2016-11-04 12:47:49 +08:00
|
|
|
First, `fork Django on GitHub <https://github.com/django/django/fork>`__.
|
2014-11-30 00:45:06 +08:00
|
|
|
|
2016-10-27 07:15:33 +08:00
|
|
|
Second, create and activate a virtual environment. If you're not familiar with
|
|
|
|
how to do that, read our :doc:`contributing tutorial </intro/contributing>`.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2016-10-27 07:15:33 +08:00
|
|
|
Next, clone your fork, install some requirements, and run the tests::
|
|
|
|
|
|
|
|
$ git clone git@github.com:YourGitHubName/django.git django-repo
|
2013-12-26 03:54:14 +08:00
|
|
|
$ cd django-repo/tests
|
2016-10-27 07:15:33 +08:00
|
|
|
$ pip install -e ..
|
2017-01-19 00:51:29 +08:00
|
|
|
$ pip install -r requirements/py3.txt
|
2016-10-27 07:15:33 +08:00
|
|
|
$ ./runtests.py
|
|
|
|
|
|
|
|
Installing the requirements will likely require some operating system packages
|
|
|
|
that your computer doesn't have installed. You can usually figure out which
|
|
|
|
package to install by doing a Web search for the last line or so of the error
|
|
|
|
message. Try adding your operating system to the search query if needed.
|
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
If you have trouble installing the requirements, you can skip that step. See
|
2016-10-27 07:15:33 +08:00
|
|
|
:ref:`running-unit-tests-dependencies` for details on installing the optional
|
|
|
|
test dependencies. If you don't have an optional dependency installed, the
|
|
|
|
tests that require it will be skipped.
|
|
|
|
|
|
|
|
Running the tests requires a Django settings module that defines the databases
|
|
|
|
to use. To make it easy to get started, Django provides and uses a sample
|
|
|
|
settings module that uses the SQLite database. See
|
|
|
|
:ref:`running-unit-tests-settings` to learn how to use a different settings
|
|
|
|
module to run the tests with a different database.
|
2013-02-28 16:00:38 +08:00
|
|
|
|
2015-03-22 19:19:22 +08:00
|
|
|
.. admonition:: Windows users
|
|
|
|
|
|
|
|
We recommend something like `Git Bash <https://msysgit.github.io/>`_ to run
|
|
|
|
the tests using the above approach.
|
|
|
|
|
2014-03-16 17:02:55 +08:00
|
|
|
Having problems? See :ref:`troubleshooting-unit-tests` for some common issues.
|
|
|
|
|
2016-06-20 05:08:41 +08:00
|
|
|
Running tests using ``tox``
|
|
|
|
---------------------------
|
|
|
|
|
2017-05-20 23:51:21 +08:00
|
|
|
`Tox <https://tox.readthedocs.io/>`_ is a tool for running tests in different
|
2016-06-20 05:08:41 +08:00
|
|
|
virtual environments. Django includes a basic ``tox.ini`` that automates some
|
|
|
|
checks that our build server performs on pull requests. To run the unit tests
|
|
|
|
and other checks (such as :ref:`import sorting <coding-style-imports>`, the
|
|
|
|
:ref:`documentation spelling checker <documentation-spelling-check>`, and
|
|
|
|
:ref:`code formatting <coding-style-python>`), install and run the ``tox``
|
|
|
|
command from any place in the Django source tree::
|
|
|
|
|
|
|
|
$ pip install tox
|
|
|
|
$ tox
|
|
|
|
|
|
|
|
By default, ``tox`` runs the test suite with the bundled test settings file for
|
|
|
|
SQLite, ``flake8``, ``isort``, and the documentation spelling checker. In
|
|
|
|
addition to the system dependencies noted elsewhere in this documentation,
|
2017-01-19 00:51:29 +08:00
|
|
|
the command ``python3`` must be on your path and linked to the appropriate
|
|
|
|
version of Python. A list of default environments can be seen as follows::
|
2016-06-20 05:08:41 +08:00
|
|
|
|
|
|
|
$ tox -l
|
|
|
|
py3
|
|
|
|
flake8
|
|
|
|
docs
|
|
|
|
isort
|
|
|
|
|
|
|
|
Testing other Python versions and database backends
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
In addition to the default environments, ``tox`` supports running unit tests
|
|
|
|
for other versions of Python and other database backends. Since Django's test
|
|
|
|
suite doesn't bundle a settings file for database backends other than SQLite,
|
|
|
|
however, you must :ref:`create and provide your own test settings
|
2019-01-18 23:04:29 +08:00
|
|
|
<running-unit-tests-settings>`. For example, to run the tests on Python 3.7
|
2016-06-20 05:08:41 +08:00
|
|
|
using PostgreSQL::
|
|
|
|
|
2019-01-18 23:04:29 +08:00
|
|
|
$ tox -e py37-postgres -- --settings=my_postgres_settings
|
2016-06-20 05:08:41 +08:00
|
|
|
|
2019-01-18 23:04:29 +08:00
|
|
|
This command sets up a Python 3.7 virtual environment, installs Django's
|
2016-06-20 05:08:41 +08:00
|
|
|
test suite dependencies (including those for PostgreSQL), and calls
|
|
|
|
``runtests.py`` with the supplied arguments (in this case,
|
|
|
|
``--settings=my_postgres_settings``).
|
|
|
|
|
|
|
|
The remainder of this documentation shows commands for running tests without
|
|
|
|
``tox``, however, any option passed to ``runtests.py`` can also be passed to
|
|
|
|
``tox`` by prefixing the argument list with ``--``, as above.
|
|
|
|
|
|
|
|
Tox also respects the ``DJANGO_SETTINGS_MODULE`` environment variable, if set.
|
|
|
|
For example, the following is equivalent to the command above::
|
|
|
|
|
|
|
|
$ DJANGO_SETTINGS_MODULE=my_postgres_settings tox -e py35-postgres
|
|
|
|
|
|
|
|
Running the JavaScript tests
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Django includes a set of :ref:`JavaScript unit tests <javascript-tests>` for
|
|
|
|
functions in certain contrib apps. The JavaScript tests aren't run by default
|
|
|
|
using ``tox`` because they require `Node.js` to be installed and aren't
|
|
|
|
necessary for the majority of patches. To run the JavaScript tests using
|
|
|
|
``tox``::
|
|
|
|
|
|
|
|
$ tox -e javascript
|
|
|
|
|
|
|
|
This command runs ``npm install`` to ensure test requirements are up to
|
|
|
|
date and then runs ``npm test``.
|
|
|
|
|
2012-10-31 08:09:49 +08:00
|
|
|
.. _running-unit-tests-settings:
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
Using another ``settings`` module
|
2016-01-03 18:56:22 +08:00
|
|
|
---------------------------------
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2016-04-06 03:52:34 +08:00
|
|
|
The included settings module (``tests/test_sqlite.py``) allows you to run the
|
|
|
|
test suite using SQLite. If you want to run the tests using a different
|
|
|
|
database, you'll need to define your own settings file. Some tests, such as
|
|
|
|
those for ``contrib.postgres``, are specific to a particular database backend
|
|
|
|
and will be skipped if run with a different backend.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2013-05-21 01:45:32 +08: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 18:49:47 +08:00
|
|
|
|
2013-05-21 01:45:32 +08:00
|
|
|
The :setting:`DATABASES` setting in any test settings module needs to define
|
2011-05-27 18:49:47 +08:00
|
|
|
two databases:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* A ``default`` database. This database should use the backend that
|
2015-09-06 00:57:05 +08:00
|
|
|
you want to use for primary testing.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2016-03-05 21:39:56 +08:00
|
|
|
* A database with the alias ``other``. The ``other`` database is used to test
|
|
|
|
that queries can be directed to different databases. This database should use
|
|
|
|
the same backend as the ``default``, and it must have a different name.
|
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:
|
|
|
|
|
2013-09-26 00:20:33 +08:00
|
|
|
* The :setting:`USER` option needs to specify an existing user account
|
2013-11-09 16:41:03 +08:00
|
|
|
for the database. That user needs permission to execute ``CREATE DATABASE``
|
|
|
|
so that the test database can be created.
|
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
|
|
|
|
2013-11-09 16:41:03 +08: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.
|
|
|
|
|
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,
|
2015-09-06 00:57:05 +08:00
|
|
|
you will need to include a value for :setting:`CHARSET <TEST_CHARSET>` in the
|
|
|
|
test settings dictionary for the applicable database.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2014-11-07 04:33:43 +08:00
|
|
|
.. _runtests-specifying-labels:
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
Running only some of the tests
|
2016-01-03 18:56:22 +08:00
|
|
|
------------------------------
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
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
|
2015-02-19 11:19:21 +08:00
|
|
|
internationalization, type::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2013-12-26 03:54:14 +08:00
|
|
|
$ ./runtests.py --settings=path.to.settings generic_relations i18n
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2013-02-26 22:00:16 +08:00
|
|
|
How do you find out the names of individual tests? Look in ``tests/`` — each
|
2015-02-11 01:03:47 +08:00
|
|
|
directory name there is the name of a test.
|
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``
|
2015-02-19 11:19:21 +08:00
|
|
|
of the ``i18n`` module, type::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2013-12-26 03:54:14 +08:00
|
|
|
$ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2015-02-19 11:19:21 +08:00
|
|
|
Going beyond that, you can specify an individual test method like this::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2013-12-26 03:54:14 +08:00
|
|
|
$ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
|
2011-05-27 18:49:47 +08: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 16:33:58 +08:00
|
|
|
Running the Selenium tests
|
2016-01-03 18:56:22 +08: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 16:33:58 +08:00
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
Some tests require Selenium and a Web browser. To run these tests, you must
|
|
|
|
install the selenium_ package and run the tests with the
|
|
|
|
``--selenium=<BROWSERS>`` option. For example, if you have Firefox and Google
|
|
|
|
Chrome installed::
|
2013-02-24 00:10:48 +08:00
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
$ ./runtests.py --selenium=firefox,chrome
|
|
|
|
|
|
|
|
See the `selenium.webdriver`_ package for the list of available browsers.
|
2013-02-24 00:10:48 +08:00
|
|
|
|
2016-02-20 02:56:13 +08:00
|
|
|
Specifying ``--selenium`` automatically sets ``--tags=selenium`` to run only
|
|
|
|
the tests that require selenium.
|
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
.. _selenium.webdriver: https://github.com/SeleniumHQ/selenium/tree/master/py/selenium/webdriver
|
|
|
|
|
2012-10-31 08:09:49 +08:00
|
|
|
.. _running-unit-tests-dependencies:
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
Running all the tests
|
2016-01-03 18:56:22 +08:00
|
|
|
---------------------
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
If you want to run the full suite of tests, you'll need to install a number of
|
|
|
|
dependencies:
|
|
|
|
|
2016-04-22 19:26:41 +08:00
|
|
|
* argon2-cffi_ 16.1.0+
|
2013-07-11 00:01:17 +08:00
|
|
|
* bcrypt_
|
|
|
|
* docutils_
|
2015-07-30 05:21:03 +08:00
|
|
|
* geoip2_
|
2015-07-03 20:20:53 +08:00
|
|
|
* jinja2_ 2.7+
|
2013-12-03 01:21:48 +08:00
|
|
|
* numpy_
|
2013-07-02 01:58:04 +08:00
|
|
|
* Pillow_
|
2011-10-14 08:12:01 +08:00
|
|
|
* PyYAML_
|
2016-10-08 09:06:49 +08:00
|
|
|
* pytz_ (required)
|
2019-01-14 09:33:47 +08:00
|
|
|
* pywatchman_
|
2011-10-14 08:12:01 +08:00
|
|
|
* setuptools_
|
|
|
|
* memcached_, plus a :ref:`supported Python binding <memcached>`
|
|
|
|
* gettext_ (:ref:`gettext_on_windows`)
|
2013-07-02 01:58:04 +08:00
|
|
|
* selenium_
|
2018-11-10 08:09:36 +08:00
|
|
|
* sqlparse_ (required)
|
2013-07-02 01:58:04 +08:00
|
|
|
|
|
|
|
You can find these dependencies in `pip requirements files`_ inside the
|
|
|
|
``tests/requirements`` directory of the Django source tree and install them
|
2015-02-19 11:19:21 +08:00
|
|
|
like so::
|
2013-12-26 03:54:14 +08:00
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
$ pip install -r tests/requirements/py3.txt
|
2013-07-02 01:58:04 +08:00
|
|
|
|
2016-07-30 08:03:48 +08:00
|
|
|
If you encounter an error during the installation, your system might be missing
|
|
|
|
a dependency for one or more of the Python packages. Consult the failing
|
|
|
|
package's documentation or search the Web with the error message that you
|
|
|
|
encounter.
|
|
|
|
|
2013-07-02 01:58:04 +08:00
|
|
|
You can also install the database adapter(s) of your choice using
|
|
|
|
``oracle.txt``, ``mysql.txt``, or ``postgres.txt``.
|
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.
|
|
|
|
|
2013-07-02 01:58:04 +08: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 18:49:47 +08:00
|
|
|
Each of these dependencies is optional. If you're missing any of them, the
|
|
|
|
associated tests will be skipped.
|
|
|
|
|
2019-01-14 09:33:47 +08:00
|
|
|
To run some of the autoreload tests, you'll need to install the Watchman_
|
|
|
|
service.
|
|
|
|
|
2018-04-18 06:19:29 +08:00
|
|
|
.. _argon2-cffi: https://pypi.org/project/argon2_cffi/
|
|
|
|
.. _bcrypt: https://pypi.org/project/bcrypt/
|
|
|
|
.. _docutils: https://pypi.org/project/docutils/
|
|
|
|
.. _geoip2: https://pypi.org/project/geoip2/
|
|
|
|
.. _jinja2: https://pypi.org/project/jinja2/
|
|
|
|
.. _numpy: https://pypi.org/project/numpy/
|
|
|
|
.. _Pillow: https://pypi.org/project/Pillow/
|
2018-01-07 21:28:41 +08:00
|
|
|
.. _PyYAML: https://pyyaml.org/wiki/PyYAML
|
2018-04-18 06:19:29 +08:00
|
|
|
.. _pytz: https://pypi.org/project/pytz/
|
2019-01-14 09:33:47 +08:00
|
|
|
.. _pywatchman: https://pypi.org/project/pywatchman/
|
2018-04-18 06:19:29 +08:00
|
|
|
.. _setuptools: https://pypi.org/project/setuptools/
|
2018-01-07 21:28:41 +08:00
|
|
|
.. _memcached: https://memcached.org/
|
2015-11-30 00:29:46 +08:00
|
|
|
.. _gettext: https://www.gnu.org/software/gettext/manual/gettext.html
|
2018-04-18 06:19:29 +08:00
|
|
|
.. _selenium: https://pypi.org/project/selenium/
|
|
|
|
.. _sqlparse: https://pypi.org/project/sqlparse/
|
2017-05-20 23:51:21 +08:00
|
|
|
.. _pip requirements files: https://pip.pypa.io/en/latest/user_guide/#requirements-files
|
2019-01-14 09:33:47 +08:00
|
|
|
.. _Watchman: https://facebook.github.io/watchman/
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2012-10-11 18:11:52 +08:00
|
|
|
Code coverage
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2012-10-11 18:11:52 +08:00
|
|
|
|
|
|
|
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>`.
|
|
|
|
|
2015-09-16 09:32:59 +08:00
|
|
|
Coverage should be run in a single process to obtain accurate statistics. To
|
|
|
|
run coverage on the Django test suite using the standard test settings::
|
2013-12-26 03:54:14 +08:00
|
|
|
|
2015-09-16 09:32:59 +08:00
|
|
|
$ coverage run ./runtests.py --settings=test_sqlite --parallel=1
|
2012-10-11 18:11:52 +08:00
|
|
|
|
2015-02-19 11:19:21 +08:00
|
|
|
After running coverage, generate the html report by running::
|
2012-10-11 18:11:52 +08:00
|
|
|
|
2013-12-26 03:54:14 +08:00
|
|
|
$ coverage html
|
2012-10-11 18:11:52 +08: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 21:16:09 +08:00
|
|
|
.. _contrib-apps:
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
Contrib apps
|
2016-01-03 18:56:22 +08:00
|
|
|
============
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2015-02-11 01:03:47 +08:00
|
|
|
Tests for contrib apps can be found in the ``tests/`` directory, typically
|
|
|
|
under ``<app_name>_tests``. For example, tests for ``contrib.auth`` are located
|
|
|
|
in ``tests/auth_tests``.
|
2014-03-16 17:02:55 +08:00
|
|
|
|
|
|
|
.. _troubleshooting-unit-tests:
|
|
|
|
|
|
|
|
Troubleshooting
|
2016-01-03 18:56:22 +08:00
|
|
|
===============
|
2014-03-16 17:02:55 +08:00
|
|
|
|
2014-11-07 04:33:43 +08:00
|
|
|
Many test failures with ``UnicodeEncodeError``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------------------------------------
|
2014-03-16 17:02:55 +08:00
|
|
|
|
|
|
|
If the ``locales`` package is not installed, some tests will fail with a
|
|
|
|
``UnicodeEncodeError``.
|
|
|
|
|
2015-02-19 11:19:21 +08:00
|
|
|
You can resolve this on Debian-based systems, for example, by running::
|
2014-03-16 17:02:55 +08:00
|
|
|
|
|
|
|
$ apt-get install locales
|
|
|
|
$ dpkg-reconfigure locales
|
2014-11-07 04:33:43 +08:00
|
|
|
|
2016-11-05 23:25:16 +08:00
|
|
|
You can resolve this for macOS systems by configuring your shell's locale::
|
|
|
|
|
|
|
|
$ export LANG="en_US.UTF-8"
|
|
|
|
$ export LC_ALL="en_US.UTF-8"
|
|
|
|
|
|
|
|
Run the ``locale`` command to confirm the change. Optionally, add those export
|
|
|
|
commands to your shell's startup file (e.g. ``~/.bashrc`` for Bash) to avoid
|
|
|
|
having to retype them.
|
|
|
|
|
2014-11-07 04:33:43 +08:00
|
|
|
Tests that only fail in combination
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------------------------
|
2014-11-07 04:33:43 +08:00
|
|
|
|
|
|
|
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-23 00:54:42 +08: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-07 04:33:43 +08:00
|
|
|
|
|
|
|
For example, suppose that the failing test that works on its own is
|
2015-02-19 11:19:21 +08:00
|
|
|
``ModelTest.test_eq``, then using::
|
2014-11-07 04:33:43 +08:00
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
$ ./runtests.py --bisect basic.tests.ModelTest.test_eq
|
2014-11-07 04:33:43 +08: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
|
2015-02-19 11:19:21 +08:00
|
|
|
failure. So::
|
2014-11-07 04:33:43 +08:00
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
$ ./runtests.py --pair basic.tests.ModelTest.test_eq
|
2014-11-07 04:33:43 +08: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
|
2015-02-19 11:19:21 +08:00
|
|
|
the first one::
|
2014-11-07 04:33:43 +08:00
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
$ ./runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
|
2014-11-23 00:59:05 +08: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
|
2015-02-19 11:19:21 +08:00
|
|
|
cause any trouble::
|
2014-11-23 00:59:05 +08:00
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
$ ./runtests.py basic --reverse
|
|
|
|
|
2015-11-16 22:12:26 +08:00
|
|
|
Seeing the SQL queries run during a test
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------------------------------
|
2015-11-16 22:12:26 +08:00
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
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
|
2015-02-19 11:19:21 +08:00
|
|
|
combine this with ``--verbosity=2``, all SQL queries will be output::
|
2015-01-11 06:52:59 +08:00
|
|
|
|
|
|
|
$ ./runtests.py basic --debug-sql
|
2014-11-23 00:59:05 +08:00
|
|
|
|
2015-11-16 22:12:26 +08:00
|
|
|
Seeing the full traceback of a test failure
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------------------------------------
|
2015-11-16 22:12:26 +08:00
|
|
|
|
|
|
|
By default tests are run in parallel with one process per core. When the tests
|
|
|
|
are run in parallel, however, you'll only see a truncated traceback for any
|
|
|
|
test failures. You can adjust this behavior with the ``--parallel`` option::
|
2015-09-06 17:12:08 +08:00
|
|
|
|
|
|
|
$ ./runtests.py basic --parallel=1
|
|
|
|
|
|
|
|
You can also use the ``DJANGO_TEST_PROCESSES`` environment variable for this
|
|
|
|
purpose.
|
|
|
|
|
2015-11-17 13:33:18 +08:00
|
|
|
Tips for writing tests
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
.. highlight:: python
|
|
|
|
|
|
|
|
Isolating model registration
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
To avoid polluting the global :attr:`~django.apps.apps` registry and prevent
|
|
|
|
unnecessary table creation, models defined in a test method should be bound to
|
|
|
|
a temporary ``Apps`` instance::
|
|
|
|
|
|
|
|
from django.apps.registry import Apps
|
|
|
|
from django.db import models
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
class TestModelDefinition(SimpleTestCase):
|
|
|
|
def test_model_definition(self):
|
|
|
|
test_apps = Apps(['app_label'])
|
|
|
|
|
|
|
|
class TestModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
apps = test_apps
|
|
|
|
...
|
|
|
|
|
|
|
|
.. function:: django.test.utils.isolate_apps(*app_labels, attr_name=None, kwarg_name=None)
|
|
|
|
|
|
|
|
Since this pattern involves a lot of boilerplate, Django provides the
|
|
|
|
:func:`~django.test.utils.isolate_apps` decorator. It's used like this::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.utils import isolate_apps
|
|
|
|
|
|
|
|
class TestModelDefinition(SimpleTestCase):
|
|
|
|
@isolate_apps('app_label')
|
|
|
|
def test_model_definition(self):
|
|
|
|
class TestModel(models.Model):
|
|
|
|
pass
|
|
|
|
...
|
|
|
|
|
|
|
|
.. admonition:: Setting ``app_label``
|
|
|
|
|
|
|
|
Models defined in a test method with no explicit
|
|
|
|
:attr:`~django.db.models.Options.app_label` are automatically assigned the
|
|
|
|
label of the app in which their test class is located.
|
|
|
|
|
|
|
|
In order to make sure the models defined within the context of
|
|
|
|
:func:`~django.test.utils.isolate_apps` instances are correctly
|
|
|
|
installed, you should pass the set of targeted ``app_label`` as arguments:
|
|
|
|
|
2018-09-11 01:00:34 +08:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: tests/app_label/tests.py
|
2015-11-17 13:33:18 +08:00
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.utils import isolate_apps
|
|
|
|
|
|
|
|
class TestModelDefinition(SimpleTestCase):
|
|
|
|
@isolate_apps('app_label', 'other_app_label')
|
|
|
|
def test_model_definition(self):
|
|
|
|
# This model automatically receives app_label='app_label'
|
|
|
|
class TestModel(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class OtherAppModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
app_label = 'other_app_label'
|
|
|
|
...
|
|
|
|
|
|
|
|
The decorator can also be applied to classes::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.utils import isolate_apps
|
|
|
|
|
|
|
|
@isolate_apps('app_label')
|
|
|
|
class TestModelDefinition(SimpleTestCase):
|
|
|
|
def test_model_definition(self):
|
|
|
|
class TestModel(models.Model):
|
|
|
|
pass
|
|
|
|
...
|
|
|
|
|
|
|
|
The temporary ``Apps`` instance used to isolate model registration can be
|
|
|
|
retrieved as an attribute when used as a class decorator by using the
|
|
|
|
``attr_name`` parameter::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.utils import isolate_apps
|
|
|
|
|
|
|
|
@isolate_apps('app_label', attr_name='apps')
|
|
|
|
class TestModelDefinition(SimpleTestCase):
|
|
|
|
def test_model_definition(self):
|
|
|
|
class TestModel(models.Model):
|
|
|
|
pass
|
|
|
|
self.assertIs(self.apps.get_model('app_label', 'TestModel'), TestModel)
|
|
|
|
|
|
|
|
Or as an argument on the test method when used as a method decorator by using
|
|
|
|
the ``kwarg_name`` parameter::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.utils import isolate_apps
|
|
|
|
|
|
|
|
class TestModelDefinition(SimpleTestCase):
|
|
|
|
@isolate_apps('app_label', kwarg_name='apps')
|
|
|
|
def test_model_definition(self, apps):
|
|
|
|
class TestModel(models.Model):
|
|
|
|
pass
|
|
|
|
self.assertIs(apps.get_model('app_label', 'TestModel'), TestModel)
|