doctest — Test interactive Python examples — Python 3.14.7 documentation

docs.python.org

Tin mới

Python Development Mode

unittest — Unit testing framework

Python Module Index

3.14.7 Documentation

The Python Standard Library

controlled using environment variables

Added in version 3.13: Output is colorized by default and can be controlled using environment variables.

Simple Usage: Checking Examples in Docstrings¶

The simplest way to start using doctest (but not necessarily the way you’ll continue to do it) is to end each module M with:

What About Exceptions?¶

No problem, provided that the traceback is the only output produced by the example: just paste in the traceback. [1] Since tracebacks contain details that are likely to change rapidly (for example, exact file paths and l

__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``.

All arguments are optional, and all except for m should be specified in keyword form.

traceback.format_exception_only()

The exception message generated by the example, if the example is expected to generate an exception; or None if it is not expected to generate an exception. This exception message is compared against the return value of

pdb: The Python debugger for interactive interpreters.

Several functions convert doctests to executable Python programs, which can be run under the Python debugger, pdb.