13.3 Unit testing with pytest
We can create unit tests using a library that provides a common framework for the test scenarios, along with a test runner to execute the tests and log results. Unit tests focus on testing the least amount of code possible in any one test. One of the more popular alternatives to the standard library unittest is the pytest package. This has the advantage of letting us write smaller and clearer test cases. The lack of overhead makes this a desirable alternative.
Since pytest is not part of the standard library, you’ll need to download and install it. You can get it from the pytest home page at https://docs.pytest.org/en/stable/. You can install it with any of the installers or environment managers.
In a Terminal window, activate the virtual environment for your current project. (If you’re using venv, for example, you might use source .venv/bin/activate.) Then, use an OS command like the following: