doctests.txt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ===================
  2. Django and doctests
  3. ===================
  4. Doctests use Python's standard :mod:`doctest` module, which searches your
  5. docstrings for statements that resemble a session of the Python interactive
  6. interpreter. A full explanation of how :mod:`doctest` works is out of the scope
  7. of this document; read Python's official documentation for the details.
  8. .. admonition:: What's a **docstring**?
  9. A good explanation of docstrings (and some guidelines for using them
  10. effectively) can be found in :pep:`257`:
  11. A docstring is a string literal that occurs as the first statement in
  12. a module, function, class, or method definition. Such a docstring
  13. becomes the ``__doc__`` special attribute of that object.
  14. For example, this function has a docstring that describes what it does::
  15. def add_two(num):
  16. "Return the result of adding two to the provided number."
  17. return num + 2
  18. Because tests often make great documentation, putting tests directly in
  19. your docstrings is an effective way to document *and* test your code.
  20. As with unit tests, for a given Django application, the test runner looks for
  21. doctests in two places:
  22. * The ``models.py`` file. You can define module-level doctests and/or a
  23. doctest for individual models. It's common practice to put
  24. application-level doctests in the module docstring and model-level
  25. doctests in the model docstrings.
  26. * A file called ``tests.py`` in the application directory -- i.e., the
  27. directory that holds ``models.py``. This file is a hook for any and all
  28. doctests you want to write that aren't necessarily related to models.
  29. This example doctest is equivalent to the example given in the unittest section
  30. above::
  31. # models.py
  32. from django.db import models
  33. class Animal(models.Model):
  34. """
  35. An animal that knows how to make noise
  36. # Create some animals
  37. >>> lion = Animal.objects.create(name="lion", sound="roar")
  38. >>> cat = Animal.objects.create(name="cat", sound="meow")
  39. # Make 'em speak
  40. >>> lion.speak()
  41. 'The lion says "roar"'
  42. >>> cat.speak()
  43. 'The cat says "meow"'
  44. """
  45. name = models.CharField(max_length=20)
  46. sound = models.CharField(max_length=20)
  47. def speak(self):
  48. return 'The %s says "%s"' % (self.name, self.sound)
  49. When you :ref:`run your tests <running-tests>`, the test runner will find this
  50. docstring, notice that portions of it look like an interactive Python session,
  51. and execute those lines while checking that the results match.
  52. In the case of model tests, note that the test runner takes care of creating
  53. its own test database. That is, any test that accesses a database -- by
  54. creating and saving model instances, for example -- will not affect your
  55. production database. However, the database is not refreshed between doctests,
  56. so if your doctest requires a certain state you should consider flushing the
  57. database or loading a fixture. (See the section on :ref:`fixtures
  58. <topics-testing-fixtures>` for more on this.) Note that to use this feature,
  59. the database user Django is connecting as must have ``CREATE DATABASE``
  60. rights.
  61. For more details about :mod:`doctest`, see the Python documentation.