Postman’s "Tests" tab is where you write JavaScript code to validate API responses, effectively turning your API client into a lightweight testing framework.
Here’s a typical request in Postman with a test script:
{
"info": {
"_postman_id": "...",
"name": "GET Users Test",
"description": "Tests for the /users endpoint",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Get All Users",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://api.example.com/users",
"protocol": "https",
"host": [
"api",
"example",
"com"
],
"path": [
"users"
]
},
"description": "Retrieves a list of all users."
},
"response": [
{
"name": "Successful Response",
"originalRequest": {
"method": "GET",
"header": [],
"url": {
"raw": "https://api.example.com/users",
"protocol": "https",
"host": [
"api",
"example",
"com"
],
"path": [
"users"
]
}
},
"status": "200 OK",
"code": 200,
"name": "Successful Response",
"response": "[\n {\n \"id\": 1,\n \"name\": \"Alice\",\n \"email\": \"alice@example.com\"\n },\n {\n \"id\": 2,\n \"name\": \"Bob\",\n \"email\": \"bob@example.com\"\n }\n]",
"tests": "pm.test(\"Status code is 200\", function () {\n pm.response.to.have.status(200);\n});\n\npm.test(\"Response body is an array\", function () {\n var jsonData = pm.response.json();\n pm.expect(jsonData).to.be.an('array');\n});\n\npm.test(\"First user has correct properties\", function () {\n var jsonData = pm.response.json();\n pm.expect(jsonData[0]).to.have.property('id');\n pm.expect(jsonData[0]).to.have.property('name');\n pm.expect(jsonData[0]).to.have.property('email');\n});\n\npm.test(\"First user's name is Alice\", function () {\n var jsonData = pm.response.json();\n pm.expect(jsonData[0].name).to.eql('Alice');\n});\n\npm.test(\"Response contains Bob\", function () {\n var jsonData = pm.response.json();\n var bob = jsonData.find(user => user.name === 'Bob');\n pm.expect(bob).to.exist;\n pm.expect(bob.email).to.eql('bob@example.com');\n});"
}
]
}
]
}
The response[0].tests field above contains the JavaScript code. When you send the GET All Users request, Postman executes this code against the received response.
You’re essentially writing assertions using the pm.test() function. Each pm.test() block defines a single test case with a descriptive name. Inside, you use pm.expect() to make assertions about the response.
The pm.response object is your gateway to the API response. You can access its properties like:
pm.response.code: The HTTP status code (e.g.,200,404).pm.response.headers: An object containing response headers.pm.response.text(): The response body as plain text.pm.response.json(): The response body parsed as a JavaScript object (if theContent-Typeisapplication/json).
Common assertions you’ll make:
- Status Code:
pm.expect(pm.response.code).to.equal(200); - Response Body Type:
pm.expect(pm.response.json()).to.be.an('array'); - Property Existence:
pm.expect(jsonData).to.have.property('userId'); - Property Value:
pm.expect(jsonData.userId).to.eql(123); - Array Length:
pm.expect(jsonData).to.have.lengthOf(5); - String Containment:
pm.expect(pm.response.text()).to.include('success');
You can also chain assertions for more complex checks. For instance, to check if a specific user object exists within an array and has certain properties:
pm.test("User with ID 1 exists and has correct name", function () {
var jsonData = pm.response.json();
var user = jsonData.find(item => item.id === 1);
pm.expect(user).to.exist;
pm.expect(user.name).to.eql("Alice");
});
This code first parses the JSON response, then uses the find array method to locate a user with id equal to 1. Finally, it asserts that such a user was found (.to.exist) and that their name property is exactly "Alice" (.to.eql("Alice")).
One of the most powerful, yet often overlooked, aspects is how Postman’s test scripts can influence subsequent requests. By using pm.environment.set("variableName", value) or pm.collectionVariables.set("variableName", value), you can capture data from a response and use it as input for later requests in your collection. For example, after creating a user and receiving their ID in the response, you can save that ID to an environment variable and then use {{userId}} in the URL of a subsequent "GET User by ID" request.
The next hurdle is managing and organizing these tests as your API grows, often leading to the need for more sophisticated test structuring and data-driven testing.