index.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. =================
  2. Testing in Django
  3. =================
  4. .. toctree::
  5. :hidden:
  6. overview
  7. doctests
  8. advanced
  9. Automated testing is an extremely useful bug-killing tool for the modern
  10. Web developer. You can use a collection of tests -- a **test suite** -- to
  11. solve, or avoid, a number of problems:
  12. * When you're writing new code, you can use tests to validate your code
  13. works as expected.
  14. * When you're refactoring or modifying old code, you can use tests to
  15. ensure your changes haven't affected your application's behavior
  16. unexpectedly.
  17. Testing a Web application is a complex task, because a Web application is made
  18. of several layers of logic -- from HTTP-level request handling, to form
  19. validation and processing, to template rendering. With Django's test-execution
  20. framework and assorted utilities, you can simulate requests, insert test data,
  21. inspect your application's output and generally verify your code is doing what
  22. it should be doing.
  23. The best part is, it's really easy.
  24. Unit tests v. doctests
  25. ======================
  26. There are two primary ways to write tests with Django, corresponding to the
  27. two test frameworks that ship in the Python standard library. The two
  28. frameworks are:
  29. * **Unit tests** -- tests that are expressed as methods on a Python class
  30. that subclasses :class:`unittest.TestCase` or Django's customized
  31. :class:`TestCase`. For example::
  32. import unittest
  33. class MyFuncTestCase(unittest.TestCase):
  34. def testBasic(self):
  35. a = ['larry', 'curly', 'moe']
  36. self.assertEqual(my_func(a, 0), 'larry')
  37. self.assertEqual(my_func(a, 1), 'curly')
  38. * **Doctests** -- tests that are embedded in your functions' docstrings and
  39. are written in a way that emulates a session of the Python interactive
  40. interpreter. For example::
  41. def my_func(a_list, idx):
  42. """
  43. >>> a = ['larry', 'curly', 'moe']
  44. >>> my_func(a, 0)
  45. 'larry'
  46. >>> my_func(a, 1)
  47. 'curly'
  48. """
  49. return a_list[idx]
  50. Which should I use?
  51. -------------------
  52. Because Django supports both of the standard Python test frameworks, it's up to
  53. you and your tastes to decide which one to use. You can even decide to use
  54. *both*.
  55. For developers new to testing, however, this choice can seem confusing. Here,
  56. then, are a few key differences to help you decide which approach is right for
  57. you:
  58. * If you've been using Python for a while, :mod:`doctest` will probably feel
  59. more "pythonic". It's designed to make writing tests as easy as possible,
  60. so it requires no overhead of writing classes or methods. You simply put
  61. tests in docstrings. This has the added advantage of serving as
  62. documentation (and correct documentation, at that!). However, while
  63. doctests are good for some simple example code, they are not very good if
  64. you want to produce either high quality, comprehensive tests or high
  65. quality documentation. Test failures are often difficult to debug
  66. as it can be unclear exactly why the test failed. Thus, doctests should
  67. generally be avoided and used primarily for documentation examples only.
  68. * The :mod:`unittest` framework will probably feel very familiar to
  69. developers coming from Java. :mod:`unittest` is inspired by Java's JUnit,
  70. so you'll feel at home with this method if you've used JUnit or any test
  71. framework inspired by JUnit.
  72. * If you need to write a bunch of tests that share similar code, then
  73. you'll appreciate the :mod:`unittest` framework's organization around
  74. classes and methods. This makes it easy to abstract common tasks into
  75. common methods. The framework also supports explicit setup and/or cleanup
  76. routines, which give you a high level of control over the environment
  77. in which your test cases are run.
  78. * If you're writing tests for Django itself, you should use :mod:`unittest`.
  79. Where to go from here
  80. =====================
  81. As unit tests are preferred in Django, we treat them in detail in the
  82. :doc:`overview` document.
  83. :doc:`doctests` describes Django-specific features when using doctests.
  84. You can also use any *other* Python test framework, Django provides an API and
  85. tools for that kind of integration. They are described in the
  86. :ref:`other-testing-frameworks` section of :doc:`advanced`.