Writing Tests & Assertions
Manual checking is slow and error-prone. Postman lets you write automated tests that run after every request.
Where Tests Live
In Postman, go to the Tests tab (now called Scripts → Post-response in newer versions).
Tests run automatically after the response is received.
// This code runs after every request
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
---
Your First Test
Testing Status Code
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
- Create a GET request to
https://fakestoreapi.com/products/1 - Go to Tests tab
- Paste the code above
- Click Send
- Check Test Results tab in response - should show ✅ PASS
Multiple Status Codes
pm.test("Successful response", function() {
pm.expect(pm.response.code).to.be.oneOf([200, 201]);
});
---
Common Assertions
Status Code Tests
// Exact status
pm.test("Status is 200 OK", function() {
pm.response.to.have.status(200);
});
// Status name
pm.test("Status is OK", function() {
pm.response.to.have.status("OK");
});
// Created
pm.test("Resource created", function() {
pm.response.to.have.status(201);
});
// Not found
pm.test("Product not found", function() {
pm.response.to.have.status(404);
});
Response Time Tests
// Response under 500ms
pm.test("Response time is acceptable", function() {
pm.expect(pm.response.responseTime).to.be.below(500);
});
// Response under 1 second
pm.test("Response time under 1 second", function() {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
Response Body Tests
// Parse JSON response
const response = pm.response.json();
// Check property exists
pm.test("Response has id", function() {
pm.expect(response).to.have.property("id");
});
// Check property value
pm.test("Product name is correct", function() {
pm.expect(response.title).to.equal("Fjallraven - Foldsack No. 1 Backpack");
});
// Check price is a number
pm.test("Price is a number", function() {
pm.expect(response.price).to.be.a("number");
});
// Check price is positive
pm.test("Price is positive", function() {
pm.expect(response.price).to.be.above(0);
});
---
E-commerce Test Examples
Test: Get Product Details
const product = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Product has required fields", function() {
pm.expect(product).to.have.property("id");
pm.expect(product).to.have.property("title");
pm.expect(product).to.have.property("price");
pm.expect(product).to.have.property("category");
});
pm.test("Price is valid", function() {
pm.expect(product.price).to.be.a("number");
pm.expect(product.price).to.be.above(0);
});
pm.test("Response time is acceptable", function() {
pm.expect(pm.response.responseTime).to.be.below(500);
});
Test: List Products
const products = pm.response.json();
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Response is an array", function() {
pm.expect(products).to.be.an("array");
});
pm.test("Products array is not empty", function() {
pm.expect(products.length).to.be.above(0);
});
pm.test("Each product has required fields", function() {
products.forEach(function(product) {
pm.expect(product).to.have.property("id");
pm.expect(product).to.have.property("title");
pm.expect(product).to.have.property("price");
});
});
Test: Add to Cart
const cart = pm.response.json();
pm.test("Cart created successfully", function() {
pm.response.to.have.status(200);
});
pm.test("Cart has ID", function() {
pm.expect(cart).to.have.property("id");
});
pm.test("Cart contains products", function() {
pm.expect(cart.products).to.be.an("array");
pm.expect(cart.products.length).to.be.above(0);
});
pm.test("Product quantities are correct", function() {
const firstProduct = cart.products[0];
pm.expect(firstProduct.quantity).to.be.above(0);
});
Test: User Login
const response = pm.response.json();
pm.test("Login successful", function() {
pm.response.to.have.status(200);
});
pm.test("Token received", function() {
pm.expect(response).to.have.property("token");
pm.expect(response.token).to.be.a("string");
pm.expect(response.token.length).to.be.above(10);
});
// Save token for later requests
if (response.token) {
pm.environment.set("authToken", response.token);
console.log("Token saved to environment");
}
---
Header Tests
pm.test("Content-Type is JSON", function() {
pm.response.to.have.header("Content-Type");
pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");
});
pm.test("Cache headers present", function() {
pm.response.to.have.header("Cache-Control");
});
---
String Assertions
const product = pm.response.json();
// Contains substring
pm.test("Description mentions wireless", function() {
pm.expect(product.description.toLowerCase()).to.include("wireless");
});
// Starts with
pm.test("ID starts with PROD", function() {
pm.expect(product.id).to.match(/^PROD/);
});
// Email format
pm.test("Email is valid format", function() {
pm.expect(user.email).to.match(/^[\w-]+@[\w-]+\.\w+$/);
});
---
Array Assertions
const products = pm.response.json();
// Array length
pm.test("Returns exactly 5 products", function() {
pm.expect(products).to.have.lengthOf(5);
});
// Array contains item
pm.test("Electronics category exists", function() {
const categories = products.map(p => p.category);
pm.expect(categories).to.include("electronics");
});
// All items match condition
pm.test("All products have price", function() {
const allHavePrice = products.every(p => p.price > 0);
pm.expect(allHavePrice).to.be.true;
});
// At least one matches
pm.test("At least one product under $50", function() {
const hasAffordable = products.some(p => p.price < 50);
pm.expect(hasAffordable).to.be.true;
});
---
Error Response Tests
pm.test("Returns 404 for invalid product", function() {
pm.response.to.have.status(404);
});
pm.test("Error message is descriptive", function() {
const error = pm.response.json();
pm.expect(error).to.have.property("message");
});
---
Using Snippets
Postman has built-in snippets! Click Snippets on the right side of the Tests tab:
- Status code is 200
- Response body: JSON value check
- Response time is less than 200ms
- Response body: Contains string
Click a snippet to insert it automatically.
---
Practice: Test Suite for Products API
Create tests for GET https://fakestoreapi.com/products:
// Response Tests
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Response time is under 1 second", function() {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
// Data structure tests
const products = pm.response.json();
pm.test("Response is an array", function() {
pm.expect(products).to.be.an("array");
});
pm.test("At least 10 products returned", function() {
pm.expect(products.length).to.be.at.least(10);
});
// Data quality tests
pm.test("All products have valid structure", function() {
products.forEach((product, index) => {
pm.expect(product, `Product ${index}`).to.have.property("id");
pm.expect(product, `Product ${index}`).to.have.property("title");
pm.expect(product, `Product ${index}`).to.have.property("price");
pm.expect(product.price, `Product ${index} price`).to.be.a("number");
pm.expect(product.price, `Product ${index} price`).to.be.above(0);
});
});
pm.test("No duplicate IDs", function() {
const ids = products.map(p => p.id);
const uniqueIds = [...new Set(ids)];
pm.expect(ids.length).to.equal(uniqueIds.length);
});
---
Key Takeaways
| What to Test | Code |
|---|---|
| Status code | pm.response.to.have.status(200) |
| Response time | pm.expect(pm.response.responseTime).to.be.below(500) |
| Property exists | pm.expect(obj).to.have.property("key") |
| Value equals | pm.expect(value).to.equal(expected) |
| Type check | pm.expect(value).to.be.a("number") |
| Array length | pm.expect(arr).to.have.lengthOf(5) |
Next up: Environment Variables - managing different environments and reusable data!