Skip to content

Testing with doctest

Somkiat Puisungnoen edited this page Jun 12, 2025 · 3 revisions

Testing with doctest

File demo.py

import doctest

"""
Say hi to a person.
>>> say_hi("Somkiat")
Hi, Somkiat!
"""
def say_hi(name: str) -> str:
    """
    Print a greeting message with the provided name.
    
    Args:
        name (str): The name to greet.
    """
    return f"Hi, {name}!"

if __name__ == "__main__":
    doctest.testmod()  # Run the doctests in this module

Run

$python demo.py -v
2 items had no tests:
    __main__
    __main__.say_hi
0 tests in 2 items.
0 passed.
Test passed.

Run with PyDoc

$pydoc -p 1234
Clone this wiki locally