0
0
Postmantesting~15 mins

Running a collection in Postman - Build an Automation Script

Choose your learning style9 modes available
Run a Postman collection and verify all requests succeed
Preconditions (2)
Step 1: Open Postman application
Step 2: Select the 'User API Tests' collection from the left sidebar
Step 3: Click the 'Run' button to open the Collection Runner
Step 4: In the Collection Runner, ensure all requests are selected
Step 5: Click the 'Start Run' button to execute the collection
Step 6: Wait for all requests to complete
Step 7: Verify that each request shows a green checkmark indicating success
✅ Expected Result: All requests in the 'User API Tests' collection run successfully with status code 200 and show green checkmarks in the Collection Runner
Automation Requirements - newman
Assertions Needed:
Verify each request response status code is 200
Verify the collection run completes without errors
Best Practices:
Use explicit checks for response status codes
Handle asynchronous execution properly
Use descriptive test names for clarity
Automated Solution
Postman
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.

Common Mistakes - 3 Pitfalls
Not handling asynchronous callback properly
Not checking the failures array in the summary
Hardcoding collection path without relative or absolute path
Bonus Challenge

Now add data-driven testing by running the collection with 3 different environment files

Show Hint