Writing Python tests
In this last section of this chapter, we are going to learn about Python testing and write three tests for our code with the help of the pytest
package.
As the pytest
package is not installed by default, the first task you should carry out is installing it using your favorite method. Part of the pytest
package is the pytest
command-line utility, which is used for running the tests.
Unit testing
In this section, we are writing unit tests, which are usually functions that we write to make sure that our code works as expected. The result of a unit test is either PASS
or FAIL
. The more extensive the unit testing is, the more useful it is.
After a successful installation, if you execute the pytest
command on a directory that does not contain any valid tests, you are going to get information about your system and your Python installation. On a macOS machine, the output is the following:
$ pytest ========================= test session starts ==========...