postman test
#### pm.response
pm.response.code //获取当前请求返回的状态码如200,404,500等
pm.response.reason() //当前请求成功返回OK
pm.response.headers://以数组的形式返回当前请求成功后的response的headers
pm.response.responseTime//获取执行此次请求的时间单位为ms
pm.response.text()//以文本的方式获取响应里面的body的内容
pm.response.json():Function → Object //将body里面的内容解析为一个json对象
#### pm.response.to
pm.response.to.have.status(code:Number) //根据状态码判断响应是否成功
pm.response.to.have.status(reason:String)//根据返回的状态值判断响应是否成功
pm.response.to.have.header(key:String) //根据response中的headers的key判断响应是否成功
pm.response.to.have.header(key:String, optionalValue:String)//对response中的header中的key和value校验,判断响应是否成功
pm.response.to.have.body()//获取响应返回的资源
pm.response.to.have.body(optionalValue:String)//对响应返回的body内容校验,判断响应是否成功
pm.response.to.have.body(optionalValue:RegExp)//对响应返回的body进行正则校验,判断响应是否成功
pm.response.to.have.jsonBody()//判断响应返回的body是否是json,判断响应是否成功
pm.response.to.have.jsonBody(optionalExpectEqual:Object)
pm.response.to.have.jsonBody(optionalExpectPath:String)
pm.response.to.have.jsonBody(optionalExpectPath:String, optionalValue:*)
#### pm.cookies
pm.cookies.has(cookieName:String):Function → Boolean //检查请求的url站点是否存在cookie
pm.cookies.get(cookieName:String):Function → String //获取存在的cookie的值
pm.cookies.toObject:Function → Object //将请求的url站点中获取到的所有存在的cookie输出到一个对象中去
pm.test(testName:String, specFunction:Function) //测试方法,第二个方法中填写测试内容
#example
pm.test("response should be okay to process", function () {
pm.response.to.not.be.error;
pm.response.to.have.jsonBody('');
pm.response.to.not.have.jsonBody('error');
});
//该方法需要结合http://chaijs.com/api/bdd/使用
pm.expect(assertion:*)
#example
pm.test('environment to be production', function () {
pm.expect(pm.environment.get('env')).to.equal('production');
});
#### pm.response.to.be.*
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.unauthorised //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
转载自:postman Test Script - 简书 https://www.jianshu.com/p/68966d58859f
网友评论