unit-tests.txt 11 KB

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