简体   繁体   English

测试失败时如何使Newman的节点脚本失败

[英]How to make a node script of Newman fail when a test fails

I have a newman script which works: 我有一个可以工作的纽曼脚本:

                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
                }
            });

This script works but even when there are test failures it finished successfully. 该脚本可以工作,但是即使有测试失败,它也可以成功完成。 The test failures are showed but I want to end the script with something like exit code 1 because I will run this in Jenkins and I wan't to check if tests work or not depending on the buildresult colour. 显示了测试失败,但我想以退出代码1之类的脚本结束脚本,因为我将在Jenkins中运行此脚本,并且我不会根据buildresult颜色检查测试是否有效。 How can I do this in node? 如何在节点中执行此操作?

Add assertion "Chai" library to your tests and use its methods to assert the result of your tests read more: https://www.chaijs.com/ https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference 将断言“ Chai”库添加到测试中,并使用其方法断言测试结果,以了解更多信息: https : //www.chaijs.com/ https://www.getpostman.com/docs/v6/postman/scripts / postman_sandbox_api_reference

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

and put an assertion depending on what you want to assert (statusCode, reposneBody etc) For Instance: assert that you expect your response body will have an array with 3 objects inside 并根据您要声明的内容(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**
                }
            })

or 要么

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

both check status code of the response on your request. 两者都会根据您的请求检查响应的状态码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何链接纽曼测试运行? - How to chain newman test runs? 从节点 js 运行 newman 时,是否可以创建或更新 postman 测试脚本或变量? - Is it possible to create or update postman test scripts or variables when running newman from node js? 使用 node 运行 newman (Postman) 集合时如何处理异步调用? - How do I handle asynchronous calls when running newman (Postman) collections with node? Postman Newman如何在使用node.js时使用“选项”进行配置 - Postman Newman how to configure “options” using when using node.js TDD原理,如何使测试失败 - TDD principle, how to make the test fail 在同一网络上的不同设备上预览 Angular 应用程序时,如何向 Node.js 服务器发出请求不会失败? - How to make a request to Node.js server not fail when previewing Angular app on a different device on the same network? 通过Newman运行Postman脚本时出现错误 - Getting error when running Postman Script via Newman 如何在Promise承诺的范围内使Node单元测试失败? - How do I fail a Node unit test on the catch of a Promise? 如何用玩笑测试线性节点脚本? - how to test a linear node script with jest? 在 Newman 脚本中访问环境变量 - Accessing environment variable in Newman script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM