By: Carlos Martínez
Introduction
Postman speeds up development and testing by evaluating APIs through “Collections.” These collections can be set up for automated testing, including the automatic generation of integration tests. This ensures our service remains intact when new changes are made during development.
In this example, we’ll show a simple way to create integration tests using PostBot AI, a tool in Postman that uses ChatGPT to generate tests automatically. We’ll use FastAPI, a high-performance web framework for building APIs with Python, to create a basic Pokemon API with CRUD operations. Then, we’ll run tests with PostBot AI to check if future changes might disrupt our data flow.
Defining Endpoints
We’ll set up routes to manage CRUD operations for the Pokemon API, following this structure:
- Create Pokemon:
Method: POST
Path: /pokemons/
Payload: { "name": <string>, "type": <string>, "abilities": [ <string>, …, <string> ] }
Response: { "name": <string>, "type": <string>, "abilities": [ <string>, …, <string> ] }
Status Code: 201
- Get Pokemon:
Method: GET
Path: /pokemons/{name}/
Payload: None
Response: { "name": <string>, "type": <string>, "abilities": [ <string>, …, <string> ] }
Status Code: 200
- Get All Pokemon:
Method: GET
Path: /pokemons/{name}/
Payload: None
Response: { "name": <string>, "type": <string>, "abilities": [ <string>, …, <string> ] }
Status Code: 200
- Update Pokemon:
Method: PATCH
Path: /pokemons/{name}/
Payload: { "type": <string>, "abilities": [ <string>, …, <string> ] }
Response: { "name": <string>, "type": <string>, "abilities": [ <string>, …, <string> ] }
Status Code: 200
- Delete Pokemon:
Method: DELETE
Path: /pokemons/{name}/
Payload: None
Response: None
Status Code: 204
Setting Up the Project
We will start by installing FastAPI and Uvicorn, for this example we will use Python 3.10:
pip install fastapi uvicorn
Creating the Code
Create a file named `main.py`. In this file, we’ll set up a FastAPI instance and our in-memory “database,” represented as a global variable list. Afterward, we’ll add our initial endpoint to create a Pokemon.
These would be our GET endpoints:
Now lets add the PATCH endpoint:
And our DELETE endpoint:
This is how our complete code should look like at this point:
Adding a Collection with Postman.
We will add a new Collection by clicking on the “New” button.
And by clicking on the “Collection” button:
We will name this collection “Pokemon.”
Now we can start adding our requests. This would be the Create Pokemon endpoint:
Our GET endpoint for a single Pokemon:
Our GET endpoint to retrieve all Pokemon:
Our PATCH endpoint to update a given Pokemon:
And our DELETE endpoint to remove an existing Pokemon:
Adding Integration Tests with PostBot AI.
We can auto-generate tests by using PostBot AI to speed up development. Let’s start by selecting our new Collection and clicking the “PostBot” button at the bottom:
In the popup window, we can type our instructions or click on the suggested option. Let’s click on the “Generate tests” button.
We will see a window like this one:
Click on the “Generate Tests” button and the magic starts:
Neat! PostBot auto-generated those tests, and if we click on the “Save Tests” button we will be able to edit them later. Once saved, if we click on the “Tests” tab, we will see our newly added tests per endpoint.
Issues to consider
If you followed this tutorial, you might have noticed an error shown for the DELETE request:
As of today, PostBot is still in Beta so in some cases it has unexpected behaviors. In this case, we are expecting a response to be returned from the request, but for 204 status codes we do not need any response back, so our empty response is correct but PostBot fails to generate our scripts when it does not see a response back. Let’s fix that.
Go to our DELETE request, and click on the “Tests” tab:
At the Snippets menu to the right, we will see an option called “Status Code: Code is 200”. Click on it:
Our base test is now added. Now we just need to change the status code to 204:
Save the change, and click again on the PostBot to generate again our tests:
We will see our test running properly this time.
Conclusion
FastAPI’s simplicity and performance make it an ideal choice for building APIs. In a few steps, we crafted a Pokemon API with CRUD operations. Integrating Postman and PostBot AI accelerates the testing process, ensuring the reliability of your API. As you embark on your API development journey, leverage these tools to streamline your workflow and build robust, scalable applications.