Prompt
How do I test a REST API with collections and assertions?
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
A good way to test a REST API with collections and assertions is to use an API testing tool like Postman (or a similar runner) and write tests that validate both:
- Single response fields
- Arrays / collections of data returned by the API
Here’s a practical approach.
1) Create a request collection
In Postman, organize your API requests into a collection:
GET /usersGET /users/:idPOST /usersPUT /users/:idDELETE /users/:id
Collections help you:
- run requests in order
- reuse variables
- automate regression testing
2) Add assertions to each request
Assertions check whether the response matches expectations.
Example: status code
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Example: response time
pm.test("Response time is under 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
Example: JSON field exists
pm.test("Response has name field", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property("name");
});
3) Test collections / arrays in the response
If the API returns a list, assert things like:
- response is an array
- array is not empty
- each item has required fields
- count is what you expect
Example: validate an array response
pm.test("Response is an array", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.be.an("array");
});
Example: array is not empty
pm.test("Array is not empty", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.length).to.be.greaterThan(0);
});
Example: each item has required fields
pm.test("Each user has id and email", function () {
const users = pm.response.json();
users.forEach(user => {
pm.expect(user).to.have.property("id");
pm.expect(user).to.have.property("email");
});
});
4) Use chained collection assertions
If you want to validate specific values in collections:
Example: check IDs
pm.test("All IDs are numbers", function () {
const users = pm.response.json();
users.forEach(user => {
pm.expect(user.id).to.be.a("number");
});
});
Example: verify one item matches a value
pm.test("Contains admin user", function () {
const users = pm.response.json();
const admin = users.find(u => u.role === "admin");
pm.expect(admin).to.exist;
});
5) Run the whole collection
Use Collection Runner or CI tools to:
- execute all requests
- ensure dependencies between requests
- validate responses repeatedly
You can also pass values from one request to another:
Save an ID from a response
const jsonData = pm.response.json();
pm.environment.set("userId", jsonData.id);
Use it in later requests
GET /users/{{userId}}
6) Recommended test layers
For good API coverage, test:
- Status codes: 200, 201, 400, 401, 404
- Schema/structure: correct JSON shape
- Field validation: required fields, types, formats
- Collections: list size, item properties, uniqueness
- Business rules: e.g. no duplicate emails
- Performance: response time limits
7) Example full test script
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response is JSON array", function () {
const users = pm.response.json();
pm.expect(users).to.be.an("array");
});
pm.test("Every user has id and email", function () {
const users = pm.response.json();
users.forEach(user => {
pm.expect(user).to.have.property("id");
pm.expect(user).to.have.property("email");
});
});
pm.test("No duplicate emails", function () {
const users = pm.response.json();
const emails = users.map(u => u.email);
const uniqueEmails = [...new Set(emails)];
pm.expect(uniqueEmails.length).to.eql(emails.length);
});
8) If you mean assertions in code
If you’re testing with code rather than Postman, use frameworks like:
- JUnit + RestAssured (Java)
- pytest + requests (Python)
- Jest + supertest (Node.js)
Example in Python:
def test_users_list():
response = requests.get("https://api.example.com/users")
assert response.status_code == 200
users = response.json()
assert isinstance(users, list)
assert len(users) > 0
assert all("id" in u and "email" in u for u in users)
If you want, I can show you:
- a Postman collection example,
- Java RestAssured tests, or
- Python pytest API tests.