如何链接纽曼测试运行?

时间:2021-05-27 09:41:40

标签: javascript node.js es6-promise newman

我想链接几个 Newman 集合测试运行。在我的特殊情况下,第一次测试运行需要将一些数据传递给第二次测试运行。我该怎么办?

在我当前(不工作)的解决方案中,我在 newman 使用的全局变量中设置了一个对象,并将该数据传递给第二次测试运行可访问的变量。我注意到 newman run 只有在运行完所有代码后才被触发。我想我可以尝试使用 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){ 时“done”事件尚未触发。

1 个答案:

答案 0 :(得分:1)

使用纽曼回调功能

根据API Referencenewman.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 测试运行可以通过执行测试运行的 Promise 的 .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()

使用异步/等待

我个人认为这是最干净的方法,因为它不涉及将一个测试运行嵌套到另一个中。

/**
 * 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()