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.[src]
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.[src] ==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.[src] The secondary prompt: , is used when continuing commands on multiple lines; and the result of executing the command is expected on following lines.[src] A blank line, or another line starting with the primary prompt is seen as the end of the output from the command.[src] 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.[src] The default action when running doctests is for no output to be shown when tests pass.[src] This can be modified by options to the doctest runner.[src] In addition, doctest has been integrated with the Python unit test module allowing doctests to be run as standard unittest testcases.[src] Unittest testcase runners allow more options when running tests such as the reporting of test statistics such as tests passed, and failed.[src] ==Literate programming and doctests== Although doctest does not allow a Python program to be embedded in narrative text, it does allow for verifiable examples to be embedded in docstrings, where the docstrings can contain other text.[src] Docstrings can in turn be extracted from program files to generate documentation in other formats such as HTML or PDF.[src] A program file can be made to contain the documentation, tests, as well as the code and the tests easily verified against the code.[src] This allows code, tests, and documentation to evolve together.[src] ==Documenting libraries by example== Doctests are well suited to provide an introduction to a library by demonstrating how the API is used.[src] On the basis of the output of Python's interactive interpreter, text can be mixed with tests that exercise the library, showing expected results.[src] ==Examples== Example one shows how narrative text can be interspersed with testable examples in a docstring.[src] In the second example, more features of doctest are shown, together with their explanation.[src] 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.[src] def list_to_0_index(lst): """A solution to the problem given in: https://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.[src] So the list x = [0, 1, 4, 2, 4, 1, 0, 2] is transformed into y = [0, 1, 2, 3, 2, 1, 0, 3].[src] Notice that for all i we have x[y[i]] = x[i].[src] 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] ====================== Demonstration doctests >>> b = 2 >>> a + b 3 Notice, that we just demonstrated how to add two numbers in Python, and what the result will look like.[src] 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): ...[src] NameError: name 'x' is not defined def unique_words(page): """Return set of the unique words in list of lines of text.[src] Example: >>> from StringIO import StringIO >>> fileText = the cat sat on the mat ...[src] the mat was ondur the cat ...[src] one fish two fish red fish ...[src] blue fish ...[src] This fish has a yellow car ...[src] 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()[src]
31 sentences · 31 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Doctest." encyclopegia.org, 2026, https://encyclopegia.org/wiki/doctest. Accessed 29 November 2026.
Chicago
"Doctest." encyclopegia.org. Accessed November 29, 2026. https://encyclopegia.org/wiki/doctest.
APA
encyclopegia.org. (2026). Doctest. Retrieved November 29, 2026, from https://encyclopegia.org/wiki/doctest
Accessed November 29, 2026 · CC BY-SA 4.0 · citation page · JSON