Run a Postman collection and verify all requests succeed
Preconditions (2)
✅ Expected Result: All requests in the 'User API Tests' collection run successfully with status code 200 and show green checkmarks in the Collection Runner
Jump into concepts and practice - no test required
import newman from 'newman'; newman.run({ collection: require('./User_API_Tests.postman_collection.json'), reporters: 'cli' }, function (err, summary) { if (err) { console.error('Collection run encountered an error:', err); process.exit(1); } const failures = summary.run.failures; if (failures.length > 0) { console.error(`Test failed: ${failures.length} request(s) did not pass.`); failures.forEach(failure => { console.error(`Request: ${failure.source.name}`); console.error(`Error: ${failure.error.test}`); }); process.exit(1); } else { console.log('All requests passed successfully.'); } });
This script uses Newman, the command-line tool for running Postman collections.
We import Newman and call newman.run() with the collection file path.
The reporters: 'cli' option shows results in the console.
In the callback, we check for errors running the collection.
Then we check summary.run.failures array for any failed tests.
If there are failures, we print details and exit with error code.
If no failures, we print success message.
This ensures the automation verifies all requests passed with status 200 as defined in the collection tests.
Now add data-driven testing by running the collection with 3 different environment files
Running a collection in Postman?