1. 程式人生 > >Postman-Tests模塊測試方法記錄

Postman-Tests模塊測試方法記錄

ase ade 試用 seh parameter request assume arc 腳本

用Postman的時候大多數測試結果是可以用Tests模塊的測試方法來代替人工檢查的,測試方法本質上是JavaScript代碼,我們可以通過運行測試用例(測試腳本是在發送請求之後並且從服務器接收到響應時執行),觀察結果是“PASS”還是“FAIL”就能判斷測試結果:

技術分享圖片

在此記錄一些常用方法備忘:

1.設置環境變量

pm.environment.set("variable_key", "variable_value");

2.將一個嵌套的對象設置為一個環境變量

var array = [1, 2, 3, 4];

pm.environment.set("array", JSON.stringify(array, null, 2));

var obj = { a: [1, 2, 3, 4], b: { c: ‘val‘ } };

pm.environment.set("obj", JSON.stringify(obj))

3.獲得一個環境變量

pm.environment.get("variable_key");

4.獲得一個環境變量(其值是一個字符串化的對象)

// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.

var array = JSON.parse(pm.environment.get("array"));

var obj = JSON.parse(pm.environment.get("obj"));

5.清除一個環境變量

pm.environment.unset("variable_key");

6.設置一個全局變量

pm.globals.set("variable_key", "variable_value");

7.獲取一個全局變量

pm.globals.get("variable_key");

8.清除一個全局變量

pm.globals.unset("variable_key");

9.獲取一個變量(該函數在全局變量和活動環境中搜索變量)
pm.variables.get("variable_key");

10.檢查響應主體是否包含字符串

pm.test("Body matches string", function () {

pm.expect(pm.response.text()).to.include("string_you_want_to_search");

});

11.檢查響應體是否等於字符串

pm.test("Body is correct", function () {

pm.response.to.have.body("response_body_string");
});

12.檢查JSON值

pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});

13.Content-Type 存在

pm.test("Content-Type is present", function () {

pm.response.to.have.header("Content-Type");
});

14.返回時間少於200ms

pm.test("Response time is less than 200ms", function () {

pm.expect(pm.response.responseTime).to.be.below(200);

});

15.狀態碼是200

pm.test("Status code is 200", function () {

pm.response.to.have.status(200);

});

16.代碼名包含一個字符串

pm.test("Status code name has string", function () {

pm.response.to.have.status("Created");

});

17.成功的POST請求狀態碼

pm.test("Successful POST request", function () {

pm.expect(pm.response.code).to.be.oneOf([201,202]);

});

18.為JSON數據使用TinyValidator

var schema = {

"items": {
"type": "boolean"

}

};

var data1 = [true, false];

var data2 = [true, 123];

pm.test(‘Schema is valid‘, function() {

pm.expect(tv4.validate(data1, schema)).to.be.true;

pm.expect(tv4.validate(data2, schema)).to.be.true;

});

19.解碼base64編碼數據

var intermediate,

base64Content, // assume this has a base64 encoded value

rawContent = base64Content.slice(‘data:application/octet-stream;base64,‘.length);


intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js

pm.test(‘Contents are valid‘, function() {

pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness

});

20.發送異步請求
此函數可作為預請求和測試腳本使用

pm.sendRequest("https://postman-echo.com/get", function (err, response) {

console.log(response.json());

});

21.將XML主體轉換為JSON對象

var jsonObject = xml2Json(responseBody);

-----------------------------------------------------------------------------------------------------------------------------

1. 清除一個全局變量
Clear a global variable
對應腳本:
postman.clearGlobalVariable("variable_key");
參數:需要清除的變量的key

2.清除一個環境變量
Clear an environment variable
對應腳本:
postman.clearEnvironmentVariable("variable_key");
參數:需要清除的環境變量的key

3.response包含內容
Response body:Contains string
對應腳本:
tests["Body matches string"] =responseBody.has("string_you_want_to_search");
參數:預期內容

4.將xml格式的response轉換成son格式
Response body:Convert XML body to a JSON Object
對應腳本:
var jsonObject = xml2Json(responseBody);
參數:(默認不需要設置參數,為接口的response)需要轉換的xml

5.response等於預期內容
Response body:Is equal to a string
對應腳本:
tests["Body is correct"] = responseBody === "response_body_string";
參數:預期response

6.json解析key的值進行校驗
Response body:JSON value check
對應腳本:
tests["Args key contains argument passed as url parameter"] = ‘test‘ in responseJSON.args
參數:test替換被測的值,args替換被測的key

7.檢查response的header信息是否有被測字段
Response headers:Content-Type header check
對應腳本:
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");
參數:預期header

8.響應時間判斷
Response time is less than 200ms
對應腳本:
tests["Response time is less than 200ms"] = responseTime < 200;
參數:響應時間

9.設置全局變量
Set an global variable
對應腳本:
postman.setGlobalVariable("variable_key", "variable_value");
參數:全局變量的鍵值

10.設置環境變量
Set an environment variable
對應腳本:
postman.setEnvironmentVariable("variable_key", "variable_value");
參數:環境變量的鍵值

11.判斷狀態碼
Status code:Code is 200
對應腳本:
tests["Status code is 200"] = responseCode.code != 400;
參數:狀態碼

12.檢查code name 是否包含內容
Status code:Code name has string
對應腳本:
tests["Status code name has string"] = responseCode.name.has("Created");
參數:預期code name包含字符串

13.成功的post請求
Status code:Successful POST request
對應腳本:
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

14.微小驗證器
Use Tiny Validator for JSON data
對應腳本:
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
console.log(tv4.error);
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
參數:可以修改items裏面的鍵值對來對應驗證json的參數

Postman-Tests模塊測試方法記錄