【问题标题】:How to make a node script of Newman fail when a test fails测试失败时如何使 Newman 的节点脚本失败
【发布时间】:2018-10-24 02:03:04
【问题描述】:

我有一个可以运行的 newman 脚本:

                var fs = require('fs'),
                newman = require('newman'),

                results = [];

            newman.run({
                reporters: 'cli',
                collection: require('.temp.json'),
                iterationData: './data.jsp',
                reporters: ['cli', 'html'],
                reporter: {
                    html: {
                        export: './newman/htmlResults.html', // If not specified, the file will be written to `newman/` in the current working directory.
                    }
                }
            })
            .on('request', function (err, args) {
                if (!err) {
                    // here, args.response represents the entire response object
                    var rawBody = args.response.stream, // this is a buffer
                    body = rawBody.toString(); // stringified JSON

                    results.push(JSON.parse(body)); // this is just to aggregate all responses into one object
                }
            })
            // a second argument is also passed to this handler, if more details are needed.
            .on('done', function (err, summary) {
                // write the details to any file of your choice. The format may vary depending on your use case
                fs.writeFileSync('migration-report.json', JSON.stringify(results, null, 4));
                if(summary.run.failures.length !== 0){
                    console.log("\\rThere are test failures")
                    //here I can exit but the Newman summary isn't showed
                }
            });

此脚本有效,但即使测试失败,它也成功完成。显示了测试失败,但我想以退出代码 1 之类的内容结束脚本,因为我将在 Jenkins 中运行它,并且我不想根据 buildresult 颜色检查测试是否有效。如何在节点中执行此操作?

【问题讨论】:

    标签: node.js jenkins newman


    【解决方案1】:

    将断言“Chai”库添加到您的测试中,并使用它的方法来断言您的测试结果 阅读更多: https://www.chaijs.com/ https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference

    const chai = require('chai')
     expect = chai.expect;
    

    并根据您要断言的内容(statusCode、reposneBody 等)进行断言 例如:断言您希望您的响应正文将包含一个包含 3 个对象的数组

    .on('request', function (err, args) {
                    if (!err) {
                        // here, args.response represents the entire response object
                        var rawBody = args.response.stream, // this is a buffer
                        body = rawBody.toString(); // stringified JSON
    
                        results.push(JSON.parse(body)); // this is just to aggregate all responses into one object
                        expect(body.array.length === 3).to.equal(true)// **EXAMPLE**
                    }
                })
    

    pm.expect(res).to.have.property('code', 200);
    pm.expect(res).to.have.property('status', 'OK');
    

    两者都检查您的请求响应的状态代码。

    阅读:https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 2016-04-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      相关资源
      最近更新 更多