Unit testing with pytest
Unit testing is a testing procedure where individual components of an application are tested. This form of testing enables us to verify the working capability of individual components. For example, unit tests are employed in testing individual routes in an application to ensure the proper responses are returned.
In this chapter, we’ll be making use of
pytest, a Python testing library, to conduct our unit testing operations. Although Python comes with a unit testing library called
unittest built in, the
pytest library has a shorter syntax and is more preferred for testing applications. Let’s install
pytest and write our first sample test.
Let’s install the
(venv)$ pip install pytest
Next, create a folder called
tests that will house the test files for our application:
(venv)$ mkdir tests && cd (venv)$ touch __init__.py
Individual test filenames, during creation, will be prefixed with