unit-tests.txt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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:`Testing Django applications
  12. </topics/testing/overview>` for an explanation of 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. PYTHONPATH=..:$PYTHONPATH python ./runtests.py --settings=test_sqlite
  28. ``runtests.py`` now uses ``test_sqlite`` by default if settings aren't provided
  29. through either ``--settings`` or :envvar:`DJANGO_SETTINGS_MODULE`.
  30. You can avoid typing the ``PYTHONPATH`` bit each time by adding your Django
  31. checkout to your ``PYTHONPATH`` or by installing the source checkout using pip.
  32. See :ref:`installing-development-version`.
  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. .. versionchanged:: 1.7
  62. Before Django 1.7, the :setting:`NAME` setting was mandatory and had to
  63. be the name of an existing database to which the given user had permission
  64. to connect.
  65. You will also need to ensure that your database uses UTF-8 as the default
  66. character set. If your database server doesn't use UTF-8 as a default charset,
  67. you will need to include a value for :setting:`TEST_CHARSET` in the settings
  68. dictionary for the applicable database.
  69. Running only some of the tests
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. Django's entire test suite takes a while to run, and running every single test
  72. could be redundant if, say, you just added a test to Django that you want to
  73. run quickly without running everything else. You can run a subset of the unit
  74. tests by appending the names of the test modules to ``runtests.py`` on the
  75. command line.
  76. For example, if you'd like to run tests only for generic relations and
  77. internationalization, type:
  78. .. code-block:: bash
  79. ./runtests.py --settings=path.to.settings generic_relations i18n
  80. How do you find out the names of individual tests? Look in ``tests/`` — each
  81. directory name there is the name of a test. Contrib app names are also valid
  82. test names.
  83. If you just want to run a particular class of tests, you can specify a list of
  84. paths to individual test classes. For example, to run the ``TranslationTests``
  85. of the ``i18n`` module, type:
  86. .. code-block:: bash
  87. ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests
  88. Going beyond that, you can specify an individual test method like this:
  89. .. code-block:: bash
  90. ./runtests.py --settings=path.to.settings i18n.tests.TranslationTests.test_lazy_objects
  91. Running the Selenium tests
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. Some admin tests require Selenium 2, Firefox and Python >= 2.6 to work via a
  94. real Web browser. To allow those tests to run and not be skipped, you must
  95. install the selenium_ package (version > 2.13) into your Python path and run
  96. the tests with the ``--selenium`` option:
  97. .. code-block:: bash
  98. ./runtests.py --settings=test_sqlite --selenium admin_inlines
  99. .. _running-unit-tests-dependencies:
  100. Running all the tests
  101. ~~~~~~~~~~~~~~~~~~~~~
  102. If you want to run the full suite of tests, you'll need to install a number of
  103. dependencies:
  104. * bcrypt_
  105. * docutils_
  106. * numpy_
  107. * Pillow_
  108. * PyYAML_
  109. * pytz_
  110. * setuptools_
  111. * memcached_, plus a :ref:`supported Python binding <memcached>`
  112. * gettext_ (:ref:`gettext_on_windows`)
  113. * selenium_
  114. You can find these dependencies in `pip requirements files`_ inside the
  115. ``tests/requirements`` directory of the Django source tree and install them
  116. like so::
  117. pip install -r tests/requirements/py2.txt # Python 3: py3.txt
  118. You can also install the database adapter(s) of your choice using
  119. ``oracle.txt``, ``mysql.txt``, or ``postgres.txt``.
  120. If you want to test the memcached cache backend, you'll also need to define
  121. a :setting:`CACHES` setting that points at your memcached instance.
  122. To run the GeoDjango tests, you will need to :doc:`setup a spatial database
  123. and install the Geospatial libraries</ref/contrib/gis/install/index>`.
  124. Each of these dependencies is optional. If you're missing any of them, the
  125. associated tests will be skipped.
  126. .. _bcrypt: https://pypi.python.org/pypi/bcrypt
  127. .. _docutils: https://pypi.python.org/pypi/docutils
  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. .. _gettext: http://www.gnu.org/software/gettext/manual/gettext.html
  135. .. _selenium: https://pypi.python.org/pypi/selenium
  136. .. _pip requirements files: http://www.pip-installer.org/en/latest/cookbook.html#requirements-files
  137. Code coverage
  138. ~~~~~~~~~~~~~
  139. Contributors are encouraged to run coverage on the test suite to identify areas
  140. that need additional tests. The coverage tool installation and use is described
  141. in :ref:`testing code coverage<topics-testing-code-coverage>`.
  142. To run coverage on the Django test suite using the standard test settings::
  143. coverage run ./runtests.py --settings=test_sqlite
  144. After running coverage, generate the html report by running::
  145. coverage html
  146. When running coverage for the Django tests, the included ``.coveragerc``
  147. settings file defines ``coverage_html`` as the output directory for the report
  148. and also excludes several directories not relevant to the results
  149. (test code or external code included in Django).
  150. .. _contrib-apps:
  151. Contrib apps
  152. ------------
  153. Tests for contrib apps go in their respective directories under
  154. ``django/contrib``, in a ``tests.py`` file. You can split the tests over
  155. multiple modules by using a ``tests`` directory in the normal Python way.
  156. If you have URLs that need to be mapped, put them in ``tests/urls.py``.
  157. To run tests for just one contrib app (e.g. ``auth``), use the same
  158. method as above::
  159. ./runtests.py --settings=settings django.contrib.auth