unit-tests.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 and the database API (``tests/modeltests``),
  8. * Everything else in core Django code (``tests/regressiontests``),
  9. * :ref:`contrib-apps` (``django/contrib/<app>/tests``).
  10. We appreciate any and all contributions to the test suite!
  11. The Django tests all use the testing infrastructure that ships with Django for
  12. testing applications. See :doc:`Testing Django applications </topics/testing>`
  13. for an explanation of how to write new tests.
  14. .. _running-unit-tests:
  15. Running the unit tests
  16. ----------------------
  17. Quickstart
  18. ~~~~~~~~~~
  19. Running the tests requires a Django settings module that defines the
  20. databases to use. To make it easy to get started, Django provides a
  21. sample settings module that uses the SQLite database. To run the tests
  22. with this sample ``settings`` module, ``cd`` into the Django
  23. ``tests/`` directory and run:
  24. .. code-block:: bash
  25. ./runtests.py --settings=test_sqlite
  26. If you get an ``ImportError: No module named django.contrib`` error,
  27. you need to add your install of Django to your ``PYTHONPATH``.
  28. Using another ``settings`` module
  29. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30. The included settings module allows you to run the test suite using
  31. SQLite. If you want to test behavior using a different database (and
  32. if you're proposing patches for Django, it's a good idea to test
  33. across databases), you may need to define your own settings file.
  34. To run the tests with different settings, ``cd`` to the ``tests/`` directory
  35. and type:
  36. .. code-block:: bash
  37. ./runtests.py --settings=path.to.django.settings
  38. The :setting:`DATABASES` setting in this test settings module needs to define
  39. two databases:
  40. * A ``default`` database. This database should use the backend that
  41. you want to use for primary testing
  42. * A database with the alias ``other``. The ``other`` database is
  43. used to establish that queries can be directed to different
  44. databases. As a result, this database can use any backend you
  45. want. It doesn't need to use the same backend as the ``default``
  46. database (although it can use the same backend if you want to).
  47. If you're using a backend that isn't SQLite, you will need to provide other
  48. details for each database:
  49. * The :setting:`USER` option for each of your databases needs to
  50. specify an existing user account for the database.
  51. * The :setting:`PASSWORD` option needs to provide the password for
  52. the :setting:`USER` that has been specified.
  53. * The :setting:`NAME` option must be the name of an existing database to
  54. which the given user has permission to connect. The unit tests will not
  55. touch this database; the test runner creates a new database whose name
  56. is :setting:`NAME` prefixed with ``test_``, and this test database is
  57. deleted when the tests are finished. This means your user account needs
  58. permission to execute ``CREATE DATABASE``.
  59. You will also need to ensure that your database uses UTF-8 as the default
  60. character set. If your database server doesn't use UTF-8 as a default charset,
  61. you will need to include a value for :setting:`TEST_CHARSET` in the settings
  62. dictionary for the applicable database.
  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. .. code-block:: bash
  73. ./runtests.py --settings=path.to.settings generic_relations i18n
  74. How do you find out the names of individual tests? Look in
  75. ``tests/modeltests`` and ``tests/regressiontests`` — each directory name
  76. there is the name of a test. Contrib app names are also valid test names.
  77. If you just want to run a particular class of tests, you can specify a list of
  78. paths to individual test classes. For example, to run the ``TranslationTests``
  79. of the ``i18n`` module, type:
  80. .. code-block:: bash
  81. ./runtests.py --settings=path.to.settings i18n.TranslationTests
  82. Going beyond that, you can specify an individual test method like this:
  83. .. code-block:: bash
  84. ./runtests.py --settings=path.to.settings i18n.TranslationTests.test_lazy_objects
  85. Running the Selenium tests
  86. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. Some admin tests require Selenium 2, Firefox and Python >= 2.6 to work via a
  88. real Web browser. To allow those tests to run and not be skipped, you must
  89. install the selenium_ package (version > 2.13) into your Python path.
  90. Then, run the tests normally, for example:
  91. .. code-block:: bash
  92. ./runtests.py --settings=test_sqlite admin_inlines
  93. Running all the tests
  94. ~~~~~~~~~~~~~~~~~~~~~
  95. If you want to run the full suite of tests, you'll need to install a number of
  96. dependencies:
  97. * PyYAML_
  98. * Markdown_
  99. * Textile_
  100. * Docutils_
  101. * setuptools_
  102. * memcached_, plus a :ref:`supported Python binding <memcached>`
  103. * gettext_ (:ref:`gettext_on_windows`)
  104. * selenium_ (if also using Python >= 2.6)
  105. If you want to test the memcached cache backend, you'll also need to define
  106. a :setting:`CACHES` setting that points at your memcached instance.
  107. Each of these dependencies is optional. If you're missing any of them, the
  108. associated tests will be skipped.
  109. .. _PyYAML: http://pyyaml.org/wiki/PyYAML
  110. .. _Markdown: http://pypi.python.org/pypi/Markdown/1.7
  111. .. _Textile: http://pypi.python.org/pypi/textile
  112. .. _docutils: http://pypi.python.org/pypi/docutils/0.4
  113. .. _setuptools: http://pypi.python.org/pypi/setuptools/
  114. .. _memcached: http://memcached.org/
  115. .. _gettext: http://www.gnu.org/software/gettext/manual/gettext.html
  116. .. _selenium: http://pypi.python.org/pypi/selenium
  117. Code coverage
  118. ~~~~~~~~~~~~~
  119. Contributors are encouraged to run coverage on the test suite to identify areas
  120. that need additional tests. The coverage tool installation and use is described
  121. in :ref:`testing code coverage<topics-testing-code-coverage>`.
  122. To run coverage on the Django test suite using the standard test settings::
  123. coverage run ./runtests.py --settings=test_sqlite
  124. After running coverage, generate the html report by running::
  125. coverage html
  126. When running coverage for the Django tests, the included ``.coveragerc``
  127. settings file defines ``coverage_html`` as the output directory for the report
  128. and also excludes several directories not relevant to the results
  129. (test code or external code included in Django).
  130. .. _contrib-apps:
  131. Contrib apps
  132. ------------
  133. Tests for contrib apps go in their respective directories under
  134. ``django/contrib``, in a ``tests.py`` file. You can split the tests over
  135. multiple modules by using a ``tests`` directory in the normal Python way.
  136. For the tests to be found, a ``models.py`` file must exist, even if it's empty.
  137. If you have URLs that need to be mapped, put them in ``tests/urls.py``.
  138. To run tests for just one contrib app (e.g. ``markup``), use the same
  139. method as above::
  140. ./runtests.py --settings=settings markup