unit-tests.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 git@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 provided
  30. 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. Running only some of the tests
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. Django's entire test suite takes a while to run, and running every single test
  74. could be redundant if, say, you just added a test to Django that you want to
  75. run quickly without running everything else. You can run a subset of the unit
  76. tests by appending the names of the test modules to ``runtests.py`` on the
  77. command line.
  78. For example, if you'd like to run tests only for generic relations and
  79. internationalization, type:
  80. .. code-block:: bash
  81. $ ./runtests.py --settings=path.to.settings generic_relations i18n
  82. How do you find out the names of individual tests? Look in ``tests/`` — each
  83. directory name there is the name of a test. Contrib app names are also valid
  84. test names.
  85. If you just want to run a particular class of tests, you can specify a list of
  86. paths to individual test classes. For example, to run the ``TranslationTests``
  87. of the ``i18n`` module, type:
  88. .. code-block:: bash
  89. $ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
  90. Going beyond that, you can specify an individual test method like this:
  91. .. code-block:: bash
  92. $ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
  93. Running the Selenium tests
  94. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. Some admin tests require Selenium 2, Firefox and Python >= 2.6 to work via a
  96. real Web browser. To allow those tests to run and not be skipped, you must
  97. install the selenium_ package (version > 2.13) into your Python path and run
  98. the tests with the ``--selenium`` option:
  99. .. code-block:: bash
  100. $ ./runtests.py --settings=test_sqlite --selenium admin_inlines
  101. .. _running-unit-tests-dependencies:
  102. Running all the tests
  103. ~~~~~~~~~~~~~~~~~~~~~
  104. If you want to run the full suite of tests, you'll need to install a number of
  105. dependencies:
  106. * bcrypt_
  107. * docutils_
  108. * numpy_
  109. * Pillow_
  110. * PyYAML_
  111. * pytz_
  112. * setuptools_
  113. * memcached_, plus a :ref:`supported Python binding <memcached>`
  114. * gettext_ (:ref:`gettext_on_windows`)
  115. * selenium_
  116. * sqlparse_
  117. You can find these dependencies in `pip requirements files`_ inside the
  118. ``tests/requirements`` directory of the Django source tree and install them
  119. like so:
  120. .. code-block:: bash
  121. $ pip install -r tests/requirements/py2.txt # Python 3: py3.txt
  122. You can also install the database adapter(s) of your choice using
  123. ``oracle.txt``, ``mysql.txt``, or ``postgres.txt``.
  124. If you want to test the memcached cache backend, you'll also need to define
  125. a :setting:`CACHES` setting that points at your memcached instance.
  126. To run the GeoDjango tests, you will need to :doc:`setup a spatial database
  127. and install the Geospatial libraries</ref/contrib/gis/install/index>`.
  128. Each of these dependencies is optional. If you're missing any of them, the
  129. associated tests will be skipped.
  130. .. _bcrypt: https://pypi.python.org/pypi/bcrypt
  131. .. _docutils: https://pypi.python.org/pypi/docutils
  132. .. _numpy: https://pypi.python.org/pypi/numpy
  133. .. _Pillow: https://pypi.python.org/pypi/Pillow/
  134. .. _PyYAML: http://pyyaml.org/wiki/PyYAML
  135. .. _pytz: https://pypi.python.org/pypi/pytz/
  136. .. _setuptools: https://pypi.python.org/pypi/setuptools/
  137. .. _memcached: http://memcached.org/
  138. .. _gettext: http://www.gnu.org/software/gettext/manual/gettext.html
  139. .. _selenium: https://pypi.python.org/pypi/selenium
  140. .. _sqlparse: https://pypi.python.org/pypi/sqlparse
  141. .. _pip requirements files: http://www.pip-installer.org/en/latest/cookbook.html#requirements-files
  142. Code coverage
  143. ~~~~~~~~~~~~~
  144. Contributors are encouraged to run coverage on the test suite to identify areas
  145. that need additional tests. The coverage tool installation and use is described
  146. in :ref:`testing code coverage<topics-testing-code-coverage>`.
  147. To run coverage on the Django test suite using the standard test settings:
  148. .. code-block:: bash
  149. $ coverage run ./runtests.py --settings=test_sqlite
  150. After running coverage, generate the html report by running:
  151. .. code-block:: bash
  152. $ coverage html
  153. When running coverage for the Django tests, the included ``.coveragerc``
  154. settings file defines ``coverage_html`` as the output directory for the report
  155. and also excludes several directories not relevant to the results
  156. (test code or external code included in Django).
  157. .. _contrib-apps:
  158. Contrib apps
  159. ------------
  160. Tests for contrib apps go in their respective directories under
  161. ``django/contrib``, in a ``tests.py`` file. You can split the tests over
  162. multiple modules by using a ``tests`` directory in the normal Python way.
  163. If you have URLs that need to be mapped, put them in ``tests/urls.py``.
  164. To run tests for just one contrib app (e.g. ``auth``), use the same
  165. method as above:
  166. .. code-block:: bash
  167. $ ./runtests.py --settings=settings django.contrib.auth
  168. .. _troubleshooting-unit-tests:
  169. Troubleshooting
  170. ---------------
  171. Many test failures with ``UnicodeEncodeError``.
  172. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  173. If the ``locales`` package is not installed, some tests will fail with a
  174. ``UnicodeEncodeError``.
  175. You can resolve this on Debian-based systems, for example, by running:
  176. .. code-block:: bash
  177. $ apt-get install locales
  178. $ dpkg-reconfigure locales