How to create API tests using Postman
Step 1. Create a collection
Collection is the set of requests which are grouped using specific logic (f.e. by features, different modules, etc.)
To create a collection proceed with the following flow: “New” -> “Collection” -> specify the title for the collection
Each collection could be divided into the separate folders
Step 2. Add a request
Using the “3 dots” menu select “Add request” option. For learning purposes let’s use any open API (f.e. https://api.agify.io/)
Each request contains the following tabs:
- Params — contains parameters for query string
- Authorization — contains necessary authorization data
- Headers-contains particular headers
- Body — contains a set of the values which will be sent in API request
- Pre-request script — set of the manipulation which will be done before sending the request. In some cases, it could be used for token generation or just preconditions
- Tests — this tab is using for tests that cover specific request
- Settings — contains a list of the request configuration
Let’s create a simple GET request using open Agify API (f.e. https://api.agify.io/). It does not require any specific headers or authorization and selected for learning purposes. According to documentation, “name” value is required parameter
To create a GET request:
- Leave default GET method
- Specify “https://api.agify.io/” value within URL field
- Add “name” parameter with any existing name value (f.e. such one — “testName”)
To send a request just click on “Send” button and check the response block
Step 3. Add tests
Step 3.1 Positive tests
Go to “Tests” tab and create a simple check for response status code (it should be 200)
You can use Snippets for quick creation
Send request again and pay attention to the “Test Results” tab within the response block
The other positive tests could be the following:
- Response time is less than 200ms
- The response has a valid structure
- The response has valid data etc.
Step 3.2 Negative tests
As mentioned before Agify API expects “name” parameter. To create simple negative test let’s send GET request without “name” parameter (you can just turn off checkbox for needed parameter). Send the already existing request with cleared (or turned off) “name” parameter to check how existing tests working
Let’s adaptive tests for negative case:
Step 4. Using environments
Environment stores the set of the variables which are using for requests but could have different values on different environments (f.e. develop, staging or preprod). To manage your environment click on “eye” icon at the top left corner
Step 5. Using environment variables
Environment variables store in one place but could be used in any request. the first reason why it necessary to store values as environment variables that there is no need to change variables in every place where they are using when value is changing. Another point is manipulation using these variables — assign new value within test or pre-request script
To use environment variable just put {{name of the variable}} into your request (make sure that correct environment is selected)
To use an environment variable for any manipulations store it from the separate variable using get method
To update environment variable value use set method
Step 6. Runner
To run all existing requests within the specific collection just open the needed collection and click on “Run” button
NOTE: If you updated any request do not forget to save it before starting Runner (Runner uses the latest saved version)
Have a happy API testing!