如何链接纽曼测试运行?
How to chain newman test runs?
我想链接几个 Newman 系列测试 运行。在我的特殊情况下,第一个测试 运行 需要将一些数据传递给第二个测试 运行。我该怎么做?
在我当前(无效)的解决方案中,我在 newman 使用的全局变量中设置了一个对象,并将该数据传递给第二个测试可访问的变量 运行。我注意到 newman 运行 仅在 运行 完成所有代码后才被触发。我想我可以尝试使用 Promise
让代码等到第一个测试 运行 完成后再进行第二个测试 运行.
这是我目前拥有的:
var output
let run1 = new Promise(function (onResolve, onReject){
let out
let error
newman
.run({ /* options like collection etc go here */ })
.on('done', function (err, summary) {
if(err) {
error = err
}
out = summary.globals.values.members.find(item => /* some query here */).value
})
if(out){
onResolve(out);
} else{
onReject(error);
}
})
run1.then(
//when "onreslove" got triggered
function(value) {
console.log('test run successful')
output = value
},
//when "onReject" got triggered
function(error){
console.log('test run failed with error!\n')
console.log(error)
return
}
)
if (output){
let run2 = new Promise(function(onReslove, onReject) {
let error
let testData = require(/* path to json test data file*/)
//some logic here that adds the data from the output variable to the "testData" object.
newman.run({ /* some options here */})
.on('done', function (err, summary) {
if (err) {
console.log(err)
error = err
}
})
if(!error){
onResolve();
} else{
onReject(error);
}
})
run2.then(
//when onResolve got triggered
function() { console.log(`test run successful`)},
//when onReject got triggered
function(error) {
console.log('test run failed with error!\n')
console.log(error)
return
}
)
} else{
console.log(`Expected output value, received ${output} instead.`)
}
我目前卡住了,out
将永远是 undefined
,因为在我的调试会话达到 if(output){
时“完成”事件尚未触发。
使用 newman 回调功能
根据API Reference,newman.run
接受回调函数。此回调函数可用于启动第二个嵌套 运行.
newman.run(
{ /* test run options */ },
function(err, summary){
console.log('first run finished')
if (err) {
console.log(err)
return
}
let output = summary.globals.values.members.find(/* some array query */).value
let testData = require(/*path to test data json file*/)
//some logic here to add the output to the testData for the next run
newman.run(
{/* test run options */},
function(err, summary){
if (err) {
console.log(err)
return
}
console.log('second run finished')
}
)
})
使用纽曼“完成”事件和承诺
后续的 newman 测试 运行 可以通过进行测试的承诺的 .then
回调链接 运行:
function newmanRun(options) {
return new Promise(function(onResolve){
newman
.run(options)
.on('done', function (err, summary) {
if (err) {
console.log(`\ntest run failed!\n${err}`)
return
}
console.log(`\ntest run succeeded!\n`)
onResolve(summary)
})
})
}
function run1() {
let options = { /* newman test run options */ }
newmanRun(options).then(
//when "onResolve" got triggered
function(summary) {
let output = summary.globals.values.members.find(/*query here*/).value
run2(output)
}
)
}
function run2(input){
let testData = {}//path to test data file
// some logic here to pass the output data from the previous run as input for the next test run.
let options = { /* use your test data variable in the newman options object*/ }
newmanRun(options).then(
//when "onResolve" got triggered
function(summary) {
console.log('test run finished.')
}
)
}
//now we just need to call the run1 function to start the test run chain:
run1()
正在使用 ASYNC/AWAIT
我个人认为这是最干净的方法,因为它不涉及将一个测试 运行 嵌套到另一个测试中。
/**
* Returns a promise to do a newman test run with the provided options. Use the `.then()` method to execute any callback function when the test run completed.
* @param {Object} options See https://github.com/postmanlabs/newman#api-reference for details on the options object.
* @returns {Promise} When the promise is fulfilled, a callback function with newman run summary as argument gets called.
*/
async function newmanRun(options) {
return new Promise(function(onResolve){
newman
.run(options)
.on('done', function (err, summary) {
if (err) {
console.log(`\ntest run failed!${err}`)
return
}
console.log(`\ntest run succeeded!`)
onResolve(summary)
});
})
}
async function run1(data) {
//create the newman options here, e.g. data file, collection to run, etc...
let options = { /* pass your data from the function parameter to the options here */ }
return newmanRun(options) //returns a promise
}
async function run2(data){
//create the newman options here, e.g. data file, collection to run, etc...
let options = { /* pass your data from the function parameter to the options here */ }
return newmanRun(options) //returns a promise
}
async function start(){
//load the test data and start iterating the newman test run chain.
let input = JSON.parse(readFileSync(/** path to yor data file here */))
summary = await run1(/* pass the data from input variable here */)
output = summary.globals.values.members.find(/* query to fetch whatever data returned from the test run */).value
await run2(/* the data from output here as input for the next test run*/)
}
start()
我想链接几个 Newman 系列测试 运行。在我的特殊情况下,第一个测试 运行 需要将一些数据传递给第二个测试 运行。我该怎么做?
在我当前(无效)的解决方案中,我在 newman 使用的全局变量中设置了一个对象,并将该数据传递给第二个测试可访问的变量 运行。我注意到 newman 运行 仅在 运行 完成所有代码后才被触发。我想我可以尝试使用 Promise
让代码等到第一个测试 运行 完成后再进行第二个测试 运行.
这是我目前拥有的:
var output
let run1 = new Promise(function (onResolve, onReject){
let out
let error
newman
.run({ /* options like collection etc go here */ })
.on('done', function (err, summary) {
if(err) {
error = err
}
out = summary.globals.values.members.find(item => /* some query here */).value
})
if(out){
onResolve(out);
} else{
onReject(error);
}
})
run1.then(
//when "onreslove" got triggered
function(value) {
console.log('test run successful')
output = value
},
//when "onReject" got triggered
function(error){
console.log('test run failed with error!\n')
console.log(error)
return
}
)
if (output){
let run2 = new Promise(function(onReslove, onReject) {
let error
let testData = require(/* path to json test data file*/)
//some logic here that adds the data from the output variable to the "testData" object.
newman.run({ /* some options here */})
.on('done', function (err, summary) {
if (err) {
console.log(err)
error = err
}
})
if(!error){
onResolve();
} else{
onReject(error);
}
})
run2.then(
//when onResolve got triggered
function() { console.log(`test run successful`)},
//when onReject got triggered
function(error) {
console.log('test run failed with error!\n')
console.log(error)
return
}
)
} else{
console.log(`Expected output value, received ${output} instead.`)
}
我目前卡住了,out
将永远是 undefined
,因为在我的调试会话达到 if(output){
时“完成”事件尚未触发。
使用 newman 回调功能
根据API Reference,newman.run
接受回调函数。此回调函数可用于启动第二个嵌套 运行.
newman.run(
{ /* test run options */ },
function(err, summary){
console.log('first run finished')
if (err) {
console.log(err)
return
}
let output = summary.globals.values.members.find(/* some array query */).value
let testData = require(/*path to test data json file*/)
//some logic here to add the output to the testData for the next run
newman.run(
{/* test run options */},
function(err, summary){
if (err) {
console.log(err)
return
}
console.log('second run finished')
}
)
})
使用纽曼“完成”事件和承诺
后续的 newman 测试 运行 可以通过进行测试的承诺的 .then
回调链接 运行:
function newmanRun(options) {
return new Promise(function(onResolve){
newman
.run(options)
.on('done', function (err, summary) {
if (err) {
console.log(`\ntest run failed!\n${err}`)
return
}
console.log(`\ntest run succeeded!\n`)
onResolve(summary)
})
})
}
function run1() {
let options = { /* newman test run options */ }
newmanRun(options).then(
//when "onResolve" got triggered
function(summary) {
let output = summary.globals.values.members.find(/*query here*/).value
run2(output)
}
)
}
function run2(input){
let testData = {}//path to test data file
// some logic here to pass the output data from the previous run as input for the next test run.
let options = { /* use your test data variable in the newman options object*/ }
newmanRun(options).then(
//when "onResolve" got triggered
function(summary) {
console.log('test run finished.')
}
)
}
//now we just need to call the run1 function to start the test run chain:
run1()
正在使用 ASYNC/AWAIT
我个人认为这是最干净的方法,因为它不涉及将一个测试 运行 嵌套到另一个测试中。
/**
* Returns a promise to do a newman test run with the provided options. Use the `.then()` method to execute any callback function when the test run completed.
* @param {Object} options See https://github.com/postmanlabs/newman#api-reference for details on the options object.
* @returns {Promise} When the promise is fulfilled, a callback function with newman run summary as argument gets called.
*/
async function newmanRun(options) {
return new Promise(function(onResolve){
newman
.run(options)
.on('done', function (err, summary) {
if (err) {
console.log(`\ntest run failed!${err}`)
return
}
console.log(`\ntest run succeeded!`)
onResolve(summary)
});
})
}
async function run1(data) {
//create the newman options here, e.g. data file, collection to run, etc...
let options = { /* pass your data from the function parameter to the options here */ }
return newmanRun(options) //returns a promise
}
async function run2(data){
//create the newman options here, e.g. data file, collection to run, etc...
let options = { /* pass your data from the function parameter to the options here */ }
return newmanRun(options) //returns a promise
}
async function start(){
//load the test data and start iterating the newman test run chain.
let input = JSON.parse(readFileSync(/** path to yor data file here */))
summary = await run1(/* pass the data from input variable here */)
output = summary.globals.values.members.find(/* query to fetch whatever data returned from the test run */).value
await run2(/* the data from output here as input for the next test run*/)
}
start()