postman 详细使用 (6):自动化测试 API newman 的基本使用

版权声明:转载随意,附上转载信息即可 :) https://blog.csdn.net/Al_assad/article/details/81370215

postman 自动化测试 API newman 的基本使用

postman 开放了应用程序中常用的 API -- newman 供外部程序调用,newman 基于 node.js ,除了可以直接在命令行中直接运行,还支持作为 module 在 node 代码中调用,newman 项目地址:https://www.npmjs.com/package/newman

newman 可以直接调用 postman 导出的 collection json 文件,也可以自己手动编写 collection json 文件,或者在 node newman 模块中通过对象的方式设定,同时 newman 对于 collection 执行的各个时间点提供了 start、beforeItem、beforePreerquest、beforeRequest、beforeTest、done 等回调函数,可以在 node 程序中十分方便地对 collection 的执行生命周期进行干预;

# 通过 npm 下载 newman
npm install -g newman
# 直接运行 postman collection
newman run ./demo-collection.json

以下以一个简单的示例,来演示 node 中 newman api 的使用,详细的使用说明请参考官方文档:https://www.npmjs.com/package/newman

测试场景描述

假设我需要对接口 http://localhost:8080/project/getHolidayByTimes.do 接口进行测试,该接口返回请求参数 startTime,stopTime 之间的假期时间,响应的 json 类似如下:

我的测试数据储存在本地数据库 postman_test 的数据表 holiday_case 中,如下:

示例代码

代码结构:

我的 collection 已经事先在 postman 中编辑好导出了,collection 只包含一个 item,item 中包含断言脚本;

getHolidayByTimes-test-collection.json

{
    "info": {
        "_postman_id": "fd24328a-4dd3-4d65-985f-40cb44c44341",
        "name": "getHolidayByTimes-test",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [{
        "_postman_id": "eee4e430-0b0e-47de-8574-a4d763202ff9",
        "name": "getHolidayByTimes.do",
        "event": [{
            "listen": "test",
            "script": {
                "id": "0d80d11b-7920-47b6-88b8-6770c38bd060",
                "type": "text/javascript",
                "exec": [
                    "var jsonData = JSON.parse(responseBody)",
                    "",
                    "var result = jsonData.code == data.code ;",
                    "if(data.code == \"0\"){",
                    "    result = result && (jsonData.data.holidays == data.assertTime)",
                    "}",
                    "",
                    "tests[data.comment] = result;   ",
                    "",
                    ""
                ]
            }
        }],
        "request": {
            "method": "POST",
            "body": {
                "mode": "urlencoded",
                "urlencoded": [{
                        "key": "startTime",
                        "value": "{{startTime}}",
                        "type": "text"
                    },
                    {
                        "key": "stopTime",
                        "value": "{{stopTime}}",
                        "type": "text"
                    }
                ]
            },
            "url": "{{host}}/getHolidayByTimes.do"
        },
        "response": []
    }]
}

node 脚本代码:

testGetHolidayByTimes.js

/**
 * 测试 newman API 
 * @author yulinying 2018/8/2
 * @description 测试 wxqyh-ask-PortalAskCtl # getHolidayByTimes 接口,从本地数据库中读取测试数据;
 */

var newman = require("newman");
var mysql = require("mysql");

//从本地数据库中获取测试数据
var connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "mysql1994assad",
    database: "postman_test"
});
connection.connect();
connection.query("select startTime,stopTime,code,assertTime,comment from holiday_case", function(error, results) {
    if (error) throw error;
    var data = convertResultToData(results);
    //运行 collectin 测试
    newman.run({
        collection: require("./getHolidayByTimes-test-collection.json"),
        reporters: ["cli", "html"],
        iterationData: data,
        environment: {
            "values": [{
                "key": "host",
                "value": "http://localhost:8080/holyshit",
                "type": "text",
                "enabled": true
            }]
        }
    });
});
connection.end();

//转化 node-mysql results 为 postman collection 迭代数据格式
function convertResultToData(results) {
    var data = new Array();
    for (i = 0; i < results.length; i++) {
        var row = results[i];
        var dataRow = new Object();
        dataRow.startTime = row.startTime;
        dataRow.stopTime = row.stopTime;
        dataRow.code = row.code;
        dataRow.assertTime = row.assertTime;
        dataRow.comment = row.comment;
        data.push(dataRow);
    }
    return data;
}

 

运行结果

我在代码中设置了 newman 的报告方式为控制台和html,他们的结果分别如下,可以看到,newman 的测试报告方式还是十分舒服的,已经很接近 postman 的 Runner 测试界面,可以十分清楚看到测试用例通过和不通过的数量报告;

控制台:

html 页面:

猜你喜欢

转载自blog.csdn.net/Al_assad/article/details/81370215