Python: Automated testing
Theory: Testing through documentation
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:
After making the necessary calls, we add them to the function description:
If we pass this file to the interpreter, we'll see a report on the tests performed:
It is how documentation can simultaneously act as tests, which is convenient and practical. We can do the same not only in a specific function but also at the module level: