Member-only story
How to Test an API with Pytest and Requests
In previous tutorials, we learned how to build a Full Stack Application with Next.js, FastAPI, and PostgreSQL. In this tutorial, we will learn how to test it with Pytest and Requests.
Table of Contents
- Full Stack Next.js, FastAPI, PostgreSQL Tutorial
- How to Build a Full Stack Next.js, FastAPI, PostgreSQL Boilerplate Tutorial
- How to Deploy Next.js, FastAPI, and PostgreSQL with Shell Scripts
- How to Develop a Full Stack Next.js, FastAPI, PostgreSQL App Using Docker
- How to Build a User Authentication Flow with Next.js, FastAPI, and PostgreSQL
- How to Test an API with Pytest and Requests
Writing tests for backend APIs has major benefits. One, they can be used while developing, so instead of manually using a GUI HTTP client to test your API, you can automate it with code. This is helpful when you find yourself repeatedly running a series of requests to test a specific workflow such as user sign up and log in. Second, they can be used to verify the functionality of your API and make sure it is working as expected.
Create nfp-test
directory.
$ mkdir nfp-test
Create virtualenv.
$ python -m venv venv
$ . venv/bin/activate
Install pytest and requests.
$ pip install pytest requests python-dotenv