Python doctest

I've recently picked up Python again, and a neat feature that I ran across by chance is the doctest module.
It tests if the example in your docstrings actually do what you claim! 1

Let's say we have this neat is_leap_year function with a few examples.
Note that the examples are all prefixed with >>> (That's from the interactive prompt.)

def _is_leap_year(year: int) -> bool:
    """Determines if a year is a leap year.
    >>> _is_leap_year(2020)
    >>> _is_leap_year(2100)
    >>> _is_leap_year(2000)
    return divides(400, year) or (divides(4, year) and not divides(100, year))

if __name__ == '__main__':
    import doctest

Calling doctest.testmod looks at all the examples in this module, runs the code and compares the result.


This reminds me of the documentation tests feature in Rust (cargo test).