postman test
#### pm.response
pm.response.code //獲取當(dāng)前請(qǐng)求返回的狀態(tài)碼如200,404,500等
pm.response.reason() //當(dāng)前請(qǐng)求成功返回OK
pm.response.headers://以數(shù)組的形式返回當(dāng)前請(qǐng)求成功后的response的headers
pm.response.responseTime//獲取執(zhí)行此次請(qǐng)求的時(shí)間單位為ms
pm.response.text()//以文本的方式獲取響應(yīng)里面的body的內(nèi)容
pm.response.json():Function → Object //將body里面的內(nèi)容解析為一個(gè)json對(duì)象
####?pm.response.to
pm.response.to.have.status(code:Number) //根據(jù)狀態(tài)碼判斷響應(yīng)是否成功
pm.response.to.have.status(reason:String)//根據(jù)返回的狀態(tài)值判斷響應(yīng)是否成功
pm.response.to.have.header(key:String) //根據(jù)response中的headers的key判斷響應(yīng)是否成功
pm.response.to.have.header(key:String, optionalValue:String)//對(duì)response中的header中的key和value校驗(yàn),判斷響應(yīng)是否成功
pm.response.to.have.body()//獲取響應(yīng)返回的資源
pm.response.to.have.body(optionalValue:String)//對(duì)響應(yīng)返回的body內(nèi)容校驗(yàn)撵儿,判斷響應(yīng)是否成功
pm.response.to.have.body(optionalValue:RegExp)//對(duì)響應(yīng)返回的body進(jìn)行正則校驗(yàn),判斷響應(yīng)是否成功
pm.response.to.have.jsonBody()//判斷響應(yīng)返回的body是否是json,判斷響應(yīng)是否成功
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 //檢查請(qǐng)求的url站點(diǎn)是否存在cookie
pm.cookies.get(cookieName:String):Function → String //獲取存在的cookie的值
pm.cookies.toObject:Function → Object //將請(qǐng)求的url站點(diǎn)中獲取到的所有存在的cookie輸出到一個(gè)對(duì)象中去
pm.test(testName:String, specFunction:Function) //測(cè)試方法,第二個(gè)方法中填寫測(cè)試內(nèi)容
#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');
? });
//該方法需要結(jié)合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?判斷當(dāng)前請(qǐng)求響應(yīng)的狀態(tài)值?//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
轉(zhuǎn)載自:postman Test Script - 簡(jiǎn)書 http://www.reibang.com/p/68966d58859f