测试 - Postman

about

引子官网的话:

Postman是一个API开发的协作平台。Postman的特性简化了构建API的每一步并简化了协作,因此您可以更快地创建更好的API。

下载

官网下载链接:https://www.getpostman.com/downloads/

百度云链接:https://pan.baidu.com/s/1YSafbmRWxB3YcKHxlF03pA 提取码:k44l

安装

安装是直接一路下一步就OK了。

使用

断言

postman断言是JavaScript语言编写的,在postman客户端指定区域编写即可。

clear a global variable清除全局变量pm.globals.unset("variable_key");
Clear an environment variable 清除一个环境变量 pm.environment.unset("variable_key");
get a global variable 得到一个全局变量 pm.globals.get("variable_key");
get a variable 得到一个变量 pm.variables.get("variable_key");
Get an environment variable 得到一个环境变量 pm.environment.get("variable_key");
response body:contains string 检查response body包含字符串 pm.test("Body matches string", function () { pm.expect(pm.response.text()).to.include("string_you_want_to_search"); });
response body:convert XML body to a JSON object response body:将XML转换为JSON对象 var jsonObject = xml2Json(responseBody);
response body:is equal to a string 检查响应体等于一个字符串 pm.test("Body is correct", function () { pm.response.to.have.body("response_body_string"); });
response body:JSON value check 检查response body中JSON某字段值 pm.test("Your test name", function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); });
response headers:content-Type header check 检查content-Type是否包含在header返回 pm.test("Content-Type is present", function () { pm.response.to.have.header("Content-Type"); });
response time is than 200ms 响应时间超过200ms pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); });
send s request 发送一个请求 pm.sendRequest("https://postman-echo.com/get", function (err, response) { console.log(resp onse.json()); });
set a global variable 设置一个全局变量 pm.globals.set("variable_key", "variable_value");
set an environment variable 设置一个环境变量 pm.environment.set("variable_key", "variable_value");
status code:Code is 200 状态码:代码是200 pm.test("Status code is 200", function () { pm.response.to.have.status(200); });
status code:code name has string 状态码:代码中有指定字符串 pm.test("Status code name has string", function () { pm.response.to.have.status("Created"); });
status code:successful POST request 状态码:成功的post请求 pm.test("Successful POST request", function () { pm.expect(pm.response.code).to.be.oneOf([201,202]); });
use tiny validator for JSON data 为json数据使用tiny验证器 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; });

see also:https://www.cnblogs.com/wangsen-123/p/9139885.html

posted @ 2019-11-30 17:09  天之坚毅  阅读(201)  评论(0编辑  收藏  举报