| name | api-testing |
| description | Generate and run REST API tests using Postman collections and Newman CLI. Use when asked to test APIs, verify endpoints, validate responses, or run Postman tests in CI. |
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:
pm.test("Status is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Returns user name", function () {
pm.expect(pm.response.json().name).to.eql("Test User");
});
pm.test("Response under 2s", function () {
pm.expect(pm.response.responseTime).to.be.below(2000);
});
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:
const id = pm.response.json().id;
pm.environment.set("created_id", id);
Negative Tests
pm.test("Rejects missing field", function () {
pm.expect(pm.response.code).to.be.oneOf([400, 422]);
});
pm.test("Rejects unauthorized", function () {
pm.response.to.have.status(401);
});
Run with Newman
newman run collection.json
newman run collection.json -e environment.json
newman run collection.json --env-var "base_url=https://staging.example.com"
newman run collection.json -d testdata.csv
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.