4

使用集合运行器(或 newman)时,您可以指定要执行的迭代次数。迭代全部按顺序执行。工具中有没有办法将测试/迭代配置为并行运行?我已经使用 Newman 在节点脚本中通过一个简单的循环来完成此操作,但是结果都被相互覆盖了。

4

1 回答 1

4

到目前为止,我发现这样做的唯一方法是编写自定义节点代码来启动多个newman.run进程,然后聚合这些进程返回的所有结果。

这是一个例子:

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);
  });
}
于 2017-03-27T09:58:02.583 回答