Member-only story

How to Test an API with Pytest and Requests

Travis Luong
2 min readDec 24, 2021

--

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

  1. Full Stack Next.js, FastAPI, PostgreSQL Tutorial
  2. How to Build a Full Stack Next.js, FastAPI, PostgreSQL Boilerplate Tutorial
  3. How to Deploy Next.js, FastAPI, and PostgreSQL with Shell Scripts
  4. How to Develop a Full Stack Next.js, FastAPI, PostgreSQL App Using Docker
  5. How to Build a User Authentication Flow with Next.js, FastAPI, and PostgreSQL
  6. 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

--

--

No responses yet