unit-tests.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. .. _running-unit-tests-settings:
  35. Using another ``settings`` module
  36. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37. The included settings module allows you to run the test suite using
  38. SQLite. If you want to test behavior using a different database (and
  39. if you're proposing patches for Django, it's a good idea to test
  40. across databases), you may need to define your own settings file.
  41. To run the tests with different settings, ensure that the module is on your
  42. ``PYTHONPATH`` and pass the module with ``--settings``.
  43. The :setting:`DATABASES` setting in any test settings module needs to define
  44. two databases:
  45. * A ``default`` database. This database should use the backend that
  46. you want to use for primary testing
  47. * A database with the alias ``other``. The ``other`` database is used to
  48. establish that queries can be directed to different databases. As a result,
  49. this database can use any backend you want. It doesn't need to use the same
  50. backend as the ``default`` database (although it can use the same backend if
  51. you want to). It cannot be the same database as the ``default``.
  52. If you're using a backend that isn't SQLite, you will need to provide other
  53. details for each database:
  54. * The :setting:`USER` option needs to specify an existing user account
  55. for the database. That user needs permission to execute ``CREATE DATABASE``
  56. so that the test database can be created.
  57. * The :setting:`PASSWORD` option needs to provide the password for
  58. the :setting:`USER` that has been specified.
  59. Test databases get their names by prepending ``test_`` to the value of the
  60. :setting:`NAME` settings for the databases defined in :setting:`DATABASES`.
  61. These test databases are deleted when the tests are finished.
  62. .. versionchanged:: 1.7
  63. Before Django 1.7, the :setting:`NAME` setting was mandatory and had to
  64. be the name of an existing database to which the given user had permission
  65. to connect.
  66. You will also need to ensure that your database uses UTF-8 as the default
  67. character set. If your database server doesn't use UTF-8 as a default charset,
  68. you will need to include a value for :setting:`TEST_CHARSET` in the settings
  69. dictionary for the applicable database.
  70. Running only some of the tests
  71. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. Django's entire test suite takes a while to run, and running every single test
  73. could be redundant if, say, you just added a test to Django that you want to
  74. run quickly without running everything else. You can run a subset of the unit
  75. tests by appending the names of the test modules to ``runtests.py`` on the
  76. command line.
  77. For example, if you'd like to run tests only for generic relations and
  78. internationalization, type:
  79. .. code-block:: bash
  80. $ ./runtests.py --settings=path.to.settings generic_relations i18n
  81. How do you find out the names of individual tests? Look in ``tests/`` — each
  82. directory name there is the name of a test. Contrib app names are also valid
  83. test names.
  84. If you just want to run a particular class of tests, you can specify a list of
  85. paths to individual test classes. For example, to run the ``TranslationTests``
  86. of the ``i18n`` module, type:
  87. .. code-block:: bash
  88. $ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
  89. Going beyond that, you can specify an individual test method like this:
  90. .. code-block:: bash
  91. $ ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
  92. Running the Selenium tests
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. Some admin tests require Selenium 2, Firefox and Python >= 2.6 to work via a
  95. real Web browser. To allow those tests to run and not be skipped, you must
  96. install the selenium_ package (version > 2.13) into your Python path and run
  97. the tests with the ``--selenium`` option:
  98. .. code-block:: bash
  99. $ ./runtests.py --settings=test_sqlite --selenium admin_inlines
  100. .. _running-unit-tests-dependencies:
  101. Running all the tests
  102. ~~~~~~~~~~~~~~~~~~~~~
  103. If you want to run the full suite of tests, you'll need to install a number of
  104. dependencies:
  105. * bcrypt_
  106. * docutils_
  107. * numpy_
  108. * Pillow_
  109. * PyYAML_
  110. * pytz_
  111. * setuptools_
  112. * memcached_, plus a :ref:`supported Python binding <memcached>`
  113. * gettext_ (:ref:`gettext_on_windows`)
  114. * selenium_
  115. You can find these dependencies in `pip requirements files`_ inside the
  116. ``tests/requirements`` directory of the Django source tree and install them
  117. like so:
  118. .. code-block:: bash
  119. $ pip install -r tests/requirements/py2.txt # Python 3: py3.txt
  120. You can also install the database adapter(s) of your choice using
  121. ``oracle.txt``, ``mysql.txt``, or ``postgres.txt``.
  122. If you want to test the memcached cache backend, you'll also need to define
  123. a :setting:`CACHES` setting that points at your memcached instance.
  124. To run the GeoDjango tests, you will need to :doc:`setup a spatial database
  125. and install the Geospatial libraries</ref/contrib/gis/install/index>`.
  126. Each of these dependencies is optional. If you're missing any of them, the
  127. associated tests will be skipped.
  128. .. _bcrypt: https://pypi.python.org/pypi/bcrypt
  129. .. _docutils: https://pypi.python.org/pypi/docutils
  130. .. _numpy: https://pypi.python.org/pypi/numpy
  131. .. _Pillow: https://pypi.python.org/pypi/Pillow/
  132. .. _PyYAML: http://pyyaml.org/wiki/PyYAML
  133. .. _pytz: https://pypi.python.org/pypi/pytz/
  134. .. _setuptools: https://pypi.python.org/pypi/setuptools/
  135. .. _memcached: http://memcached.org/
  136. .. _gettext: http://www.gnu.org/software/gettext/manual/gettext.html
  137. .. _selenium: https://pypi.python.org/pypi/selenium
  138. .. _pip requirements files: http://www.pip-installer.org/en/latest/cookbook.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. To run coverage on the Django test suite using the standard test settings:
  145. .. code-block:: bash
  146. $ coverage run ./runtests.py --settings=test_sqlite
  147. After running coverage, generate the html report by running:
  148. .. code-block:: bash
  149. $ coverage html
  150. When running coverage for the Django tests, the included ``.coveragerc``
  151. settings file defines ``coverage_html`` as the output directory for the report
  152. and also excludes several directories not relevant to the results
  153. (test code or external code included in Django).
  154. .. _contrib-apps:
  155. Contrib apps
  156. ------------
  157. Tests for contrib apps go in their respective directories under
  158. ``django/contrib``, in a ``tests.py`` file. You can split the tests over
  159. multiple modules by using a ``tests`` directory in the normal Python way.
  160. If you have URLs that need to be mapped, put them in ``tests/urls.py``.
  161. To run tests for just one contrib app (e.g. ``auth``), use the same
  162. method as above:
  163. .. code-block:: bash
  164. $ ./runtests.py --settings=settings django.contrib.auth