Postman(day2)
1、Postman的test功能---运行在sandbox中
设置全局变量 :(用例中好多地方都用到的)
postman.clearGlobalVariable("variable_key");
设置环境变量:(比如ip设置)
postman.clearEnvironmentVariable("variable_key");
拿到并处理请求的响应:
tests["Body is correct"]=responseBody==="response_body_string";
定义测试检查点和断言(判断预期结果与实际结果之间的差异):
tests["Response time is less than 200ms"]=reponseTime<200;
tests["state code is 200"]=reponseCode.code===200;
var jsondata = JSON.parse(reponseBody) //将响应的json字符串转为javascript对象
tests["num is 10"]=jsondata.length===10;
2、Http响应的构成:
状态行(状态码和简单的一句描述)
Headers
Body
3、postman的Sandbox
是javascript执行环境,在postman运行一些javascript代码的地方
- 公共库
- 环境变量与全局变量
- 动态变量
- 操作cookie
- 获取和查看请求及响应
- 读取数据文件
sandbox 的 API:
pm.test("状态返回200",function(){
pm.expect(pm.response).to.have.status(200);
pm.expect(pm)
})
Response Assertion API in test scripts
pm.response.to.have.status(code:Number)pm.response.to.have.status(reason:String)pm.response.to.have.header(key:String)pm.response.to.have.header(key:String, optionalValue:String)pm.response.to.have.body()pm.response.to.have.body(optionalValue:String)pm.response.to.have.body(optionalValue:RegExp)pm.response.to.have.jsonBody()pm.response.to.have.jsonBody(optionalExpectEqual:Object)pm.response.to.have.jsonBody(optionalExpectPath:String)pm.response.to.have.jsonBody(optionalExpectPath:String, optionalValue
pm.response.to.be.*
The properties inside the pm.response.to.be object allows you to easily assert a set of pre-defined rules.
pm.response.to.be.info- Checks 1XX status code
pm.response.to.be.success- Checks 2XX status code
pm.response.to.be.redirection- Checks 3XX status code
pm.response.to.be.clientError- Checks 4XX status code
pm.response.to.be.serverError- Checks 5XX
pm.response.to.be.error- Checks 4XX or 5XX
pm.response.to.be.ok- Status code must be 200
pm.response.to.be.accepted- Status code must be 202
pm.response.to.be.badRequest- Status code must be 400
pm.response.to.be.unauthorized- Status code must be 401
pm.response.to.be.forbidden- Status code 403
pm.response.to.be.notFound
- Status code of response is checked to be 404
pm.response.to.be.rateLimited- Checks whether response status code is 429

浙公网安备 33010602011771号