pydoc > doctest

Help on module doctest:

📘 NAME

doctest – Module doctest — a framework for running examples in docstrings.

🚀 Quick Reference

Use CaseCommand / CodeDescription
Run doctests in a modulepython M.py -vExecute module and verify docstring examples, verbose output
Run doctests programmaticallydoctest.testmod()Test all docstrings in the current module
Convert docstrings to scriptdoctest.script_from_examples(s)Turn doctest text into a Python script
Test a single filedoctest.testfile(filename)Run doctests from an external file
Integrate with unittestdoctest.DocTestSuite()Create a unittest suite from doctests
Debug a specific doctestdoctest.debug(module, name)Drop into debugger for a failing test
Customize comparisonELLIPSIS, NORMALIZE_WHITESPACE etc.Option flags to relax output matching

📖 MODULE REFERENCE

https://docs.python.org/3.10/library/doctest.html

The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.

📄 DESCRIPTION

In simplest use, end each module M to be tested with:

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

if __name__ == "__main__":
    _test()

Then running the module as a script will cause the examples in the docstrings to get executed and verified:

python M.py

This won't display anything unless an example fails, in which case the failing example(s) and the cause(s) are printed to stdout (why not stderr? because stderr is a lame hack <0.2 wink>), and the final line of output is "Test failed.".

Run it with the -v switch instead:

python M.py -v

and a detailed report of all examples tried is printed to stdout, along with assorted summaries at the end.

You can force verbose mode by passing verbose=True to testmod, or prohibit it by passing verbose=False. In either of those cases, sys.argv is not examined by testmod.

There are a variety of other ways to run doctests, including integration with the unittest framework, and support for running non-Python text files containing doctests. There are also many ways to override parts of doctest's default behaviors. See the Library Reference Manual for details.

📦 CLASSES

🗂️ DebugRunner (inherits DocTestRunner)

Run doc tests but raise an exception as soon as there is a failure. If an unexpected exception occurs, an UnexpectedException is raised. It contains the test, the example, and the original exception.

Methods inherited from DocTestRunner:

📦 DocTest (object)

A collection of doctest examples to be run in a single namespace. Attributes: examples, globs, name, filename, lineno, docstring.

⚠️ DocTestFailure (Exception)

A DocTest example has failed in debugging mode. Attributes: test (DocTest object), example (Example object that failed), got (actual output).

🔍 DocTestFinder (object)

Extracts DocTests from a given object’s docstring and its contained objects. Works with modules, functions, classes, methods, staticmethods, classmethods, properties.

🔧 DocTestParser (object)

Parses strings containing doctest examples.

🏃 DocTestRunner (object)

Runs DocTest test cases and accumulates statistics. run returns (f, t). summarize prints summary and returns aggregated (f, t).

📝 Example (object)

A single doctest example with source, want (expected output), exc_msg, lineno, indent, options.

OutputChecker (object)

Checks whether actual output matches expected output. Methods: check_output returns True if match, output_difference returns string describing differences.

⚠️ UnexpectedException (Exception)

A DocTest example encountered an unexpected exception. Attributes: test, example, exc_info.

🔧 FUNCTIONS

📈 DATA

📁 FILE

/usr/lib/python3.10/doctest.py

doctest
📘 NAME 🚀 Quick Reference 📖 MODULE REFERENCE 📄 DESCRIPTION 📦 CLASSES
🗂️ DebugRunner (inherits DocTestRunner) 📦 DocTest (object) ⚠️ DocTestFailure (Exception) 🔍 DocTestFinder (object) 🔧 DocTestParser (object) 🏃 DocTestRunner (object) 📝 Example (object) ✅ OutputChecker (object) ⚠️ UnexpectedException (Exception)
🔧 FUNCTIONS 📈 DATA 📁 FILE

Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 18:22 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^