postman断言篇

目录

一、postman提供断言方法

二、自定义的编写的断言方法

三、Pre-request Script(前置请求脚本)

四、多接口运行

五、案例:批量运行时,一个接口需要用到另一个接口的数据时怎么实现?


一、postman提供断言方法

方法名

注解

Get an environment variable

获取一个环境变量(只会再活动的环境变量中搜索)

Get a global variable

获取一个全局变量

Get a variable

获取一个环境变量(全局变量和活动的环境变量中搜索)

Get a collection variable

获取一个集合变量

Set an environment variable

设置一个环境变量

Set a global variable

设置一个全局变量

Set a collection variable

设置一个集合变量

Clear an environment varlable

清除一个环境变量

Clear a global variable

清除一个全局变量

Clear a collection varlable

清除一个集合变量

Send a request

发送一个请求

Status code: Code is 200

检查状态码为200

Response body: Contains string

响应结果:包含指定字符串

Response body: JSON value check

响应结果:json检查

Response body: Is equal to a string

响应结果:等于指定字符串

Response headers: Content-Type header check

header报文检查

Response time is less than 200ms

响应时间小于200ms

Status code: SuccessfulPOST request

post请求成功的状态码

Status code: Code name has string

检查状态码名字(OK)

Response body: Converl XML body to a JSON Object

将xml主题转化为json对象

Use Tiny Validator for JSON data

为JSON data使用微小验证器

Get an environment variable(获取活动的环境变量variable_key)

pm.environment.get("variable_key");

Get a global variable(获取全局变量variable_key)

pm.globals.get("variable_key");

Get a variable(获取一个变量variable_key,搜索范围为全局和活动的环境变量)

pm.variables.get("variable_key");

Get a collection variable(获取一个集合变量variable_key)

pm.collectionVariables.get("variable_key");

Set an environment variable(设置一个环境变量,变量名variable_key,属性值variable_value)

pm.environment.set("variable_key", "variable_value");

Set a global variable(设置一个全局变量,设置后global中真实存在,不用时需要进行删除)

pm.globals.set("variable_key", "variable_value");

Set a collection variable(设置一个集合变量)

pm.collectionVariables.set("variable_key", "variable_value");

Clear an environment varlable(删除环境变量为variable_key,该变量只能时当前选择的环境)

pm.environment.unset("variable_key");

Clear a global variable(删除全局变量variable_key)

pm.globals.unset("variable_key");

Clear a collection varlable(删除集合变量variable_key)

pm.collectionVariables.unset("variable_key");

Send a request(请求"https://postman-echo.com/get",并记录在log中)

pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});

Status code: Code is 200(检查状态码是200)

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

Response body: Contains string(检查响应结果包含"hello,world")

pm.test("Body matches string", function () {                                                          pm.expect(pm.response.text()).to.include("hello,world");
});

Response body: JSON value check(转json格式,检查value属性值为100)

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});

Response body: Is equal to a string(检查响应结果等于"response_body_string")

pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});

Response headers: Content-Type header check(检查header中存在的类型"Conten-Type")

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

Response time is less than 200ms(检查响应时间小于200毫秒)

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

Status code: SuccessfulPOST request(检查post请求状态码为201或202)

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});

Status code: Code name has string(检查请求状态字符为OK)

pm.test("Status code name has string", function () {
    pm.response.to.have.status("OK");
});

Response body: Converl XML body toaJSON(xml格式转为json格式)

var jsonObject = xml2Json(responseBody);

Use Tiny Validator for JSON data(为JSON data使用微小验证器)

var schema = {
    "items": {
        "type": "boolean"
    }
};

var data1 = [true, false];
var data2 = [true, 123];

pm.test('Schema is valid', function () {
    pm.expect(tv4.validate(data1, schema)).to.be.true;
    pm.expect(tv4.validate(data2, schema)).to.be.true;
});

二、自定义的编写的断言方法

tests["响应时间小于1000毫秒"] = responseTime < 1000;
tests["响应状态码等于200"] = responseCode.code === 200;
tests["响应结果包含hello"] = responseBody.has("hello");

var data = JSON.parse(responseBody);
tests["value等于100"] = data.value ==== 100;
tests["value等于100或者200"] = data.value ==== 100 || data.value === 200;

console.log("打印log");

// 截取字符串
var str = "123我是谁,皮卡丘456"

// 截取123之后的字符串
re = str.match(/123(\S*)/)[1]
console.log(re)

// 截取456之前的字符串
re = str.match(/(\S*)456/)[1]
console.log(re)

// 截取123 456之间的字符串
re = str.match(/123(\S*)456/)[1]
console.log(re)

三、Pre-request Script(前置请求脚本)

        提供方法与Tests一致,该脚本可以在运行接口前执行。例如运行需要登录权限的接口时,获取token

方法名

注解

Get an environment variable

获取一个环境变量(只会再活动的环境变量中搜索)

Get a global variable

获取一个全局变量

Get a variable

获取一个环境变量(全局变量和活动的环境变量中搜索)

Get a collection variable

获取一个集合变量

Set an environment variable

设置一个环境变量

Set a global variable

设置一个全局变量

Set a collection variable

设置一个集合变量

Clear an environment varlable

清除一个环境变量

Clear a global variable

清除一个全局变量

Clear a collection varlable

清除一个集合变量

Send a request

发送一个请求

四、多接口运行

  1. 讲需要运行的接口放在一个collection(集合)中;

  2. run collection

  3. 勾选需要运行的接口

  4. 可选择select File

    1. json格式:[{"data":"1"},{"data":"2"},{"data":"3"},{"data":"4"}]

    2. text格式:

五、案例:批量运行时,一个接口需要用到另一个接口的数据时怎么实现?

第一个接口在Tests中将数据设置全局变量

pm.globals.set("variable_key", "variable_value");

第二个接口只需要获取到这个全局变量就可以使用

pm.global.get("variable_key");

  • 18
    点赞
  • 176
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值