Basic API Test Automation Using Postman
Putting It All Together
Basic API automation in Postman combines several features covered earlier: organizing requests into a Collection, parameterizing them with Environment variables, writing pm.test() assertions in each request, and running the whole suite with the Collection Runner.
A Simple Workflow
- Build and save each request needed for a test scenario inside a Collection.
- Add assertions in the Tests tab to check status codes and key response values.
- Parameterize URLs, headers, and body values using Environment variables.
- Run the Collection with the Collection Runner (or the Newman CLI for CI/CD) to get a pass/fail report.
Next Steps
As suites grow, teams often graduate from Postman-based automation to a code-based framework like Rest Assured for more advanced logic, reporting, and CI/CD integration.
Writing Assertions in the Tests Tab
Postman runs JavaScript in the Tests tab (labelled Post-response in newer versions) after each response arrives. A request that creates a user might carry these checks:
pm.test("Status is 201 Created", function () {
pm.response.to.have.status(201);
});
pm.test("Response time is acceptable", function () {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
const body = pm.response.json();
pm.test("User has an id and correct name", function () {
pm.expect(body.id).to.exist;
pm.expect(body.name).to.eql(pm.environment.get("userName"));
});
// save the id for the next request in the chain
pm.environment.set("userId", body.id);
The last line is what turns separate requests into a flow. The next request can use {{userId}} in its URL, for example {{baseUrl}}/users/{{userId}}.
Worked Example: A Create, Read, Delete Chain
- POST Create User sends a body using
{{userName}}, asserts status 201 and savesuserId. - GET Read User calls
{{baseUrl}}/users/{{userId}}and asserts the name matches. - DELETE User removes the record and asserts a success status.
- GET Read Deleted User asserts status 404, proving the delete really worked.
Keep the requests in this order inside the collection, since the Collection Runner runs them top to bottom by default. Pre-request scripts can generate fresh data before a request, such as a unique email using a timestamp, so repeated runs do not clash.
Running the Suite from the Command Line
Export the collection and environment as JSON files, then run them with Newman, Postman's command-line runner:
npm install -g newman
newman run users.postman_collection.json \
-e staging.postman_environment.json \
--iteration-data users.csv \
--reporters cli,junit
The --iteration-data option feeds one row of the CSV file per iteration, which is a quick way to do data-driven testing. The JUnit reporter writes an XML report that most CI servers can display. Postman also offers its own CLI as an alternative; either works in a pipeline.
Practices That Keep Postman Suites Healthy
- Never hard-code URLs or tokens. Use environment variables so the same collection runs on dev, staging and production-like setups.
- Keep secrets out of shared collections. Store tokens as secret or current-value variables and avoid committing them to version control.
- Make each folder independent. A test should create the data it needs rather than rely on leftovers from an earlier run.
- Assert more than the status code. A 200 with the wrong body is still a bug.
- Clean up. Delete test records at the end so the environment stays predictable.
When to switch tools: Postman is excellent for quick suites and for teams with mixed technical skills. When you need complex logic, reusable libraries or tight integration with a Java or Python test stack, a code-based framework becomes easier to maintain.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.