Python has built-in testing through documentation in addition to the classic tests. It is an unusual way to write tests, but developers sometimes use it for library functions. In this lesson, we'll study this topic in more detail to learn what such documentation looks like.
Testing through documentation
The idea is pretty simple. We call a function inside the terminal. The call itself and the resulting output are the tests. Then, we add them to the documentation of the function:
# Example.py has a function called `reverse()` that reverses a string
# The `-i` flag turns on the interactive mode after module execution
python -i example.py
reverse('')
# ''
reverse('Hexlet')
# 'telxeH'
After making the necessary calls, we add them to the function description:
def reverse(string):
"""Reverse string
>>> reverse('')
''