Test interactive Python examples with doctest


Python tip:

You can use the doctest module to run interactive code examples inside your docstrings.

It notifies you If any of the examples don't return the expected value.

👇

def sum_ab(a, b):
    """
    Sum numbers a and b
    >>> sum_ab(1, 3)
    4
    >>> sum_ab(-41, 50)
    1
    :return: sum of 2 numbers
    """
    return a + b


# python -m doctest example.py
# **********************************************************************
# File "example.py", line 4, in example.sum_ab
# Failed example:
#     sum_ab(-41, 50)
# Expected:
#     1
#     :return: sum of 2 numbers
# Got:
#     9
# **********************************************************************
# 1 items had failures:
#    1 of   2 in example.sum_ab
# ***Test Failed*** 1 failures.