Doctest

Doctest

doctest is a module included in the Python programming language's standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstrings.

Implementation specifics

doctest makes innovative use of the following Python capabilities:
* docstrings
* The Python interactive shell, (both command line and the included idle application).
* Python introspection

When using the Python shell, the primary prompt: >>> , is followed by new commands. The secondary prompt: ... , is used when continuing commands on multiple lines; and the result of executing the command is expected on following lines.A blank line, or another line starting with the primary prompt is seen as the end of the output from the command.

The doctest module looks for such sequences of prompts in a docstring, re-executes the extracted command and checks the output against the output of the command given in the docstrings test example.

The default action when running doctests is for no output to be shown when tests pass. This can be modified by options to the doctest runner. In addition, doctest has been integrated with the Python unit test module allowing doctests to be run as standard unittest testcases. Unittest testcase runners allow more options when running tests such as the reporting of test statistics such as tests passed, and failed.

Literate Programming and Doctests

Although doctest does not allow a Python program to be embedded in narrative text, they do allow for verifiable examples to be embedded in docstrings, where the docstrings can contain other text. Docstrings can in turn be extracted from program files to generate documentation in other formats such as HTML or PDF. A program file can be made to contain the documentation, tests, as well as the code and the tests easily verified against the code. This allows code, tests, and documentation to evolve together.

Documenting libraries by example

Doctests are well suited to provide an introduction to a library by demonstrating how the API is used.

On the basis of the output of Python's interactive interpreter, text can be mixed with tests that exercise the library, showing expected results.

Examples

Example one shows how narrative text can be interspersed with testable examples in a docstring. In the second example, more features of doctest are shown, together with their explanation. Example three is set up to run all doctests in a file when the file is run, but when imported as a module, the tests will not be run.

Example 1: A doctest embedded in the docstring of a function

def list_to_0_index(lst): """A solution to the problem given in: http://rgrig.blogspot.com/2005/11/writing-readable-code.html 'Given a list, lst, say for each element the 0-index where it appears for the first time. So the list x = [0, 1, 4, 2, 4, 1, 0, 2] is transformed into y = [0, 1, 2, 3, 2, 1, 0, 3] . Notice that for all i we have xyi = xi. Use any programming language and any data representation you want.' >>> x = [0, 1, 4, 2, 4, 1, 0, 2] >>> list_to_0_index(x) [0, 1, 2, 3, 2, 1, 0, 3] >>> """ return [lst.index(i) for i in lst]

Example 2: doctests embedded in a README.txt file


Demonstration doctests


This is just an example of what a README text looks like that can be used withthe doctest.DocFileSuite() function from Python's doctest module.

Normally, the README file would explain the API of the module, like this:

>>> a = 1 >>> b = 2 >>> a + b 3

Notice, that we just demonstrated how to add two numbers in Python, and what the result will look like.

A special option allows you to be somewhat fuzzy about your examples:

>>> o = object() >>> o # doctest: +ELLIPSIS

Exceptions can be tested very nicely too:

>>> x Traceback (most recent call last): ... NameError: name 'x' is not defined

Example 3: unique_words.py

