PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/topics/testing.txt

https://code.google.com/p/mango-py/
Plain Text | 1801 lines | 1291 code | 510 blank | 0 comment | 0 complexity | 168acb74e07b0e612dca55e557f4fece MD5 | raw file
Possible License(s): BSD-3-Clause
  1. ===========================
  2. Testing Django applications
  3. ===========================
  4. .. module:: django.test
  5. :synopsis: Testing tools for Django applications.
  6. Automated testing is an extremely useful bug-killing tool for the modern
  7. Web developer. You can use a collection of tests -- a **test suite** -- to
  8. solve, or avoid, a number of problems:
  9. * When you're writing new code, you can use tests to validate your code
  10. works as expected.
  11. * When you're refactoring or modifying old code, you can use tests to
  12. ensure your changes haven't affected your application's behavior
  13. unexpectedly.
  14. Testing a Web application is a complex task, because a Web application is made
  15. of several layers of logic -- from HTTP-level request handling, to form
  16. validation and processing, to template rendering. With Django's test-execution
  17. framework and assorted utilities, you can simulate requests, insert test data,
  18. inspect your application's output and generally verify your code is doing what
  19. it should be doing.
  20. The best part is, it's really easy.
  21. This document is split into two primary sections. First, we explain how to
  22. write tests with Django. Then, we explain how to run them.
  23. Writing tests
  24. =============
  25. There are two primary ways to write tests with Django, corresponding to the
  26. two test frameworks that ship in the Python standard library. The two
  27. frameworks are:
  28. * **Unit tests** -- tests that are expressed as methods on a Python class
  29. that subclasses ``unittest.TestCase`` or Django's customized
  30. :class:`TestCase`. For example::
  31. import unittest
  32. class MyFuncTestCase(unittest.TestCase):
  33. def testBasic(self):
  34. a = ['larry', 'curly', 'moe']
  35. self.assertEqual(my_func(a, 0), 'larry')
  36. self.assertEqual(my_func(a, 1), 'curly')
  37. * **Doctests** -- tests that are embedded in your functions' docstrings and
  38. are written in a way that emulates a session of the Python interactive
  39. interpreter. For example::
  40. def my_func(a_list, idx):
  41. """
  42. >>> a = ['larry', 'curly', 'moe']
  43. >>> my_func(a, 0)
  44. 'larry'
  45. >>> my_func(a, 1)
  46. 'curly'
  47. """
  48. return a_list[idx]
  49. We'll discuss choosing the appropriate test framework later, however, most
  50. experienced developers prefer unit tests. You can also use any *other* Python
  51. test framework, as we'll explain in a bit.
  52. Writing unit tests
  53. ------------------
  54. Django's unit tests use a Python standard library module: unittest_. This
  55. module defines tests in class-based approach.
  56. .. admonition:: unittest2
  57. .. versionchanged:: 1.3
  58. Python 2.7 introduced some major changes to the unittest library,
  59. adding some extremely useful features. To ensure that every Django
  60. project can benefit from these new features, Django ships with a
  61. copy of unittest2_, a copy of the Python 2.7 unittest library,
  62. backported for Python 2.4 compatibility.
  63. To access this library, Django provides the
  64. ``django.utils.unittest`` module alias. If you are using Python
  65. 2.7, or you have installed unittest2 locally, Django will map the
  66. alias to the installed version of the unittest library. Otherwise,
  67. Django will use it's own bundled version of unittest2.
  68. To use this alias, simply use::
  69. from django.utils import unittest
  70. wherever you would have historically used::
  71. import unittest
  72. If you want to continue to use the base unittest library, you can --
  73. you just won't get any of the nice new unittest2 features.
  74. .. _unittest2: http://pypi.python.org/pypi/unittest2
  75. For a given Django application, the test runner looks for unit tests in two
  76. places:
  77. * The ``models.py`` file. The test runner looks for any subclass of
  78. ``unittest.TestCase`` in this module.
  79. * A file called ``tests.py`` in the application directory -- i.e., the
  80. directory that holds ``models.py``. Again, the test runner looks for any
  81. subclass of ``unittest.TestCase`` in this module.
  82. Here is an example ``unittest.TestCase`` subclass::
  83. from django.utils import unittest
  84. from myapp.models import Animal
  85. class AnimalTestCase(unittest.TestCase):
  86. def setUp(self):
  87. self.lion = Animal.objects.create(name="lion", sound="roar")
  88. self.cat = Animal.objects.create(name="cat", sound="meow")
  89. def testSpeaking(self):
  90. self.assertEqual(self.lion.speak(), 'The lion says "roar"')
  91. self.assertEqual(self.cat.speak(), 'The cat says "meow"')
  92. When you :ref:`run your tests <running-tests>`, the default behavior of the
  93. test utility is to find all the test cases (that is, subclasses of
  94. ``unittest.TestCase``) in ``models.py`` and ``tests.py``, automatically build a
  95. test suite out of those test cases, and run that suite.
  96. There is a second way to define the test suite for a module: if you define a
  97. function called ``suite()`` in either ``models.py`` or ``tests.py``, the
  98. Django test runner will use that function to construct the test suite for that
  99. module. This follows the `suggested organization`_ for unit tests. See the
  100. Python documentation for more details on how to construct a complex test
  101. suite.
  102. For more details about ``unittest``, see the `standard library unittest
  103. documentation`_.
  104. .. _unittest: http://docs.python.org/library/unittest.html
  105. .. _standard library unittest documentation: unittest_
  106. .. _suggested organization: http://docs.python.org/library/unittest.html#organizing-tests
  107. Writing doctests
  108. ----------------
  109. Doctests use Python's standard doctest_ module, which searches your docstrings
  110. for statements that resemble a session of the Python interactive interpreter.
  111. A full explanation of how doctest works is out of the scope of this document;
  112. read Python's official documentation for the details.
  113. .. admonition:: What's a **docstring**?
  114. A good explanation of docstrings (and some guidelines for using them
  115. effectively) can be found in :pep:`257`:
  116. A docstring is a string literal that occurs as the first statement in
  117. a module, function, class, or method definition. Such a docstring
  118. becomes the ``__doc__`` special attribute of that object.
  119. For example, this function has a docstring that describes what it does::
  120. def add_two(num):
  121. "Return the result of adding two to the provided number."
  122. return num + 2
  123. Because tests often make great documentation, putting tests directly in
  124. your docstrings is an effective way to document *and* test your code.
  125. As with unit tests, for a given Django application, the test runner looks for
  126. doctests in two places:
  127. * The ``models.py`` file. You can define module-level doctests and/or a
  128. doctest for individual models. It's common practice to put
  129. application-level doctests in the module docstring and model-level
  130. doctests in the model docstrings.
  131. * A file called ``tests.py`` in the application directory -- i.e., the
  132. directory that holds ``models.py``. This file is a hook for any and all
  133. doctests you want to write that aren't necessarily related to models.
  134. This example doctest is equivalent to the example given in the unittest section
  135. above::
  136. # models.py
  137. from django.db import models
  138. class Animal(models.Model):
  139. """
  140. An animal that knows how to make noise
  141. # Create some animals
  142. >>> lion = Animal.objects.create(name="lion", sound="roar")
  143. >>> cat = Animal.objects.create(name="cat", sound="meow")
  144. # Make 'em speak
  145. >>> lion.speak()
  146. 'The lion says "roar"'
  147. >>> cat.speak()
  148. 'The cat says "meow"'
  149. """
  150. name = models.CharField(max_length=20)
  151. sound = models.CharField(max_length=20)
  152. def speak(self):
  153. return 'The %s says "%s"' % (self.name, self.sound)
  154. When you :ref:`run your tests <running-tests>`, the test runner will find this
  155. docstring, notice that portions of it look like an interactive Python session,
  156. and execute those lines while checking that the results match.
  157. In the case of model tests, note that the test runner takes care of creating
  158. its own test database. That is, any test that accesses a database -- by
  159. creating and saving model instances, for example -- will not affect your
  160. production database. However, the database is not refreshed between doctests,
  161. so if your doctest requires a certain state you should consider flushing the
  162. database or loading a fixture. (See the section on fixtures, below, for more
  163. on this.) Note that to use this feature, the database user Django is connecting
  164. as must have ``CREATE DATABASE`` rights.
  165. For more details about how doctest works, see the `standard library
  166. documentation for doctest`_.
  167. .. _doctest: http://docs.python.org/library/doctest.html
  168. .. _standard library documentation for doctest: doctest_
  169. Which should I use?
  170. -------------------
  171. Because Django supports both of the standard Python test frameworks, it's up to
  172. you and your tastes to decide which one to use. You can even decide to use
  173. *both*.
  174. For developers new to testing, however, this choice can seem confusing. Here,
  175. then, are a few key differences to help you decide which approach is right for
  176. you:
  177. * If you've been using Python for a while, ``doctest`` will probably feel
  178. more "pythonic". It's designed to make writing tests as easy as possible,
  179. so it requires no overhead of writing classes or methods. You simply put
  180. tests in docstrings. This has the added advantage of serving as
  181. documentation (and correct documentation, at that!). However, while
  182. doctests are good for some simple example code, they are not very good if
  183. you want to produce either high quality, comprehensive tests or high
  184. quality documentation. Test failures are often difficult to debug
  185. as it can be unclear exactly why the test failed. Thus, doctests should
  186. generally be avoided and used primarily for documentation examples only.
  187. * The ``unittest`` framework will probably feel very familiar to developers
  188. coming from Java. ``unittest`` is inspired by Java's JUnit, so you'll
  189. feel at home with this method if you've used JUnit or any test framework
  190. inspired by JUnit.
  191. * If you need to write a bunch of tests that share similar code, then
  192. you'll appreciate the ``unittest`` framework's organization around
  193. classes and methods. This makes it easy to abstract common tasks into
  194. common methods. The framework also supports explicit setup and/or cleanup
  195. routines, which give you a high level of control over the environment
  196. in which your test cases are run.
  197. * If you're writing tests for Django itself, you should use ``unittest``.
  198. .. _running-tests:
  199. Running tests
  200. =============
  201. Once you've written tests, run them using the :djadmin:`test` command of
  202. your project's ``manage.py`` utility::
  203. $ ./manage.py test
  204. By default, this will run every test in every application in
  205. :setting:`INSTALLED_APPS`. If you only want to run tests for a particular
  206. application, add the application name to the command line. For example, if your
  207. :setting:`INSTALLED_APPS` contains ``'myproject.polls'`` and
  208. ``'myproject.animals'``, you can run the ``myproject.animals`` unit tests alone
  209. with this command::
  210. $ ./manage.py test animals
  211. Note that we used ``animals``, not ``myproject.animals``.
  212. You can be even *more* specific by naming an individual test case. To
  213. run a single test case in an application (for example, the
  214. ``AnimalTestCase`` described in the "Writing unit tests" section), add
  215. the name of the test case to the label on the command line::
  216. $ ./manage.py test animals.AnimalTestCase
  217. And it gets even more granular than that! To run a *single* test
  218. method inside a test case, add the name of the test method to the
  219. label::
  220. $ ./manage.py test animals.AnimalTestCase.testFluffyAnimals
  221. .. versionadded:: 1.2
  222. The ability to select individual doctests was added.
  223. You can use the same rules if you're using doctests. Django will use the
  224. test label as a path to the test method or class that you want to run.
  225. If your ``models.py`` or ``tests.py`` has a function with a doctest, or
  226. class with a class-level doctest, you can invoke that test by appending the
  227. name of the test method or class to the label::
  228. $ ./manage.py test animals.classify
  229. If you want to run the doctest for a specific method in a class, add the
  230. name of the method to the label::
  231. $ ./manage.py test animals.Classifier.run
  232. If you're using a ``__test__`` dictionary to specify doctests for a
  233. module, Django will use the label as a key in the ``__test__`` dictionary
  234. for defined in ``models.py`` and ``tests.py``.
  235. .. versionadded:: 1.2
  236. You can now trigger a graceful exit from a test run by pressing ``Ctrl-C``.
  237. If you press ``Ctrl-C`` while the tests are running, the test runner will
  238. wait for the currently running test to complete and then exit gracefully.
  239. During a graceful exit the test runner will output details of any test
  240. failures, report on how many tests were run and how many errors and failures
  241. were encountered, and destroy any test databases as usual. Thus pressing
  242. ``Ctrl-C`` can be very useful if you forget to pass the :djadminopt:`--failfast`
  243. option, notice that some tests are unexpectedly failing, and want to get details
  244. on the failures without waiting for the full test run to complete.
  245. If you do not want to wait for the currently running test to finish, you
  246. can press ``Ctrl-C`` a second time and the test run will halt immediately,
  247. but not gracefully. No details of the tests run before the interruption will
  248. be reported, and any test databases created by the run will not be destroyed.
  249. .. admonition:: Test with warnings enabled
  250. It's a good idea to run your tests with Python warnings enabled:
  251. ``python -Wall manage.py test``. The ``-Wall`` flag tells Python to
  252. display deprecation warnings. Django, like many other Python libraries,
  253. uses these warnings to flag when features are going away. It also might
  254. flag areas in your code that aren't strictly wrong but could benefit
  255. from a better implementation.
  256. Running tests outside the test runner
  257. -------------------------------------
  258. If you want to run tests outside of ``./manage.py test`` -- for example,
  259. from a shell prompt -- you will need to set up the test
  260. environment first. Django provides a convenience method to do this::
  261. >>> from django.test.utils import setup_test_environment
  262. >>> setup_test_environment()
  263. This convenience method sets up the test database, and puts other
  264. Django features into modes that allow for repeatable testing.
  265. The call to :meth:`~django.test.utils.setup_test_environment` is made
  266. automatically as part of the setup of `./manage.py test`. You only
  267. need to manually invoke this method if you're not using running your
  268. tests via Django's test runner.
  269. The test database
  270. -----------------
  271. Tests that require a database (namely, model tests) will not use your "real"
  272. (production) database. Separate, blank databases are created for the tests.
  273. Regardless of whether the tests pass or fail, the test databases are destroyed
  274. when all the tests have been executed.
  275. By default the test databases get their names by prepending ``test_``
  276. to the value of the :setting:`NAME` settings for the databases
  277. defined in :setting:`DATABASES`. When using the SQLite database engine
  278. the tests will by default use an in-memory database (i.e., the
  279. database will be created in memory, bypassing the filesystem
  280. entirely!). If you want to use a different database name, specify
  281. :setting:`TEST_NAME` in the dictionary for any given database in
  282. :setting:`DATABASES`.
  283. Aside from using a separate database, the test runner will otherwise
  284. use all of the same database settings you have in your settings file:
  285. :setting:`ENGINE`, :setting:`USER`, :setting:`HOST`, etc. The test
  286. database is created by the user specified by :setting:`USER`, so you'll need
  287. to make sure that the given user account has sufficient privileges to
  288. create a new database on the system.
  289. For fine-grained control over the character encoding of your test
  290. database, use the :setting:`TEST_CHARSET` option. If you're using
  291. MySQL, you can also use the :setting:`TEST_COLLATION` option to
  292. control the particular collation used by the test database. See the
  293. :doc:`settings documentation </ref/settings>` for details of these
  294. advanced settings.
  295. .. _topics-testing-masterslave:
  296. Testing master/slave configurations
  297. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  298. .. versionadded:: 1.2
  299. If you're testing a multiple database configuration with master/slave
  300. replication, this strategy of creating test databases poses a problem.
  301. When the test databases are created, there won't be any replication,
  302. and as a result, data created on the master won't be seen on the
  303. slave.
  304. To compensate for this, Django allows you to define that a database is
  305. a *test mirror*. Consider the following (simplified) example database
  306. configuration::
  307. DATABASES = {
  308. 'default': {
  309. 'ENGINE': 'django.db.backends.mysql',
  310. 'NAME': 'myproject',
  311. 'HOST': 'dbmaster',
  312. # ... plus some other settings
  313. },
  314. 'slave': {
  315. 'ENGINE': 'django.db.backends.mysql',
  316. 'NAME': 'myproject',
  317. 'HOST': 'dbslave',
  318. 'TEST_MIRROR': 'default'
  319. # ... plus some other settings
  320. }
  321. }
  322. In this setup, we have two database servers: ``dbmaster``, described
  323. by the database alias ``default``, and ``dbslave`` described by the
  324. alias ``slave``. As you might expect, ``dbslave`` has been configured
  325. by the database administrator as a read slave of ``dbmaster``, so in
  326. normal activity, any write to ``default`` will appear on ``slave``.
  327. If Django created two independent test databases, this would break any
  328. tests that expected replication to occur. However, the ``slave``
  329. database has been configured as a test mirror (using the
  330. :setting:`TEST_MIRROR` setting), indicating that under testing,
  331. ``slave`` should be treated as a mirror of ``default``.
  332. When the test environment is configured, a test version of ``slave``
  333. will *not* be created. Instead the connection to ``slave``
  334. will be redirected to point at ``default``. As a result, writes to
  335. ``default`` will appear on ``slave`` -- but because they are actually
  336. the same database, not because there is data replication between the
  337. two databases.
  338. .. _topics-testing-creation-dependencies:
  339. Controlling creation order for test databases
  340. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  341. .. versionadded:: 1.3
  342. By default, Django will always create the ``default`` database first.
  343. However, no guarantees are made on the creation order of any other
  344. databases in your test setup.
  345. If your database configuration requires a specific creation order, you
  346. can specify the dependencies that exist using the
  347. :setting:`TEST_DEPENDENCIES` setting. Consider the following
  348. (simplified) example database configuration::
  349. DATABASES = {
  350. 'default': {
  351. # ... db settings
  352. 'TEST_DEPENDENCIES': ['diamonds']
  353. },
  354. 'diamonds': {
  355. # ... db settings
  356. },
  357. 'clubs': {
  358. # ... db settings
  359. 'TEST_DEPENDENCIES': ['diamonds']
  360. },
  361. 'spades': {
  362. # ... db settings
  363. 'TEST_DEPENDENCIES': ['diamonds','hearts']
  364. },
  365. 'hearts': {
  366. # ... db settings
  367. 'TEST_DEPENDENCIES': ['diamonds','clubs']
  368. }
  369. }
  370. Under this configuration, the ``diamonds`` database will be created first,
  371. as it is the only database alias without dependencies. The ``default`` and
  372. ``clubs`` alias will be created next (although the order of creation of this
  373. pair is not guaranteed); then ``hearts``; and finally ``spades``.
  374. If there are any circular dependencies in the
  375. :setting:`TEST_DEPENDENCIES` definition, an ``ImproperlyConfigured``
  376. exception will be raised.
  377. Other test conditions
  378. ---------------------
  379. Regardless of the value of the :setting:`DEBUG` setting in your configuration
  380. file, all Django tests run with :setting:`DEBUG`\=False. This is to ensure that
  381. the observed output of your code matches what will be seen in a production
  382. setting.
  383. Understanding the test output
  384. -----------------------------
  385. When you run your tests, you'll see a number of messages as the test runner
  386. prepares itself. You can control the level of detail of these messages with the
  387. ``verbosity`` option on the command line::
  388. Creating test database...
  389. Creating table myapp_animal
  390. Creating table myapp_mineral
  391. Loading 'initial_data' fixtures...
  392. No fixtures found.
  393. This tells you that the test runner is creating a test database, as described
  394. in the previous section.
  395. Once the test database has been created, Django will run your tests.
  396. If everything goes well, you'll see something like this::
  397. ----------------------------------------------------------------------
  398. Ran 22 tests in 0.221s
  399. OK
  400. If there are test failures, however, you'll see full details about which tests
  401. failed::
  402. ======================================================================
  403. FAIL: Doctest: ellington.core.throttle.models
  404. ----------------------------------------------------------------------
  405. Traceback (most recent call last):
  406. File "/dev/django/test/doctest.py", line 2153, in runTest
  407. raise self.failureException(self.format_failure(new.getvalue()))
  408. AssertionError: Failed doctest test for myapp.models
  409. File "/dev/myapp/models.py", line 0, in models
  410. ----------------------------------------------------------------------
  411. File "/dev/myapp/models.py", line 14, in myapp.models
  412. Failed example:
  413. throttle.check("actor A", "action one", limit=2, hours=1)
  414. Expected:
  415. True
  416. Got:
  417. False
  418. ----------------------------------------------------------------------
  419. Ran 2 tests in 0.048s
  420. FAILED (failures=1)
  421. A full explanation of this error output is beyond the scope of this document,
  422. but it's pretty intuitive. You can consult the documentation of Python's
  423. ``unittest`` library for details.
  424. Note that the return code for the test-runner script is 1 for any number of
  425. failed and erroneous tests. If all the tests pass, the return code is 0. This
  426. feature is useful if you're using the test-runner script in a shell script and
  427. need to test for success or failure at that level.
  428. Testing tools
  429. =============
  430. Django provides a small set of tools that come in handy when writing tests.
  431. .. _test-client:
  432. The test client
  433. ---------------
  434. .. module:: django.test.client
  435. :synopsis: Django's test client.
  436. The test client is a Python class that acts as a dummy Web browser, allowing
  437. you to test your views and interact with your Django-powered application
  438. programmatically.
  439. Some of the things you can do with the test client are:
  440. * Simulate GET and POST requests on a URL and observe the response --
  441. everything from low-level HTTP (result headers and status codes) to
  442. page content.
  443. * Test that the correct view is executed for a given URL.
  444. * Test that a given request is rendered by a given Django template, with
  445. a template context that contains certain values.
  446. Note that the test client is not intended to be a replacement for Twill_,
  447. Selenium_, or other "in-browser" frameworks. Django's test client has
  448. a different focus. In short:
  449. * Use Django's test client to establish that the correct view is being
  450. called and that the view is collecting the correct context data.
  451. * Use in-browser frameworks such as Twill and Selenium to test *rendered*
  452. HTML and the *behavior* of Web pages, namely JavaScript functionality.
  453. A comprehensive test suite should use a combination of both test types.
  454. .. _Twill: http://twill.idyll.org/
  455. .. _Selenium: http://seleniumhq.org/
  456. Overview and a quick example
  457. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  458. To use the test client, instantiate ``django.test.client.Client`` and retrieve
  459. Web pages::
  460. >>> from django.test.client import Client
  461. >>> c = Client()
  462. >>> response = c.post('/login/', {'username': 'john', 'password': 'smith'})
  463. >>> response.status_code
  464. 200
  465. >>> response = c.get('/customer/details/')
  466. >>> response.content
  467. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...'
  468. As this example suggests, you can instantiate ``Client`` from within a session
  469. of the Python interactive interpreter.
  470. Note a few important things about how the test client works:
  471. * The test client does *not* require the Web server to be running. In fact,
  472. it will run just fine with no Web server running at all! That's because
  473. it avoids the overhead of HTTP and deals directly with the Django
  474. framework. This helps make the unit tests run quickly.
  475. * When retrieving pages, remember to specify the *path* of the URL, not the
  476. whole domain. For example, this is correct::
  477. >>> c.get('/login/')
  478. This is incorrect::
  479. >>> c.get('http://www.example.com/login/')
  480. The test client is not capable of retrieving Web pages that are not
  481. powered by your Django project. If you need to retrieve other Web pages,
  482. use a Python standard library module such as urllib_ or urllib2_.
  483. * To resolve URLs, the test client uses whatever URLconf is pointed-to by
  484. your :setting:`ROOT_URLCONF` setting.
  485. * Although the above example would work in the Python interactive
  486. interpreter, some of the test client's functionality, notably the
  487. template-related functionality, is only available *while tests are
  488. running*.
  489. The reason for this is that Django's test runner performs a bit of black
  490. magic in order to determine which template was loaded by a given view.
  491. This black magic (essentially a patching of Django's template system in
  492. memory) only happens during test running.
  493. * By default, the test client will disable any CSRF checks
  494. performed by your site.
  495. .. versionadded:: 1.2.2
  496. If, for some reason, you *want* the test client to perform CSRF
  497. checks, you can create an instance of the test client that
  498. enforces CSRF checks. To do this, pass in the
  499. ``enforce_csrf_checks`` argument when you construct your
  500. client::
  501. >>> from django.test import Client
  502. >>> csrf_client = Client(enforce_csrf_checks=True)
  503. .. _urllib: http://docs.python.org/library/urllib.html
  504. .. _urllib2: http://docs.python.org/library/urllib2.html
  505. Making requests
  506. ~~~~~~~~~~~~~~~
  507. Use the ``django.test.client.Client`` class to make requests. It requires no
  508. arguments at time of construction:
  509. .. class:: Client()
  510. Once you have a ``Client`` instance, you can call any of the following
  511. methods:
  512. .. method:: Client.get(path, data={}, follow=False, **extra)
  513. Makes a GET request on the provided ``path`` and returns a ``Response``
  514. object, which is documented below.
  515. The key-value pairs in the ``data`` dictionary are used to create a GET
  516. data payload. For example::
  517. >>> c = Client()
  518. >>> c.get('/customers/details/', {'name': 'fred', 'age': 7})
  519. ...will result in the evaluation of a GET request equivalent to::
  520. /customers/details/?name=fred&age=7
  521. The ``extra`` keyword arguments parameter can be used to specify
  522. headers to be sent in the request. For example::
  523. >>> c = Client()
  524. >>> c.get('/customers/details/', {'name': 'fred', 'age': 7},
  525. ... HTTP_X_REQUESTED_WITH='XMLHttpRequest')
  526. ...will send the HTTP header ``HTTP_X_REQUESTED_WITH`` to the
  527. details view, which is a good way to test code paths that use the
  528. :meth:`django.http.HttpRequest.is_ajax()` method.
  529. .. admonition:: CGI specification
  530. The headers sent via ``**extra`` should follow CGI_ specification.
  531. For example, emulating a different "Host" header as sent in the
  532. HTTP request from the browser to the server should be passed
  533. as ``HTTP_HOST``.
  534. .. _CGI: http://www.w3.org/CGI/
  535. If you already have the GET arguments in URL-encoded form, you can
  536. use that encoding instead of using the data argument. For example,
  537. the previous GET request could also be posed as::
  538. >>> c = Client()
  539. >>> c.get('/customers/details/?name=fred&age=7')
  540. If you provide a URL with both an encoded GET data and a data argument,
  541. the data argument will take precedence.
  542. If you set ``follow`` to ``True`` the client will follow any redirects
  543. and a ``redirect_chain`` attribute will be set in the response object
  544. containing tuples of the intermediate urls and status codes.
  545. If you had an url ``/redirect_me/`` that redirected to ``/next/``, that
  546. redirected to ``/final/``, this is what you'd see::
  547. >>> response = c.get('/redirect_me/', follow=True)
  548. >>> response.redirect_chain
  549. [(u'http://testserver/next/', 302), (u'http://testserver/final/', 302)]
  550. .. method:: Client.post(path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra)
  551. Makes a POST request on the provided ``path`` and returns a
  552. ``Response`` object, which is documented below.
  553. The key-value pairs in the ``data`` dictionary are used to submit POST
  554. data. For example::
  555. >>> c = Client()
  556. >>> c.post('/login/', {'name': 'fred', 'passwd': 'secret'})
  557. ...will result in the evaluation of a POST request to this URL::
  558. /login/
  559. ...with this POST data::
  560. name=fred&passwd=secret
  561. If you provide ``content_type`` (e.g., ``text/xml`` for an XML
  562. payload), the contents of ``data`` will be sent as-is in the POST
  563. request, using ``content_type`` in the HTTP ``Content-Type`` header.
  564. If you don't provide a value for ``content_type``, the values in
  565. ``data`` will be transmitted with a content type of
  566. ``multipart/form-data``. In this case, the key-value pairs in ``data``
  567. will be encoded as a multipart message and used to create the POST data
  568. payload.
  569. To submit multiple values for a given key -- for example, to specify
  570. the selections for a ``<select multiple>`` -- provide the values as a
  571. list or tuple for the required key. For example, this value of ``data``
  572. would submit three selected values for the field named ``choices``::
  573. {'choices': ('a', 'b', 'd')}
  574. Submitting files is a special case. To POST a file, you need only
  575. provide the file field name as a key, and a file handle to the file you
  576. wish to upload as a value. For example::
  577. >>> c = Client()
  578. >>> f = open('wishlist.doc')
  579. >>> c.post('/customers/wishes/', {'name': 'fred', 'attachment': f})
  580. >>> f.close()
  581. (The name ``attachment`` here is not relevant; use whatever name your
  582. file-processing code expects.)
  583. Note that if you wish to use the same file handle for multiple
  584. ``post()`` calls then you will need to manually reset the file
  585. pointer between posts. The easiest way to do this is to
  586. manually close the file after it has been provided to
  587. ``post()``, as demonstrated above.
  588. You should also ensure that the file is opened in a way that
  589. allows the data to be read. If your file contains binary data
  590. such as an image, this means you will need to open the file in
  591. ``rb`` (read binary) mode.
  592. The ``extra`` argument acts the same as for :meth:`Client.get`.
  593. If the URL you request with a POST contains encoded parameters, these
  594. parameters will be made available in the request.GET data. For example,
  595. if you were to make the request::
  596. >>> c.post('/login/?visitor=true', {'name': 'fred', 'passwd': 'secret'})
  597. ... the view handling this request could interrogate request.POST
  598. to retrieve the username and password, and could interrogate request.GET
  599. to determine if the user was a visitor.
  600. If you set ``follow`` to ``True`` the client will follow any redirects
  601. and a ``redirect_chain`` attribute will be set in the response object
  602. containing tuples of the intermediate urls and status codes.
  603. .. method:: Client.head(path, data={}, follow=False, **extra)
  604. Makes a HEAD request on the provided ``path`` and returns a ``Response``
  605. object. Useful for testing RESTful interfaces. Acts just like
  606. :meth:`Client.get` except it does not return a message body.
  607. If you set ``follow`` to ``True`` the client will follow any redirects
  608. and a ``redirect_chain`` attribute will be set in the response object
  609. containing tuples of the intermediate urls and status codes.
  610. .. method:: Client.options(path, data={}, follow=False, **extra)
  611. Makes an OPTIONS request on the provided ``path`` and returns a
  612. ``Response`` object. Useful for testing RESTful interfaces.
  613. If you set ``follow`` to ``True`` the client will follow any redirects
  614. and a ``redirect_chain`` attribute will be set in the response object
  615. containing tuples of the intermediate urls and status codes.
  616. The ``extra`` argument acts the same as for :meth:`Client.get`.
  617. .. method:: Client.put(path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra)
  618. Makes a PUT request on the provided ``path`` and returns a
  619. ``Response`` object. Useful for testing RESTful interfaces. Acts just
  620. like :meth:`Client.post` except with the PUT request method.
  621. If you set ``follow`` to ``True`` the client will follow any redirects
  622. and a ``redirect_chain`` attribute will be set in the response object
  623. containing tuples of the intermediate urls and status codes.
  624. .. method:: Client.delete(path, follow=False, **extra)
  625. Makes an DELETE request on the provided ``path`` and returns a
  626. ``Response`` object. Useful for testing RESTful interfaces.
  627. If you set ``follow`` to ``True`` the client will follow any redirects
  628. and a ``redirect_chain`` attribute will be set in the response object
  629. containing tuples of the intermediate urls and status codes.
  630. The ``extra`` argument acts the same as for :meth:`Client.get`.
  631. .. method:: Client.login(**credentials)
  632. If your site uses Django's :doc:`authentication system</topics/auth>`
  633. and you deal with logging in users, you can use the test client's
  634. ``login()`` method to simulate the effect of a user logging into the
  635. site.
  636. After you call this method, the test client will have all the cookies
  637. and session data required to pass any login-based tests that may form
  638. part of a view.
  639. The format of the ``credentials`` argument depends on which
  640. :ref:`authentication backend <authentication-backends>` you're using
  641. (which is configured by your :setting:`AUTHENTICATION_BACKENDS`
  642. setting). If you're using the standard authentication backend provided
  643. by Django (``ModelBackend``), ``credentials`` should be the user's
  644. username and password, provided as keyword arguments::
  645. >>> c = Client()
  646. >>> c.login(username='fred', password='secret')
  647. # Now you can access a view that's only available to logged-in users.
  648. If you're using a different authentication backend, this method may
  649. require different credentials. It requires whichever credentials are
  650. required by your backend's ``authenticate()`` method.
  651. ``login()`` returns ``True`` if it the credentials were accepted and
  652. login was successful.
  653. Finally, you'll need to remember to create user accounts before you can
  654. use this method. As we explained above, the test runner is executed
  655. using a test database, which contains no users by default. As a result,
  656. user accounts that are valid on your production site will not work
  657. under test conditions. You'll need to create users as part of the test
  658. suite -- either manually (using the Django model API) or with a test
  659. fixture. Remember that if you want your test user to have a password,
  660. you can't set the user's password by setting the password attribute
  661. directly -- you must use the
  662. :meth:`~django.contrib.auth.models.User.set_password()` function to
  663. store a correctly hashed password. Alternatively, you can use the
  664. :meth:`~django.contrib.auth.models.UserManager.create_user` helper
  665. method to create a new user with a correctly hashed password.
  666. .. method:: Client.logout()
  667. If your site uses Django's :doc:`authentication system</topics/auth>`,
  668. the ``logout()`` method can be used to simulate the effect of a user
  669. logging out of your site.
  670. After you call this method, the test client will have all the cookies
  671. and session data cleared to defaults. Subsequent requests will appear
  672. to come from an AnonymousUser.
  673. Testing responses
  674. ~~~~~~~~~~~~~~~~~
  675. The ``get()`` and ``post()`` methods both return a ``Response`` object. This
  676. ``Response`` object is *not* the same as the ``HttpResponse`` object returned
  677. Django views; the test response object has some additional data useful for
  678. test code to verify.
  679. Specifically, a ``Response`` object has the following attributes:
  680. .. class:: Response()
  681. .. attribute:: client
  682. The test client that was used to make the request that resulted in the
  683. response.
  684. .. attribute:: content
  685. The body of the response, as a string. This is the final page content as
  686. rendered by the view, or any error message.
  687. .. attribute:: context
  688. The template ``Context`` instance that was used to render the template that
  689. produced the response content.
  690. If the rendered page used multiple templates, then ``context`` will be a
  691. list of ``Context`` objects, in the order in which they were rendered.
  692. Regardless of the number of templates used during rendering, you can
  693. retrieve context values using the ``[]`` operator. For example, the
  694. context variable ``name`` could be retrieved using::
  695. >>> response = client.get('/foo/')
  696. >>> response.context['name']
  697. 'Arthur'
  698. .. attribute:: request
  699. The request data that stimulated the response.
  700. .. attribute:: status_code
  701. The HTTP status of the response, as an integer. See RFC2616_ for a full
  702. list of HTTP status codes.
  703. .. versionadded:: 1.3
  704. .. attribute:: templates
  705. A list of ``Template`` instances used to render the final content, in
  706. the order they were rendered. For each template in the list, use
  707. ``template.name`` to get the template's file name, if the template was
  708. loaded from a file. (The name is a string such as
  709. ``'admin/index.html'``.)
  710. You can also use dictionary syntax on the response object to query the value
  711. of any settings in the HTTP headers. For example, you could determine the
  712. content type of a response using ``response['Content-Type']``.
  713. .. _RFC2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  714. Exceptions
  715. ~~~~~~~~~~
  716. If you point the test client at a view that raises an exception, that exception
  717. will be visible in the test case. You can then use a standard ``try...except``
  718. block or ``unittest.TestCase.assertRaises()`` to test for exceptions.
  719. The only exceptions that are not visible to the test client are ``Http404``,
  720. ``PermissionDenied`` and ``SystemExit``. Django catches these exceptions
  721. internally and converts them into the appropriate HTTP response codes. In these
  722. cases, you can check ``response.status_code`` in your test.
  723. Persistent state
  724. ~~~~~~~~~~~~~~~~
  725. The test client is stateful. If a response returns a cookie, then that cookie
  726. will be stored in the test client and sent with all subsequent ``get()`` and
  727. ``post()`` requests.
  728. Expiration policies for these cookies are not followed. If you want a cookie
  729. to expire, either delete it manually or create a new ``Client`` instance (which
  730. will effectively delete all cookies).
  731. A test client has two attributes that store persistent state information. You
  732. can access these properties as part of a test condition.
  733. .. attribute:: Client.cookies
  734. A Python ``SimpleCookie`` object, containing the current values of all the
  735. client cookies. See the `Cookie module documentation`_ for more.
  736. .. attribute:: Client.session
  737. A dictionary-like object containing session information. See the
  738. :doc:`session documentation</topics/http/sessions>` for full details.
  739. To modify the session and then save it, it must be stored in a variable
  740. first (because a new ``SessionStore`` is created every time this property
  741. is accessed)::
  742. def test_something(self):
  743. session = self.client.session
  744. session['somekey'] = 'test'
  745. session.save()
  746. .. _Cookie module documentation: http://docs.python.org/library/cookie.html
  747. Example
  748. ~~~~~~~
  749. The following is a simple unit test using the test client::
  750. from django.utils import unittest
  751. from django.test.client import Client
  752. class SimpleTest(unittest.TestCase):
  753. def setUp(self):
  754. # Every test needs a client.
  755. self.client = Client()
  756. def test_details(self):
  757. # Issue a GET request.
  758. response = self.client.get('/customer/details/')
  759. # Check that the response is 200 OK.
  760. self.assertEqual(response.status_code, 200)
  761. # Check that the rendered context contains 5 customers.
  762. self.assertEqual(len(response.context['customers']), 5)
  763. The request factory
  764. -------------------
  765. .. Class:: RequestFactory
  766. .. versionadded:: 1.3
  767. The :class:`~django.test.client.RequestFactory` shares the same API as
  768. the test client. However, instead of behaving like a browser, the
  769. RequestFactory provides a way to generate a request instance that can
  770. be used as the first argument to any view. This means you can test a
  771. view function the same way as you would test any other function -- as
  772. a black box, with exactly known inputs, testing for specific outputs.
  773. The API for the :class:`~django.test.client.RequestFactory` is a slightly
  774. restricted subset of the test client API:
  775. * It only has access to the HTTP methods :meth:`~Client.get()`,
  776. :meth:`~Client.post()`, :meth:`~Client.put()`,
  777. :meth:`~Client.delete()`, :meth:`~Client.head()` and
  778. :meth:`~Client.options()`.
  779. * These methods accept all the same arguments *except* for
  780. ``follows``. Since this is just a factory for producing
  781. requests, it's up to you to handle the response.
  782. * It does not support middleware. Session and authentication
  783. attributes must be supplied by the test itself if required
  784. for the view to function properly.
  785. Example
  786. ~~~~~~~
  787. The following is a simple unit test using the request factory::
  788. from django.utils import unittest
  789. from django.test.client import RequestFactory
  790. class SimpleTest(unittest.TestCase):
  791. def setUp(self):
  792. # Every test needs access to the request factory.
  793. self.factory = RequestFactory()
  794. def test_details(self):
  795. # Create an instance of a GET request.
  796. request = self.factory.get('/customer/details')
  797. # Test my_view() as if it were deployed at /customer/details
  798. response = my_view(request)
  799. self.assertEqual(response.status_code, 200)
  800. TestCase
  801. --------
  802. .. currentmodule:: django.test
  803. Normal Python unit test classes extend a base class of ``unittest.TestCase``.
  804. Django provides an extension of this base class:
  805. .. class:: TestCase()
  806. This class provides some additional capabilities that can be useful for testing
  807. Web sites.
  808. Converting a normal ``unittest.TestCase`` to a Django ``TestCase`` is easy:
  809. just change the base class of your test from ``unittest.TestCase`` to
  810. ``django.test.TestCase``. All of the standard Python unit test functionality
  811. will continue to be available, but it will be augmented with some useful
  812. additions, including:
  813. * Automatic loading of fixtures.
  814. * Wraps each test in a transaction.
  815. * Creates a TestClient instance.
  816. * Django-specific assertions for testing for things
  817. like redirection and form errors.
  818. .. class:: TransactionTestCase()
  819. Django ``TestCase`` classes make use of database transaction facilities, if
  820. available, to speed up the process of resetting the database to a known state
  821. at the beginning of each test. A consequence of this, however, is that the
  822. effects of transaction commit and rollback cannot be tested by a Django
  823. ``TestCase`` class. If your test requires testing of such transactional
  824. behavior, you should use a Django ``TransactionTestCase``.
  825. ``TransactionTestCase`` and ``TestCase`` are identical except for the manner
  826. in which the database is reset to a known state and the ability for test code
  827. to test the effects of commit and rollback. A ``TransactionTestCase`` resets
  828. the database before the test runs by truncating all tables and reloading
  829. initial data. A ``TransactionTestCase`` may call commit and rollback and
  830. observe the effects of these calls on the database.
  831. A ``TestCase``, on the other hand, does not truncate tables and reload initial
  832. data at the beginning of a test. Instead, it encloses the test code in a
  833. database transaction that is rolled back at the end of the test. It also
  834. prevents the code under test from issuing any commit or rollback operations
  835. on the database, to ensure that the rollback at the end of the test restores
  836. the database to its initial state. In order to guarantee that all ``TestCase``
  837. code starts with a clean database, the Django test runner runs all ``TestCase``
  838. tests first, before any other tests (e.g. doctests) that may alter the
  839. database without restoring it to its original state.
  840. When running on a database that does not support rollback (e.g. MySQL with the
  841. MyISAM storage engine), ``TestCase`` falls back to initializing the database
  842. by truncating tables and reloading initial data.
  843. .. note::
  844. The ``TestCase`` use of rollback to un-do the effects of the test code
  845. may reveal previously-undetected errors in test code. For example,
  846. test code that assumes primary keys values will be assigned starting at
  847. one may find that assumption no longer holds true when rollbacks instead
  848. of table truncation are being used to reset the database. Similarly,
  849. the reordering of tests so that all ``TestCase`` classes run first may
  850. reveal unexpected dependencies on test case ordering. In such cases a
  851. quick fix is to switch the ``TestCase`` to a ``TransactionTestCase``.
  852. A better long-term fix, that allows the test to take advantage of the
  853. speed benefit of ``TestCase``, is to fix the underlying test problem.
  854. Default test client
  855. ~~~~~~~~~~~~~~~~~~~
  856. .. attribute:: TestCase.client
  857. Every test case in a ``django.test.TestCase`` instance has access to an
  858. instance of a Django test client. This client can be accessed as
  859. ``self.client``. This client is recreated for each test, so you don't have to
  860. worry about state (such as cookies) carrying over from one test to another.
  861. This means, instead of instantiating a ``Client`` in each test::
  862. from django.utils import unittest
  863. from django.test.client import Client
  864. class SimpleTest(unittest.TestCase):
  865. def test_details(self):
  866. client = Client()
  867. response = client.get('/customer/details/')
  868. self.assertEqual(response.status_code, 200)
  869. def test_index(self):
  870. client = Client()
  871. response = client.get('/customer/index/')
  872. self.assertEqual(response.status_code, 200)
  873. ...you can just refer to ``self.client``, like so::
  874. from django.test import TestCase
  875. class SimpleTest(TestCase):
  876. def test_details(self):
  877. response = self.client.get('/customer/details/')
  878. self.assertEqual(response.status_code, 200)
  879. def test_index(self):
  880. response = self.client.get('/customer/index/')
  881. self.assertEqual(response.status_code, 200)
  882. Customizing the test client
  883. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  884. .. versionadded:: 1.3
  885. .. attribute:: TestCase.client_class
  886. If you want to use a different ``Client`` class (for example, a subclass
  887. with customized behavior), use the :attr:`~TestCase.client_class` class
  888. attribute::
  889. from django.test import TestCase
  890. from django.test.client import Client
  891. class MyTestClient(Client):
  892. # Specialized methods for your environment...
  893. class MyTest(TestCase):
  894. client_class = MyTestClient
  895. def test_my_stuff(self):
  896. # Here self.client is an instance of MyTestClient...
  897. .. _topics-testing-fixtures:
  898. Fixture loading
  899. ~~~~~~~~~~~~~~~
  900. .. attribute:: TestCase.fixtures
  901. A test case for a database-backed Web site isn't much use if there isn't any
  902. data in the database. To make it easy to put test data into the database,
  903. Django's custom ``TestCase`` class provides a way of loading **fixtures**.
  904. A fixture is a collection of data that Django knows how to import into a
  905. database. For example, if your site has user accounts, you might set up a
  906. fixture of fake user accounts in order to populate your database during tests.
  907. The most straightforward way of creating a fixture is to use the
  908. :djadmin:`manage.py dumpdata <dumpdata>` command. This assumes you
  909. already have some data in your database. See the :djadmin:`dumpdata
  910. documentation<dumpdata>` for more details.
  911. .. note::
  912. If you've ever run :djadmin:`manage.py syncdb<syncdb>`, you've
  913. already used a fixture without even knowing it! When you call
  914. :djadmin:`syncdb` in the database for the first time, Django
  915. installs a fixture called ``initial_data``. This gives you a way
  916. of populating a new database with any initial data, such as a
  917. default set of categories.
  918. Fixtures with other names can always be installed manually using
  919. the :djadmin:`manage.py loaddata<loaddata>` command.
  920. .. admonition:: Initial SQL data and testing
  921. Django provides a second way to insert initial data into models --
  922. the :ref:`custom SQL hook <initial-sql>`. However, this technique
  923. *cannot* be used to provide initial data for testing purposes.
  924. Django's test framework flushes the contents of the test database
  925. after each test; as a result, any data added using the custom SQL
  926. hook will be lost.
  927. Once you've created a fixture and placed it in a ``fixtures`` directory in one
  928. of your :setting:`INSTALLED_APPS`, you can use it in your unit tests by
  929. specifying a ``fixtures`` class attribute on your :class:`django.test.TestCase`
  930. subclass::
  931. from django.test import TestCase
  932. from myapp.models import Animal
  933. class AnimalTestCase(TestCase):
  934. fixtures = ['mammals.json', 'birds']
  935. def setUp(self):
  936. # Test definitions as before.
  937. call_setup_methods()
  938. def testFluffyAnimals(self):
  939. # A test that uses the fixtures.
  940. call_some_test_code()
  941. Here's specifically what will happen:
  942. * At the start of each test case, before ``setUp()`` is run, Django will
  943. flush the database, returning the database to the state it was in
  944. directly after :djadmin:`syncdb` was called.
  945. * Then, all the named fixtures are installed. In this example, Django will
  946. install any JSON fixture named ``mammals``, followed by any fixture named
  947. ``birds``. See the :djadmin:`loaddata` documentation for more
  948. details on defining and installing fixtures.
  949. This flush/load procedure is repeated for each test in the test case, so you
  950. can be certain that the outcome of a test will not be affected by another test,
  951. or by the order of test execution.
  952. URLconf configuration
  953. ~~~~~~~~~~~~~~~~~~~~~
  954. .. attribute:: TestCase.urls
  955. If your application provides views, you may want to include tests that use the
  956. test client to exercise those views. However, an end user is free to deploy the
  957. views in your application at any URL of their choosing. This means that your
  958. tests can't rely upon the fact that your views will be available at a
  959. particular URL.
  960. In order to provide a reliable URL space for your test,
  961. ``django.test.TestCase`` provides the ability to customize the URLconf
  962. configuration for the duration of the execution of a test suite. If your
  963. ``TestCase`` instance defines an ``urls`` attribute, the ``TestCase`` will use
  964. the value of that attribute as the :setting:`ROOT_URLCONF` for the duration
  965. of that test.
  966. For example::
  967. from django.test import TestCase
  968. class TestMyViews(TestCase):
  969. urls = 'myapp.test_urls'
  970. def testIndexPageView(self):
  971. # Here you'd test your view using ``Client``.
  972. call_some_test_code()
  973. This test case will use the contents of ``myapp.test_urls`` as the
  974. URLconf for the duration of the test case.
  975. .. _emptying-test-outbox:
  976. Multi-database support
  977. ~~~~~~~~~~~~~~~~~~~~~~
  978. .. attribute:: TestCase.multi_db
  979. .. versionadded:: 1.2
  980. Django sets up a test database corresponding to every database that is
  981. defined in the :setting:`DATABASES` definition in your settings
  982. file. However, a big part of the time taken to run a Django TestCase
  983. is consumed by the call to ``flush`` that ensures that you have a
  984. clean database at the start of each test run. If you have multiple
  985. databases, multiple flushes are required (one for each database),
  986. which can be a time consuming activity -- especially if your tests
  987. don't need to test multi-database activity.
  988. As an optimization, Django only flushes the ``default`` database at
  989. the start of each test run. If your setup contains multiple databases,
  990. and you have a test that requires every database to be clean, you can
  991. use the ``multi_db`` attribute on the test suite to request a full
  992. flush.
  993. For example::
  994. class TestMyViews(TestCase):
  995. multi_db = True
  996. def testIndexPageView(self):
  997. call_some_test_code()
  998. This test case will flush *all* the test databases before running
  999. ``testIndexPageView``.
  1000. Emptying the test outbox
  1001. ~~~~~~~~~~~~~~~~~~~~~~~~
  1002. If you use Django's custom ``TestCase`` class, the test runner will clear the
  1003. contents of the test e-mail outbox at the start of each test case.
  1004. For more detail on e-mail services during tests, see `E-mail services`_.
  1005. Assertions
  1006. ~~~~~~~~~~
  1007. .. versionchanged:: 1.2
  1008. Addded ``msg_prefix`` argument.
  1009. As Python's normal ``unittest.TestCase`` class implements assertion methods
  1010. such as ``assertTrue`` and ``assertEqual``, Django's custom ``TestCase`` class
  1011. provides a number of custom assertion methods that are useful for testing Web
  1012. applications:
  1013. The failure messages given by the assertion methods can be customized
  1014. with the ``msg_prefix`` argument. This string will be prefixed to any
  1015. failure message generated by the assertion. This allows you to provide
  1016. additional details that may help you to identify the location and
  1017. cause of an failure in your test suite.
  1018. .. method:: TestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='')
  1019. Asserts that a ``Response`` instance produced the given ``status_code`` and
  1020. that ``text`` appears in the content of the response. If ``count`` is
  1021. provided, ``text`` must occur exactly ``count`` times in the response.
  1022. .. method:: TestCase.assertNotContains(response, text, status_code=200, msg_prefix='')
  1023. Asserts that a ``Response`` instance produced the given ``status_code`` and
  1024. that ``text`` does not appears in the content of the response.
  1025. .. method:: TestCase.assertFormError(response, form, field, errors, msg_prefix='')
  1026. Asserts that a field on a form raises the provided list of errors when
  1027. rendered on the form.
  1028. ``form`` is the name the ``Form`` instance was given in the template
  1029. context.
  1030. ``field`` is the name of the field on the form to check. If ``field``
  1031. has a value of ``None``, non-field errors (errors you can access via
  1032. ``form.non_field_errors()``) will be checked.
  1033. ``errors`` is an error string, or a list of error strings, that are
  1034. expected as a result of form validation.
  1035. .. method:: TestCase.assertTemplateUsed(response, template_name, msg_prefix='')
  1036. Asserts that the template with the given name was used in rendering the
  1037. response.
  1038. The name is a string such as ``'admin/index.html'``.
  1039. .. method:: TestCase.assertTemplateNotUsed(response, template_name, msg_prefix='')
  1040. Asserts that the template with the given name was *not* used in rendering
  1041. the response.
  1042. .. method:: TestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, msg_prefix='')
  1043. Asserts that the response return a ``status_code`` redirect status, it
  1044. redirected to ``expected_url`` (including any GET data), and the final
  1045. page was received with ``target_status_code``.
  1046. If your request used the ``follow`` argument, the ``expected_url`` and
  1047. ``target_status_code`` will be the url and status code for the final
  1048. point of the redirect chain.
  1049. .. method:: TestCase.assertQuerysetEqual(qs, values, transform=repr)
  1050. .. versionadded:: 1.3
  1051. Asserts that a queryset ``qs`` returns a particular list of values ``values``.
  1052. The comparison of the contents of ``qs`` and ``values`` is performed using
  1053. the function ``transform``; by default, this means that the ``repr()`` of
  1054. each value is compared. Any other callable can be used if ``repr()`` doesn't
  1055. provide a unique or helpful comparison.
  1056. The comparison is also ordering dependent. If ``qs`` doesn't provide an
  1057. implicit ordering, you will need to apply a ``order_by()`` clause to your
  1058. queryset to ensure that the test will pass reliably.
  1059. .. method:: TestCase.assertNumQueries(num, func, *args, **kwargs)
  1060. .. versionadded:: 1.3
  1061. Asserts that when ``func`` is called with ``*args`` and ``**kwargs`` that
  1062. ``num`` database queries are executed.
  1063. If a ``"using"`` key is present in ``kwargs`` it is used as the database
  1064. alias for which to check the number of queries. If you wish to call a
  1065. function with a ``using`` parameter you can do it by wrapping the call with
  1066. a ``lambda`` to add an extra parameter::
  1067. self.assertNumQueries(7, lambda: my_function(using=7))
  1068. If you're using Python 2.5 or greater you can also use this as a context
  1069. manager::
  1070. # This is necessary in Python 2.5 to enable the with statement, in 2.6
  1071. # and up it is no longer necessary.
  1072. from __future__ import with_statement
  1073. with self.assertNumQueries(2):
  1074. Person.objects.create(name="Aaron")
  1075. Person.objects.create(name="Daniel")
  1076. .. _topics-testing-email:
  1077. E-mail services
  1078. ---------------
  1079. If any of your Django views send e-mail using :doc:`Django's e-mail
  1080. functionality </topics/email>`, you probably don't want to send e-mail each time
  1081. you run a test using that view. For this reason, Django's test runner
  1082. automatically redirects all Django-sent e-mail to a dummy outbox. This lets you
  1083. test every aspect of sending e-mail -- from the number of messages sent to the
  1084. contents of each message -- without actually sending the messages.
  1085. The test runner accomplishes this by transparently replacing the normal
  1086. email backend with a testing backend.
  1087. (Don't worry -- this has no effect on any other e-mail senders outside of
  1088. Django, such as your machine's mail server, if you're running one.)
  1089. .. currentmodule:: django.core.mail
  1090. .. data:: django.core.mail.outbox
  1091. During test running, each outgoing e-mail is saved in
  1092. ``django.core.mail.outbox``. This is a simple list of all
  1093. :class:`~django.core.mail.EmailMessage` instances that have been sent.
  1094. The ``outbox`` attribute is a special attribute that is created *only* when
  1095. the ``locmem`` e-mail backend is used. It doesn't normally exist as part of the
  1096. :mod:`django.core.mail` module and you can't import it directly. The code
  1097. below shows how to access this attribute correctly.
  1098. Here's an example test that examines ``django.core.mail.outbox`` for length
  1099. and contents::
  1100. from django.core import mail
  1101. from django.test import TestCase
  1102. class EmailTest(TestCase):
  1103. def test_send_email(self):
  1104. # Send message.
  1105. mail.send_mail('Subject here', 'Here is the message.',
  1106. 'from@example.com', ['to@example.com'],
  1107. fail_silently=False)
  1108. # Test that one message has been sent.
  1109. self.assertEqual(len(mail.outbox), 1)
  1110. # Verify that the subject of the first message is correct.
  1111. self.assertEqual(mail.outbox[0].subject, 'Subject here')
  1112. As noted :ref:`previously <emptying-test-outbox>`, the test outbox is emptied
  1113. at the start of every test in a Django ``TestCase``. To empty the outbox
  1114. manually, assign the empty list to ``mail.outbox``::
  1115. from django.core import mail
  1116. # Empty the test outbox
  1117. mail.outbox = []
  1118. Skipping tests
  1119. --------------
  1120. .. versionadded:: 1.3
  1121. The unittest library provides the ``@skipIf`` and ``@skipUnless``
  1122. decorators to allow you to skip tests if you know ahead of time that
  1123. those tests are going to fail under certain conditions.
  1124. For example, if your test requires a particular optional library in
  1125. order to succeed, you could decorate the test case with ``@skipIf``.
  1126. Then, the test runner will report that the test wasn't executed and
  1127. why, instead of failing the test or omitting the test altogether.
  1128. To supplement these test skipping behaviors, Django provides two
  1129. additional skip decorators. Instead of testing a generic boolean,
  1130. these decorators check the capabilities of the database, and skip the
  1131. test if the database doesn't support a specific named feature.
  1132. The decorators use a string identifier to describe database features.
  1133. This string corresponds to attributes of the database connection
  1134. features class. See :class:`~django.db.backends.BaseDatabaseFeatures`
  1135. class for a full list of database features that can be used as a basis
  1136. for skipping tests.
  1137. skipIfDBFeature
  1138. ~~~~~~~~~~~~~~~
  1139. Skip the decorated test if the named database feature is supported.
  1140. For example, the following test will not be executed if the database
  1141. supports transactions (e.g., it would *not* run under PostgreSQL, but
  1142. it would under MySQL with MyISAM tables)::
  1143. class MyTests(TestCase):
  1144. @skipIfDBFeature('supports_transactions')
  1145. def test_transaction_behavior(self):
  1146. # ... conditional test code
  1147. skipUnlessDBFeature
  1148. ~~~~~~~~~~~~~~~~~~~
  1149. Skip the decorated test if the named database feature is *not*
  1150. supported.
  1151. For example, the following test will not be executed if the database
  1152. supports transactions (e.g., it would run under PostgreSQL, but *not*
  1153. under MySQL with MyISAM tables)::
  1154. class MyTests(TestCase):
  1155. @skipUnlessDBFeature('supports_transactions')
  1156. def test_transaction_behavior(self):
  1157. # ... conditional test code
  1158. Using different testing frameworks
  1159. ==================================
  1160. Clearly, ``doctest`` and ``unittest`` are not the only Python testing
  1161. frameworks. While Django doesn't provide explicit support for alternative
  1162. frameworks, it does provide a way to invoke tests constructed for an
  1163. alternative framework as if they were normal Django tests.
  1164. When you run ``./manage.py test``, Django looks at the :setting:`TEST_RUNNER`
  1165. setting to determine what to do. By default, :setting:`TEST_RUNNER` points to
  1166. ``'django.test.simple.DjangoTestSuiteRunner'``. This class defines the default Django
  1167. testing behavior. This behavior involves:
  1168. #. Performing global pre-test setup.
  1169. #. Looking for unit tests and doctests in the ``models.py`` and
  1170. ``tests.py`` files in each installed application.
  1171. #. Creating the test databases.
  1172. #. Running ``syncdb`` to install models and initial data into the test
  1173. databases.
  1174. #. Running the unit tests and doctests that are found.
  1175. #. Destroying the test databases.
  1176. #. Performing global post-test teardown.
  1177. If you define your own test runner class and point :setting:`TEST_RUNNER` at
  1178. that class, Django will execute your test runner whenever you run
  1179. ``./manage.py test``. In this way, it is possible to use any test framework
  1180. that can be executed from Python code, or to modify the Django test execution
  1181. process to satisfy whatever testing requirements you may have.
  1182. .. _topics-testing-test_runner:
  1183. Defining a test runner
  1184. ----------------------
  1185. .. versionchanged:: 1.2
  1186. Prior to 1.2, test runners were a single function, not a class.
  1187. .. currentmodule:: django.test.simple
  1188. A test runner is a class defining a ``run_tests()`` method. Django ships
  1189. with a ``DjangoTestSuiteRunner`` class that defines the default Django
  1190. testing behavior. This class defines the ``run_tests()`` entry point,
  1191. plus a selection of other methods that are used to by ``run_tests()`` to
  1192. set up, execute and tear down the test suite.
  1193. .. class:: DjangoTestSuiteRunner(verbosity=1, interactive=True, failfast=True, **kwargs)
  1194. ``verbosity`` determines the amount of notification and debug information
  1195. that will be printed to the console; ``0`` is no output, ``1`` is normal
  1196. output, and ``2`` is verbose output.
  1197. If ``interactive`` is ``True``, the test suite has permission to ask the
  1198. user for instructions when the test suite is executed. An example of this
  1199. behavior would be asking for permission to delete an existing test
  1200. database. If ``interactive`` is ``False``, the test suite must be able to
  1201. run without any manual intervention.
  1202. If ``failfast`` is ``True``, the test suite will stop running after the
  1203. first test failure is detected.
  1204. Django will, from time to time, extend the capabilities of
  1205. the test runner by adding new arguments. The ``**kwargs`` declaration
  1206. allows for this expansion. If you subclass ``DjangoTestSuiteRunner`` or
  1207. write your own test runner, ensure accept and handle the ``**kwargs``
  1208. parameter.
  1209. .. method:: DjangoTestSuiteRunner.run_tests(test_labels, extra_tests=None, **kwargs)
  1210. Run the test suite.
  1211. ``test_labels`` is a list of strings describing the tests to be run. A test
  1212. label can take one of three forms:
  1213. * ``app.TestCase.test_method`` -- Run a single test method in a test
  1214. case.
  1215. * ``app.TestCase`` -- Run all the test methods in a test case.
  1216. * ``app`` -- Search for and run all tests in the named application.
  1217. If ``test_labels`` has a value of ``None``, the test runner should run
  1218. search for tests in all the applications in :setting:`INSTALLED_APPS`.
  1219. ``extra_tests`` is a list of extra ``TestCase`` instances to add to the
  1220. suite that is executed by the test runner. These extra tests are run
  1221. in addition to those discovered in the modules listed in ``test_labels``.
  1222. This method should return the number of tests that failed.
  1223. .. method:: DjangoTestSuiteRunner.setup_test_environment(**kwargs)
  1224. Sets up the test environment ready for testing.
  1225. .. method:: DjangoTestSuiteRunner.build_suite(test_labels, extra_tests=None, **kwargs)
  1226. Constructs a test suite that matches the test labels provided.
  1227. ``test_labels`` is a list of strings describing the tests to be run. A test
  1228. label can take one of three forms:
  1229. * ``app.TestCase.test_method`` -- Run a single test method in a test
  1230. case.
  1231. * ``app.TestCase`` -- Run all the test methods in a test case.
  1232. * ``app`` -- Search for and run all tests in the named application.
  1233. If ``test_labels`` has a value of ``None``, the test runner should run
  1234. search for tests in all the applications in :setting:`INSTALLED_APPS`.
  1235. ``extra_tests`` is a list of extra ``TestCase`` instances to add to the
  1236. suite that is executed by the test runner. These extra tests are run
  1237. in addition to those discovered in the modules listed in ``test_labels``.
  1238. Returns a ``TestSuite`` instance ready to be run.
  1239. .. method:: DjangoTestSuiteRunner.setup_databases(**kwargs)
  1240. Creates the test databases.
  1241. Returns a data structure that provides enough detail to undo the changes
  1242. that have been made. This data will be provided to the ``teardown_databases()``
  1243. function at the conclusion of testing.
  1244. .. method:: DjangoTestSuiteRunner.run_suite(suite, **kwargs)
  1245. Runs the test suite.
  1246. Returns the result produced by the running the test suite.
  1247. .. method:: DjangoTestSuiteRunner.teardown_databases(old_config, **kwargs)
  1248. Destroys the test databases, restoring pre-test conditions.
  1249. ``old_config`` is a data structure defining the changes in the
  1250. database configuration that need to be reversed. It is the return
  1251. value of the ``setup_databases()`` method.
  1252. .. method:: DjangoTestSuiteRunner.teardown_test_environment(**kwargs)
  1253. Restores the pre-test environment.
  1254. .. method:: DjangoTestSuiteRunner.suite_result(suite, result, **kwargs)
  1255. Computes and returns a return code based on a test suite, and the result
  1256. from that test suite.
  1257. Testing utilities
  1258. -----------------
  1259. .. module:: django.test.utils
  1260. :synopsis: Helpers to write custom test runners.
  1261. To assist in the creation of your own test runner, Django provides a number of
  1262. utility methods in the ``django.test.utils`` module.
  1263. .. function:: setup_test_environment()
  1264. Performs any global pre-test setup, such as the installing the
  1265. instrumentation of the template rendering system and setting up
  1266. the dummy ``SMTPConnection``.
  1267. .. function:: teardown_test_environment()
  1268. Performs any global post-test teardown, such as removing the black
  1269. magic hooks into the template system and restoring normal e-mail
  1270. services.
  1271. The creation module of the database backend (``connection.creation``)
  1272. also provides some utilities that can be useful during testing.
  1273. .. function:: create_test_db(verbosity=1, autoclobber=False)
  1274. Creates a new test database and runs ``syncdb`` against it.
  1275. ``verbosity`` has the same behavior as in ``run_tests()``.
  1276. ``autoclobber`` describes the behavior that will occur if a
  1277. database with the same name as the test database is discovered:
  1278. * If ``autoclobber`` is ``False``, the user will be asked to
  1279. approve destroying the existing database. ``sys.exit`` is
  1280. called if the user does not approve.
  1281. * If autoclobber is ``True``, the database will be destroyed
  1282. without consulting the user.
  1283. Returns the name of the test database that it created.
  1284. ``create_test_db()`` has the side effect of modifying the value of
  1285. :setting:`NAME` in :setting:`DATABASES` to match the name of the test
  1286. database.
  1287. .. function:: destroy_test_db(old_database_name, verbosity=1)
  1288. Destroys the database whose name is in stored in :setting:`NAME` in the
  1289. :setting:`DATABASES`, and sets :setting:`NAME` to use the
  1290. provided name.
  1291. ``verbosity`` has the same behavior as in ``run_tests()``.