基于gitlab持续集成Newman执行单元测试

Newman

newman官网介绍,
Newman 是 Postman 推出的一个 nodejs 库,直接来说就是 Postman 的json文件可以在命令行执行的插件。
Newman 可以方便地运行和测试集合,并用之构建接口自动化测试和持续集成。

项目目录

在这里插入图片描述

导出接口集合Json文件

postman中所有接口都可以自定义tests脚本实现断言功能
在这里插入图片描述
导出接口集合
在这里插入图片描述
导出命名为ids4.postman_collection.json
在这里插入图片描述

安装node

https://www.runoob.com/nodejs/nodejs-install-setup.html

安装newman

全局安装

npm install -g newman

安装导出报告模板

用于自动生成测试用例报告

npm install -g newman-reporter-htmlextra

新建run-test.js文件

/*
参考文档
https://github.com/postmanlabs/newman
https://github.com/DannyDainton/newman-reporter-htmlextra#newman-reporter-htmlextra

npm install -g newman-reporter-htmlextra

 */


const newman = require('newman');

newman.run({
    collection: './ids4.postman_collection.json', // Collection URL from a public link or the Postman API can also be used
    reporters: ['cli', 'htmlextra'], // 同时支持cli,htmltextra两种方式生成
    iterationCount: 1,
    reporter: {
        htmlextra: {
            export: './report/report-dashboard.html',  //指定生成报告目录
            template: './template/template-dashboard.hbs' // 以此模板生成报告 
        }
    }
}, function(err, summary){
    if (err) {
        throw err;
    }

    const { total, pending, failed } = summary.run.stats.assertions;
    const { responseAverage, responseMin, responseMax, started, completed } = summary.run.timings;

    if(summary.run.failures && summary.run.failures.length>0){
        console.log("未能通过测试");
        process.exit(1);
    }
    else{
        console.log("测试通过");
    }
});

运行如下命令

node run-test.js

结果如下
在这里插入图片描述
生成报告report-dashboard.html
在这里插入图片描述
总结:

  1. newman已经可以做到将postman所有接口实现自动化单元测试,完全可以集成到gitlab的ci/cd流程
  2. template参数模板地址:https://github.com/DannyDainton/newman-reporter-htmlextra#newman-reporter-htmlextra
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值