Prompt

How do I test a REST API with collections and assertions?

Technology / Testing & QA1 observationLast seen Oct 6, 2026

Latest observation

Oct 6, 2026OpenAI 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:

  1. Single response fields
  2. 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 /users
  • GET /users/:id
  • POST /users
  • PUT /users/:id
  • DELETE /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:

  1. a Postman collection example,
  2. Java RestAssured tests, or
  3. Python pytest API tests.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.