Python doctests for inline testing
Aug 18, 2025
Python’s doctest
module allows you to write tests directly in docstrings by mimicking interactive Python sessions. Simply write >>>
followed by Python code and the expected output on the next line.
"""
Add two numbers.
>>> add(2, 3)
5
>>> add(-1, 1)
0
"""
return +
Run with python -m doctest filename.py
or include doctest.testmod()
in your script. Great for keeping examples and tests close to the code they document.