Get the FREE Ultimate OpenClaw Setup Guide →

api-testing

npx machina-cli add skill aiqualitylab/agent-skills/api-testing --openclaw
Files (1)
SKILL.md
2.3 KB

API Testing with Postman + Newman

Generate API tests as Postman collections. Run them with Newman CLI.

Setup

npm install -g newman

Test Pattern

Each request gets tests in the Tests tab:

// Assert status code
pm.test("Status is 200", function () {
    pm.response.to.have.status(200);
});

// Assert response body
pm.test("Returns user name", function () {
    pm.expect(pm.response.json().name).to.eql("Test User");
});

// Assert response time
pm.test("Response under 2s", function () {
    pm.expect(pm.response.responseTime).to.be.below(2000);
});

// Validate schema
pm.test("Matches schema", function () {
    pm.response.to.have.jsonSchema({
        type: "object",
        required: ["id", "name", "email"],
        properties: {
            id: { type: "number" },
            name: { type: "string" },
            email: { type: "string" }
        }
    });
});

Pass Data Between Requests

Save a value from one response, use it in the next:

// In POST /users Tests tab — save created ID
const id = pm.response.json().id;
pm.environment.set("created_id", id);

// In GET /users/{{created_id}} — it auto-resolves

Negative Tests

// Missing required field → expect 400 or 422
pm.test("Rejects missing field", function () {
    pm.expect(pm.response.code).to.be.oneOf([400, 422]);
});

// No auth token → expect 401
pm.test("Rejects unauthorized", function () {
    pm.response.to.have.status(401);
});

Run with Newman

# Basic run
newman run collection.json

# With environment
newman run collection.json -e environment.json

# Override variables
newman run collection.json --env-var "base_url=https://staging.example.com"

# Data-driven from CSV
newman run collection.json -d testdata.csv

# CI report
newman run collection.json -r junit --reporter-junit-export report.xml

Guidelines

  • Use environment variables for base_url and secrets, never hardcode.
  • One assertion per pm.test block.
  • Assert status code first, then body.
  • Export collections as v2.1 format.

Source

git clone https://github.com/aiqualitylab/agent-skills/blob/main/skills/api-testing/SKILL.mdView on GitHub

Overview

Generates API tests as Postman collections and runs them with the Newman CLI. It lets you verify endpoints, validate responses, and run Postman tests in CI by reusing familiar Postman workflows.

How This Skill Works

Create tests in Postman and write assertions in the Tests tab (status, body, time, and schema). Run the collection with Newman using commands like newman run collection.json -e environment.json, and you can pass data with -d file or --env-var. Save values between requests into the environment (e.g., store IDs) to reuse in subsequent requests.

When to Use It

  • When you need to verify API endpoints return correct status codes and data
  • When you want to validate responses against a schema using jsonSchema
  • When running tests in CI and generating automated reports with Newman
  • When performing data-driven testing using CSV or other data sources
  • When testing authentication/authorization flows and error handling

Quick Start

  1. Step 1: Install Newman globally with npm install -g newman
  2. Step 2: Create a Postman collection.json and environment.json with your tests
  3. Step 3: Run newman run collection.json -e environment.json and review results (or add -r junit for CI)

Best Practices

  • Use environment variables for base_url and secrets; never hardcode
  • Write one assertion per pm.test block
  • Assert status code first, then body
  • Export collections as v2.1 format
  • Keep tests deterministic and use clear time thresholds for performance checks

Example Use Cases

  • GET /users/{id} returns 200 and matches the user schema
  • POST /users creates a user and returns 201 with an id stored in environment
  • Negative test: missing required field returns 400 or 422
  • Unauthorized access returns 401
  • CI workflow: run collection with -r junit and publish report.xml

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers