unit-tests.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. ==========
  2. Unit tests
  3. ==========
  4. Django comes with a test suite of its own, in the ``tests`` directory of the
  5. code base. It's our policy to make sure all tests pass at all times.
  6. The tests cover:
  7. * Models, the database API and everything else in core Django core (``tests/``),
  8. * :ref:`contrib-apps` (``django/contrib/<app>/tests`` or ``tests/<app>_...``).
  9. We appreciate any and all contributions to the test suite!
  10. The Django tests all use the testing infrastructure that ships with Django for
  11. testing applications. See :doc:`/topics/testing/overview` for an explanation of
  12. how to write new tests.
  13. .. _running-unit-tests:
  14. Running the unit tests
  15. ----------------------
  16. Quickstart
  17. ~~~~~~~~~~
  18. Running the tests requires a Django settings module that defines the
  19. databases to use. To make it easy to get started, Django provides and uses a
  20. sample settings module that uses the SQLite database. To run the tests:
  21. .. code-block:: bash
  22. $ git clone https://github.com/django/django.git django-repo
  23. $ cd django-repo/tests
  24. $ PYTHONPATH=..:$PYTHONPATH ./runtests.py
  25. .. versionchanged:: 1.7
  26. Older versions of Django required specifying a settings file:
  27. .. code-block:: bash
  28. $ PYTHONPATH=..:$PYTHONPATH python ./runtests.py --settings=test_sqlite
  29. ``runtests.py`` now uses ``test_sqlite`` by default if settings aren't
  30. provided through either ``--settings`` or :envvar:`DJANGO_SETTINGS_MODULE`.
  31. You can avoid typing the ``PYTHONPATH`` bit each time by adding your Django
  32. checkout to your ``PYTHONPATH`` or by installing the source checkout using pip.
  33. See :ref:`installing-development-version`.
  34. Having problems? See :ref:`troubleshooting-unit-tests` for some common issues.
  35. .. _running-unit-tests-settings:
  36. Using another ``settings`` module
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. The included settings module allows you to run the test suite using
  39. SQLite. If you want to test behavior using a different database (and
  40. if you're proposing patches for Django, it's a good idea to test
  41. across databases), you may need to define your own settings file.
  42. To run the tests with different settings, ensure that the module is on your
  43. ``PYTHONPATH`` and pass the module with ``--settings``.
  44. The :setting:`DATABASES` setting in any test settings module needs to define
  45. two databases:
  46. * A ``default`` database. This database should use the backend that
  47. you want to use for primary testing
  48. * A database with the alias ``other``. The ``other`` database is used to
  49. establish that queries can be directed to different databases. As a result,
  50. this database can use any backend you want. It doesn't need to use the same
  51. backend as the ``default`` database (although it can use the same backend if
  52. you want to). It cannot be the same database as the ``default``.
  53. If you're using a backend that isn't SQLite, you will need to provide other
  54. details for each database:
  55. * The :setting:`USER` option needs to specify an existing user account
  56. for the database. That user needs permission to execute ``CREATE DATABASE``
  57. so that the test database can be created.
  58. * The :setting:`PASSWORD` option needs to provide the password for
  59. the :setting:`USER` that has been specified.
  60. Test databases get their names by prepending ``test_`` to the value of the
  61. :setting:`NAME` settings for the databases defined in :setting:`DATABASES`.
  62. These test databases are deleted when the tests are finished.
  63. .. versionchanged:: 1.7
  64. Before Django 1.7, the :setting:`NAME` setting was mandatory and had to
  65. be the name of an existing database to which the given user had permission
  66. to connect.
  67. You will also need to ensure that your database uses UTF-8 as the default
  68. character set. If your database server doesn't use UTF-8 as a default charset,
  69. you will need to include a value for :setting:`TEST_CHARSET` in the settings
  70. dictionary for the applicable database.
  71. .. _runtests-specifying-labels:
  72. Running only some of the tests
  73. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. Django's entire test suite takes a while to run, and running every single test
  75. could be redundant if, say, you just added a test to Django that you want to
  76. run quickly without running everything else. You can run a subset of the unit
  77. tests by appending the names of the test modules to ``runtests.py`` on the
  78. command line.
  79. For example, if you'd like to run tests only for generic relations and
  80. internationalization, type:
  81. .. code-block:: bash
  82. $ ./runtests.py --settings=path.to.settings generic_relations i18n
  83. How do you find out the names of individual tests? Look in ``tests/`` — each
  84. directory name there is the name of a test. Contrib app names are also valid
  85. test names.
  86. If you just want to run a particular class of tests, you can specify a list of
  87. paths to individual test classes. For example, to run the ``TranslationTests``
  88. of the ``i18n`` module, type:
  89. .. code-block:: bash
  90. $ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
  91. Going beyond that, you can specify an individual test method like this:
  92. .. code-block:: bash
  93. $ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
  94. Running the Selenium tests
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. Some admin tests require Selenium 2, Firefox and Python >= 2.6 to work via a
  97. real Web browser. To allow those tests to run and not be skipped, you must
  98. install the selenium_ package (version > 2.13) into your Python path and run
  99. the tests with the ``--selenium`` option:
  100. .. code-block:: bash
  101. $ ./runtests.py --settings=test_sqlite --selenium admin_inlines
  102. .. _running-unit-tests-dependencies:
  103. Running all the tests
  104. ~~~~~~~~~~~~~~~~~~~~~
  105. If you want to run the full suite of tests, you'll need to install a number of
  106. dependencies:
  107. * bcrypt_
  108. * docutils_
  109. * numpy_
  110. * Pillow_
  111. * PyYAML_
  112. * pytz_
  113. * setuptools_
  114. * memcached_, plus a :ref:`supported Python binding <memcached>`
  115. * gettext_ (:ref:`gettext_on_windows`)
  116. * selenium_
  117. * sqlparse_
  118. You can find these dependencies in `pip requirements files`_ inside the
  119. ``tests/requirements`` directory of the Django source tree and install them
  120. like so:
  121. .. code-block:: bash
  122. $ pip install -r tests/requirements/py2.txt # Python 3: py3.txt
  123. You can also install the database adapter(s) of your choice using
  124. ``oracle.txt``, ``mysql.txt``, or ``postgres.txt``.
  125. If you want to test the memcached cache backend, you'll also need to define
  126. a :setting:`CACHES` setting that points at your memcached instance.
  127. To run the GeoDjango tests, you will need to :doc:`setup a spatial database
  128. and install the Geospatial libraries</ref/contrib/gis/install/index>`.
  129. Each of these dependencies is optional. If you're missing any of them, the
  130. associated tests will be skipped.
  131. .. _bcrypt: https://pypi.python.org/pypi/bcrypt
  132. .. _docutils: https://pypi.python.org/pypi/docutils
  133. .. _numpy: https://pypi.python.org/pypi/numpy
  134. .. _Pillow: https://pypi.python.org/pypi/Pillow/
  135. .. _PyYAML: http://pyyaml.org/wiki/PyYAML
  136. .. _pytz: https://pypi.python.org/pypi/pytz/
  137. .. _setuptools: https://pypi.python.org/pypi/setuptools/
  138. .. _memcached: http://memcached.org/
  139. .. _gettext: http://www.gnu.org/software/gettext/manual/gettext.html
  140. .. _selenium: https://pypi.python.org/pypi/selenium
  141. .. _sqlparse: https://pypi.python.org/pypi/sqlparse
  142. .. _pip requirements files: http://www.pip-installer.org/en/latest/user_guide.html#requirements-files
  143. Code coverage
  144. ~~~~~~~~~~~~~
  145. Contributors are encouraged to run coverage on the test suite to identify areas
  146. that need additional tests. The coverage tool installation and use is described
  147. in :ref:`testing code coverage<topics-testing-code-coverage>`.
  148. To run coverage on the Django test suite using the standard test settings:
  149. .. code-block:: bash
  150. $ coverage run ./runtests.py --settings=test_sqlite
  151. After running coverage, generate the html report by running:
  152. .. code-block:: bash
  153. $ coverage html
  154. When running coverage for the Django tests, the included ``.coveragerc``
  155. settings file defines ``coverage_html`` as the output directory for the report
  156. and also excludes several directories not relevant to the results
  157. (test code or external code included in Django).
  158. .. _contrib-apps:
  159. Contrib apps
  160. ------------
  161. Tests for contrib apps go in their respective directories under
  162. ``django/contrib``, in a ``tests.py`` file. You can split the tests over
  163. multiple modules by using a ``tests`` directory in the normal Python way.
  164. If you have URLs that need to be mapped, put them in ``tests/urls.py``.
  165. To run tests for just one contrib app (e.g. ``auth``), use the same
  166. method as above:
  167. .. code-block:: bash
  168. $ ./runtests.py --settings=settings django.contrib.auth
  169. .. _troubleshooting-unit-tests:
  170. Troubleshooting
  171. ---------------
  172. Many test failures with ``UnicodeEncodeError``
  173. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  174. If the ``locales`` package is not installed, some tests will fail with a
  175. ``UnicodeEncodeError``.
  176. You can resolve this on Debian-based systems, for example, by running:
  177. .. code-block:: bash
  178. $ apt-get install locales
  179. $ dpkg-reconfigure locales
  180. Tests that only fail in combination
  181. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  182. In case a test passes when run in isolation but fails within the whole suite,
  183. we have some tools to help analyze the problem.
  184. The ``--bisect`` option of ``runtests.py`` will run the failing test while
  185. halving the test set it is run together with on each iteration, often making
  186. it possible to identify a small number of tests that may be related to the
  187. failure.
  188. For example, suppose that the failing test that works on its own is
  189. ``ModelTest.test_eq``, then using:
  190. .. code-block:: bash
  191. $ ./runtests.py --bisect basic.tests.ModelTest.test_eq
  192. will try to determine a test that interferes with the given one. First, the
  193. test is run with the first half of the test suite. If a failure occurs, the
  194. first half of the test suite is split in two groups and each group is then run
  195. with the specified test. If there is no failure with the first half of the test
  196. suite, the second half of the test suite is run with the specified test and
  197. split appropriately as described earlier. The process repeats until the set of
  198. failing tests is minimized.
  199. The ``--pair`` option runs the given test alongside every other test from the
  200. suite, letting you check if another test has side-effects that cause the
  201. failure. So:
  202. .. code-block:: bash
  203. $ ./runtests.py --pair basic.tests.ModelTest.test_eq
  204. will pair ``test_eq`` with every test label.
  205. With both ``--bisect`` and ``--pair``, if you already suspect which cases
  206. might be responsible for the failure, you may limit tests to be cross-analyzed
  207. by :ref:`specifying further test labels <runtests-specifying-labels>` after
  208. the first one:
  209. .. code-block:: bash
  210. $ ./runtests.py --pair basic.tests.ModelTest.test_eq queries transactions
  211. You can also try running any set of tests in reverse using the ``--reverse``
  212. option in order to verify that executing tests in a different order does not
  213. cause any trouble:
  214. .. code-block:: bash
  215. $ ./runtests.py basic --reverse
  216. .. versionadded:: 1.8
  217. The ``--reverse`` option was added.