unit-tests.txt 10 KB

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