This example also simulates input to the function from a file by using the python [http://docs.python.org/lib/module-StringIO.html StringIO] moduledef unique_words(page): "' Returns set of the unique words in list of lines of text

Example:

>>> from StringIO import StringIO >>> fileText = """the cat sat on the mat ... the mat was ondur the cat ... one fish two fish red fish ... blue fish ... This fish has a yellow car ... This fish has a yellow star""" >>> file = StringIO(fileText) >>> page = file.readlines() >>> words = unique_words(page) >>> print sorted(list(words)) ['This', 'a', 'blue', 'car', 'cat', 'fish', 'has', 'mat', 'on', 'ondur', 'one', 'red', 'sat', 'star', 'the', 'two', 'was', 'yellow'] >>> "'

return set(word for line in page for word in line.split())

def _test(): import doctest doctest.testmod()

if __name__ = "__main__": _test()

Critique of doctest

Advantages

* Many programmers already use the Python shell to test their code. Cutting and pasting from the shell to a docstring is a very simple way of generating tests.
* Tests and code are together. The tests are easily run making them much more likely to be kept up-to-date.
* Some level of documentation, tests and code are kept together.
* Testing of exceptions looks very intuitive.
* A simple concept.
* Can test for returned exceptions
* Nice output formatting/comparison integration (e.g. whitespace normalization, ndiff usage for failed examples, use of ellipsis).

Disadvantages

* Large numbers of tests in a docstring can become unwieldy. docstrings should be pruned and excised tests put in external file(s).
* Tests producing large amounts of output make for large docstrings.
* Debugging integration is far from perfect.
* 'print' (or 'trace') debugging is not possible (because it intervenes with the test result).
* Test setup has to be either copied or hidden away from the test, making the overall environment harder to understand.
* A few of the complex assertions of other unit test frameworks do not exist, (e.g. assert_almost_equal).
* Failing assertions can be harder to debug if you compare a lot of output. This is especially the case for web applications if the expected result is a web page with a lot of HTML.

Doctest and Documentation generators

Both the [http://epydoc.sourceforge.net/epytextintro.html EpyText] format of Epydoc, and the [http://docutils.sourceforge.net/rst.html reStructuredText] format of [http://docutils.sourceforge.net/index.html Docutils] , support the markup of doctest sections within docstrings.

References

* cite newsgroup
author = Tim Peters
date = 1999-03-06
title = docstring-driven testing
id = 000001be679b$0b263e00$a99e2299@tim
url = http://groups.google.com/group/comp.lang.python/msg/1c57cfb7b3772763

* cite web
title = Development Tools – doctest
work = Python Library Reference
url = http://python.org/doc/current/lib/module-doctest.html
date = 2006-09-19

* [http://blog.ianbicking.org/minimock.html minimock] - Minimalist mock object implementation for doctest by Ian Bicking.
* [http://github.com/tablatom/rubydoctest/wikis Doctest for Ruby and Rails] .
* [http://svn.colorstudy.com/doctestjs/trunk/docs/index.html Doctest/JS] an implementation for Javascript.
* [http://www.cis.upenn.edu/~edloper/projects/doctestmode/ doctest-mode] - Emacs mode for editing doctests.

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • doctest — is a module included in the Python programming language s standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstrings. Contents 1 Implementation specifics 2… …   Wikipedia

  • Behavior Driven Development — (or BDD) is an Agile software development technique that encourages collaboration between developers, QA and non technical or business participants in a software project. It was originally conceived in 2003 by Dan North D.North,… …   Wikipedia

  • Python — У этого термина существуют и другие значения, см. Python (значения). Python Класс языка: му …   Википедия

  • Zope 3 — is the new generation of the popular Zope web application server, rewritten from scratch with a component architecture and which has evolved into a large library of reusable Python components. The first production release of the software, Zope X3 …   Wikipedia

  • Read-eval-print loop — A read eval print loop (REPL), also known as an interactive toplevel, is a simple, interactive computer programming environment. The term is most usually used to refer to a Lisp interactive environment, but can be applied to command line shells… …   Wikipedia

  • Docstring — In programming, a docstring is a string literal specified in source code that is used, like a comment, to document a specific segment of code. Unlike conventional source code comments, or even specifically formatted comments like Javadoc… …   Wikipedia

  • Python syntax and semantics — The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted (by both the runtime system and by human readers). Python was designed to be a highly readable language. It aims… …   Wikipedia

  • Comparison of web application frameworks — This is a comparison of notable web application frameworks. Contents 1 General 1.1 Perl 1.2 PHP 1.3 Java 1.4 Python …   Wikipedia

  • Behavior Driven Development — (ou BDD) est une méthode Agile qui encourage la collaboration entre les développeurs, les responsables qualités, les intervenants non techniques et les entreprises participants à un projet de logiciel. Il a été conçu en 2003 par Dan North comme… …   Wikipédia en Français

  • Behavior driven development — (ou BDD) est une méthode Agile qui encourage la collaboration entre les développeurs, les responsables qualités, les intervenants non techniques et les entreprises participants à un projet de logiciel. Il a été conçu en 2003 par Dan North comme… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”