
一、語法
1、語法簡介
pm.test("測試名稱", function () {
// 測試代碼
});
// 或 使用 ES6 語法
pm.test("測試名稱", () => {
// 測試代碼
});
說明:使用 pm.test() 方法,來定義一個測試。包含兩個參數(shù):
- 參數(shù)一:字符串,表示測試名稱,
- 參數(shù)二:函數(shù),用于測試的 function,可以在函數(shù)體中編寫自己的測試代碼。
2、測試規(guī)則說明
在參數(shù)二傳入的測試函數(shù)中,函數(shù)體中拋出異常,則測試失敗,否則測試成功。
舉例:這個例子會導(dǎo)致測試失敗,但只會顯示測試名稱,不會顯示額外信息
pm.test("My Test", function () {
throw 'MyException'
});
如果需要輸出詳細(xì)信息:
pm.test("My Test", function () {
throw {
name: "異常名稱",
message: "需要顯示的詳細(xì)異常消息"
}
});
二、測試的編寫
上面拋出異常的方式,理論上沒問題,但由于這種方式編寫過于繁瑣,所以實際場景中不采用這種方式。
Postman 測試可以使用 Chai Assertion Library BDD 語法,它提供了優(yōu)化測試,提供了更好的可讀性選項。
這種方式能夠讓代碼更明確地表示測試目標(biāo),并且能夠拋出具備更明確信息的異常,異常類型為 AssertionError。
1、最簡單的規(guī)則判斷
pm.test("使用 expect 測試", function () {
pm.expect(true).to.eql(false);
pm.expect('abc').to.eql('xyz');
pm.expect(200).to.eql(300);
});
2、其他測試
如:測試狀態(tài)碼是否為 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
三、常見斷言樣例
1、返回值斷言
判斷 Response 中的屬性與環(huán)境變量值是否一致
pm.test("Response property matches environment variable", function () {
pm.expect(pm.response.json().name).to.eql(pm.environment.get("name"));
});
2、值類型斷言
判斷值類型
/* response has this structure:
{
"name": "Jane",
"age": 29,
"hobbies": [
"skating",
"painting"
],
"email": null
}
*/
const jsonData = pm.response.json();
pm.test("Test data type of the response", () => {
pm.expect(jsonData).to.be.an("object");
pm.expect(jsonData.name).to.be.a("string");
pm.expect(jsonData.age).to.be.a("number");
pm.expect(jsonData.hobbies).to.be.an("array");
pm.expect(jsonData.website).to.be.undefined;
pm.expect(jsonData.email).to.be.null;
});
3、數(shù)組屬性斷言
/*
response has this structure:
{
"errors": [],
"areas": [ "goods", "services" ],
"settings": [
{
"type": "notification",
"detail": [ "email", "sms" ]
},
{
"type": "visual",
"detail": [ "light", "large" ]
}
]
}
*/
const jsonData = pm.response.json();
pm.test("Test array properties", () => {
//errors array is empty
pm.expect(jsonData.errors).to.be.empty;
//areas includes "goods"
pm.expect(jsonData.areas).to.include("goods");
//get the notification settings object
const notificationSettings = jsonData.settings.find
(m => m.type === "notification");
pm.expect(notificationSettings)
.to.be.an("object", "Could not find the setting");
//detail array must include "sms"
pm.expect(notificationSettings.detail).to.include("sms");
//detail array must include all listed
pm.expect(notificationSettings.detail)
.to.have.members(["email", "sms"]);
});
4、對象屬性斷言
pm.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
pm.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
pm.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
pm.expect({a: 1}).to.have.property('a');
pm.expect({a: 1, b: 2}).to.be.an('object')
.that.has.all.keys('a', 'b');
5、值包含在 Set 中的斷言
pm.test("Value is in valid list", () => {
pm.expect(pm.response.json().type)
.to.be.oneOf(["Subscriber", "Customer", "User"]);
});
6、對象包含斷言
/*
response has the following structure:
{
"id": "d8893057-3e91-4cdd-a36f-a0af460b6373",
"created": true,
"errors": []
}
*/
pm.test("Object is contained", () => {
const expectedObject = {
"created": true,
"errors": []
};
pm.expect(pm.response.json()).to.deep.include(expectedObject);
});
四、HTTP 響應(yīng)斷言
1、測試響應(yīng) Body
判斷響應(yīng)體中的值:
pm.test("Person is Jane", () => {
const responseJson = pm.response.json();
pm.expect(responseJson.name).to.eql("Jane");
pm.expect(responseJson.age).to.eql(23);
});
2、測試狀態(tài)碼
測試響應(yīng)狀態(tài)碼:
pm.test("Status code is 201", () => {
pm.response.to.have.status(201);
});
判斷響應(yīng)狀態(tài)碼是多值中的一個:
pm.test("Successful POST request", () => {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
判斷狀態(tài)碼文本:
pm.test("Status code name has string", () => {
pm.response.to.have.status("Created");
});
3、測試響應(yīng)頭
判斷指定響應(yīng)頭是否存在:
pm.test("Content-Type header is present", () => {
pm.response.to.have.header("Content-Type");
});
判斷響應(yīng)頭指定字段值:
pm.test("Content-Type header is application/json", () => {
pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json');
});
4、測試 Cookie
判斷 Cookie 存在
pm.test("Cookie JSESSIONID is present", () => {
pm.expect(pm.cookies.has('JSESSIONID')).to.be.true;
});
判斷 Cookie 值
pm.test("Cookie isLoggedIn has value 1", () => {
pm.expect(pm.cookies.get('isLoggedIn')).to.eql('1');
});
5、測試響應(yīng)時間
判斷響應(yīng)時間在指定范圍:
pm.test("Response time is less than 200ms", () => {
pm.expect(pm.response.responseTime).to.be.below(200);
});
五、參考
- Test script examples
https://learning.postman.com/docs/writing-scripts/script-references/test-examples/
- Chai Assertion Library - BDD
https://www.chaijs.com/api/bdd/
(完)