robjwilkins
robjwilkins

Reputation: 5652

Run Postman (or Newman) collection runner iterations in parallel

When using the collection runner (or newman) you can specify the number of iterations to perform. The iterations all execute sequentially. Is there a way within the tool to configure tests/iterations to run in parallel? I have done this with a simple loop in a node script using Newman, but then the results are all written over one another.

Upvotes: 4

Views: 6941

Answers (1)

robjwilkins
robjwilkins

Reputation: 5652

The only way I have found to do this so far is to write custom node code to kick off multiple newman.run processes, and then aggregate all the results which those processes return.

Here is an example:

const
  newman = require('newman');
  config = require('./postman-config.js').CONFIG,
  collectionPath = 'postman-collection.json',
  iterationCount = 5,
  threadCount = 5,
  after = require('lodash').after;

exports.test = function() {
  // Use lodash.after to wait till all threads complete before aggregating the results
  let finished = after(threadCount, processResults);
  let summaries = [];
  console.log(`Running test collection: ${collectionPath}`);
  console.log(`Running ${threadCount} threads`);
  for (let i = 0; i < threadCount; i++) {
    testThread(summaries, finished, collectionPath);
  }

};

function processResults(summaries) {
  let sections = ['iterations', 'items', 'scripts', 'prerequests', 'requests', 'tests', 'assertions', 'testScripts', 'prerequestScripts'];
  let totals = summaries[0].run.stats;
  for (let i = 1; i < threadCount; i++) {
    let summary = summaries[i].run.stats;
    for (let j = 0; j < sections.length; j++) {
      let section = sections[j];
      totals[section].total += summary[section].total;
      totals[section].pending += summary[section].pending;
      totals[section].failed += summary[section].failed;
    }
  }
  console.log(`Run results: ${JSON.stringify(totals, null, 2)}`);
}

function testThread(summaries, callback, collectionPath) {
  console.log(`Running ${iterationCount} iterations`);
  newman.run({
    iterationCount: iterationCount,
    environment: config,
    collection: require(collectionPath),
    reporters: []
  }, function (err, summary) {
    if (err) {
      throw err;
    }
    console.log('collection run complete');
    summaries.push(summary);
    callback(summaries);
  });
}

Upvotes: 4

Related Questions