0. test的構(gòu)成
一個(gè)標(biāo)準(zhǔn)test的寫法如下:
pm.test("test name", function () {
//測(cè)試邏輯
});
它由兩部分構(gòu)成:
- test name. 這個(gè)name會(huì)出現(xiàn)在你的測(cè)試報(bào)告中
- function. 這部分包含你的居體測(cè)試邏輯(解析數(shù)據(jù)祟峦,斷言等等)
一個(gè)具體的例子如下:
pm.test("http code is 200", function () {
//判斷http返回碼
pm.response.to.have.status(200);
});
pm.test("response data is right", function () {
//解析返回值
var response = JSON.parse(responseBody);
//通過斷言判斷返回值的正確性
pm.expect(response.msg).to.eql("ok");
pm.expect(response.code).to.eql(0);
});
1. 如何運(yùn)行/查看一個(gè)test
1. 單個(gè)test
image
- 在Tests窗口寫測(cè)試
- 點(diǎn)擊send發(fā)送請(qǐng)求
- 在Test Results窗口查看結(jié)果
2. 批量test
如下圖所示瞻惋,可以整體運(yùn)行一個(gè)集合的中的全部測(cè)試
image
運(yùn)行后可看到類似下圖的測(cè)試結(jié)果
image
2. 一些常用的變量
//響應(yīng)的http code
pm.response.code
//請(qǐng)求響應(yīng)時(shí)間, 單位ms
pm.response.responseTime,
//返回回?cái)?shù)據(jù)的大小乳怎,包含header
pm.response.responseSize,
//響應(yīng)頭洗贰,一個(gè)object
responseHeaders
//取Content-Type
responseHeaders['Content-Type']
//響應(yīng)體洲鸠,字串蝇更。如果服務(wù)端返回的是json幽崩,需要JSON.parse后才能得到對(duì)應(yīng)的json對(duì)象
responseBody
//獲取相應(yīng)cookie值
pm.cookies.get(cookieName:String)
3. 常用斷言
3.1 pm.expect
1. pm.expect(a).to.equal(b)
a,b嚴(yán)格相等,例:
pm.test("response data is right", function () {
var response = JSON.parse(responseBody);
//response.code === 0
pm.expect(response.code).to.eql(0);
});
2. pm.expect(a).to.deep.equal(b)
a,b為多層結(jié)構(gòu)時(shí),判斷a,b相等帖池。例:
pm.test("deep equal", function(){
var a = {
num1:[1,1,1],
num2:[2,2,2]
};
var b = {
num1:[1,1,1],
num2:[2,2,2]
};
pm.expect(a).to.deep.equal(b);
});
3. pm.expect(object).to.have.property(key)
object中有屬性key奈惑。例:
pm.test("have property", function(){
var object = {
name:"ball",
money:0,
age:100
}
pm.expect(object).to.have.property('age');
});
4. pm.expect(a).to.include('b')
a中包含b
pm.test("include", function(){
//a可以是字串
var str = "this is a test";
pm.expect(str).to.include('this');
//a可以是數(shù)組,此時(shí)b只能是簡(jiǎn)單值睡汹,不能是array肴甸。若希望b為array,需要用members方法做斷言。
var ary = [1,2,3,4];
pm.expect(ary).to.include(2);
//a可以是是object
var obj = {
name:"ball",
money:0,
age:100
};
pm.expect(obj).to.include({age:100, name:"ball"});
});
5. pm.expect(a).to.be.below(b)
a小于b囚巴。例:
//響應(yīng)時(shí)間小于200ms
pm.expect(pm.response.responseTime).to.be.below(200);
6. expect(a).to.be.above(b)
a大于b原在。
更多斷言,參見
https://learning.postman.com/docs/postman/scripts/test-examples/#assertion-library-examples
https://www.chaijs.com/api/bdd/
3.2 pm.response
1. pm.response.to.have.status(code:Number)
判斷http返回碼彤叉。例:
//http code 200
pm.response.to.have.status(200);
2. pm.response.to.have.header(key:String, optionalValue:String)
判斷響應(yīng)頭字段庶柿。例
//響應(yīng)頭中有Cache-Control,其值為no-cache
pm.response.to.have.header('Cache-Control', 'no-cache');
更多使用參見
https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#response-assertion-api-available-in-the-test-scripts
4. 引用沙箱中的js庫
postman的測(cè)試中可以使用很多js庫秽浇。具體參見
https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/
一個(gè)使用crypto-js做AES加密的例子:
var crypto = require('crypto-js');
var uid = "999@qq.com";
var key = crypto.enc.Utf8.parse("112233");
var opt = {
mode:crypto.mode.ECB,
padding:crypto.pad.Pkcs7
}
var userid = crypto.AES.encrypt(uid, key, opt);
5. 發(fā)送異步請(qǐng)求
詳見
https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmsendrequest