unit-tests.txt 11 KB

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