5 page-title: PyLit - Tutorial
6 file: testmod_literate.py
7 file: testfile_literate.py
10 How to write literate doctests with PyLit
11 =========================================
19 *Doctests* are a literate way of testing a Python script. They are
20 supported by the `doctest block`_ syntax in reStructured Text.
22 The `doctest module`_ searches strings for pieces of text that look like
23 interactive Python sessions, and then executes those sessions to verify that
24 they work exactly as shown.
26 * The most common way to use the doctest module is to check examples in all
27 *docstrings* of a module with ``doctest.testmod()``, e.g. ::
29 if __name__ == "__main__":
33 * You can also check a *text* file as if it were a docstring by calling
34 doctest from the command line, e.g. ::
36 python -c "import doctest; doctest.testfile('example.py.txt')"
38 However, both methods will not check doctest blocks in comments.
39 This is why they will fail to find doctests in the text blocks of a source
40 in code format (say ``example.py``). (See the tutorial_ for discussion.)
42 You can of course convert your source to text form and run doctest on it,
43 but there is an option to facilitate literal doctests:
46 will check a literate source file for all doctests regardless of their
47 location in docstrings or text parts. It can work with both, text or code
50 In order to do this, it will read the file, transform a code source to
51 text format on-the-fly and feed the result to a DocTestParser_ object.
53 This way, it is possible to separate basic examples in doc strings from
54 additional test in the literate source.
57 Checking Examples in Docstrings and Documentation text
58 ------------------------------------------------------
61 is a "literate version" of the example in the `doctest module`_ doc that
62 does a self test when called as `__main__`.
64 It calls `pylit.run_doctest` to find tests in both docstrings and
69 sh> python testmod_literate.py
70 0 failures in 14 tests
73 Checking Examples in a Literal Source File
74 --------------------------------------------------------
77 is a "literate version" of the example in the `doctest module`_ doc
78 adapted for beeing tested with ``pylit --doctest``.
82 sh> pylit --doctest testfile_literate.py
83 0 failures in 19 tests
90 .. _Python doctests: http://docs.python.org/lib/module-doctest.html
91 .. _DocTestParser: http://docs.python.org/lib/doctest-DocTestParser.html
92 .. _Advanced API: http://docs.python.org/lib/doctest-advanced-api.html
94 .. _tutorial: /tutorial/
95 .. _testmod_literate.py: testmod_literate.py
96 .. _testfile_literate.py: testfile_literate.py
99 .. _parsed-literal block:
100 http://docutils.sf.net/docs/ref/rst/directives.html#parsed-literal-block
102 http://docutils.sf.net/docs/ref/rst/restructuredtext.html#doctest-blocks