0
点赞
收藏
分享

微信扫一扫

Postman 使用

大南瓜鸭 2022-04-16 阅读 44
postman

这里写自定义目录标题

1. Postman 使用

1.1. Postman Console

应用菜单 –>View—>Show Postman Console, 打印变量的值, 就可以在此窗口查看数据。

1.2. Postman Tests 使用

在 Tests 里面写 JS 语句, 如返回是下面的话:

{
    "success": false,
    "code": -3001,
    "msg": "","tips": {"duration": 400,"limitMsg":" 前方拥挤, 请稍后再试..."},"data": {}
}

为了检测 success 是否是 true, 就这样写:

pm.test("success", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.success).to.eql("true");
});

写完后, 点击左上角的 “Runner” 进入跑测试。

1.2.1. 例子

pm.environment.get("variable_key");
pm.globals.get("variable_key");
pm.variables.get("variable_key");
pm.environment.set("variable_key", "variable_value");
pm.environment.unset("variable_key");
pm.environment.unset("variable_key");
pm.globals.unset("variable_key");
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});

1.2.2. 校验接口响应的状态码, 常见的有 200、404、500 当然也包括前面鉴权学到的 401 等等。

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

1.2.3. 检查响应信息中是否包含某些指定的字符串;

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

1.2.4. 检查从 JSON 响应中获取到某个字段, 判断其是否与预期字段一致;

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

1.2.5. 检查实际获取的响应体 (即 Body 信息) 与预期结果的响应体是否一致;

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

1.2.6. 检查响应中的头域信息 (Headers) 是否与预期一致;

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

1.2.7. 判断实际响应时间是否与低于预期时间

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

1.2.8. 检查响应码是否与预期集合中的某个值一致

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

1.2.9. 检查响应信息中是否包含某个预期值

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

1.2.10. 转化 XML 格式的响应成 JSON 对象

var jsonObject = xml2Json(responseBody);
举报

相关推荐

0 条评论