Using Pytest

Learn how to use pytest.

Getting started

Pytest is an excellent alternative to the built-in unittest library. It automatically finds our tests and runs them. It makes it easy to do the setup for the tests and break them down afterward, which is important since we have to make it work with Flask applications.

To get started, install pytest using pipenv.

$ pipenv install --dev pytest

Note: It’s already installed here.

Within the pipenv shell, we can test whether or not the installation worked by running pytest --help to see the help message or just pytest to run any tests it finds. So far, no tests have been defined, so it will find nothing. In the flask-examples directory, create a file some_test.py. Pytest will automatically discover any file starting with test or ending in test.py. Next, put a simple test in it.

Get hands-on with 1200+ tech skills courses.