postman tests 測試實例


1、返回狀態碼是200
pm.test("返回狀態碼是200", function () {
pm.response.to.have.status(200);
});

tests["返回狀態碼是200"] = responseCode.code === 200;

 

2、返回時間小於200毫秒
pm.test("返回時間小於200毫秒", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

tests["返回時間小於200毫秒"] = responseTime < 200;
pm.test("返回的是字符串", function () {

pm.response.to.be.string;
});

3.設置環境變量
postman.setEnvironmentVariable("key", "value");
pm.environment.set("variable_key", "variable_value");

4.將嵌套獨享設置為環境變量

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

postman.setEnvironmentVariable("array", JSON.stringify(array, null, 2));

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

postman.setEnvironmentVariable("obj", JSON.stringify(obj));

5.獲取環境變量
postman.getEnvironmentVariable("key");
獲取一個環境變量(其值是一個字符串對象)
var array = JSON.parse(postman.getEnvironmentVariable("array"));

var obj = JSON.parse(postman.getEnvironmentVariable("obj"));

6.清除一個環境變量

postman.clearEnvironmentVariable("key");
pm.environment.unset("variable_key");


7.設置一個全局變量

postman.setGlobalVariable("key", "value");

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

8.獲取全局變量


postman.getGlobalVariable("key");

pm.globals.get("variable_key");

9.清除全局變量

postman.clearGlobalVariable("key");
pm.globals.unset("variable_key");

10.檢查response是否包含一個字符串

tests["Body matches string"] = responseBody.has("string_you_want_to_search");


11.將xml體轉換為JSON對象

var jsonObject = xml2Json(responseBody);

12.檢查response是否等於一個字符串

tests["Body is correct"] = responseBody === "response_body_string";


13.檢查JSON值

var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;


14.內容類型存在(不區分大小寫的檢查)

tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

15.內容類型存在(區分大小寫)

tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

 

16.響應時間在一個特定的范圍內(包括下限和上限)


tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001);

17.代碼包含一個字符串

tests["Status code name has string"] = responseCode.name.has("Created");


18.成功的POST請求狀態碼

tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

19.將TinyValidator用於JSON數據

var schema = {
"items": {
"type": "boolean"
}
};

var data1 = [true, false];

var data2 = [true, 123];

tests["Valid Data1"] = tv4.validate(data1, schema);

tests["Valid Data2"] = tv4.validate(data2, schema);

console.log("Validation failed: ", tv4.error);

20.解碼base64編碼數據

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


intermediate = CryptoJS.enc.Base64.parse(base64content);
tests["Contents are valid"] = CryptoJS.enc.Utf8.stringify(intermediate);

參考網址:
https://blog.csdn.net/weixin_34407348/article/details/93331261
https://blog.csdn.net/qq1124794084/article/details/78049456
https://www.jianshu.com/p/61cfcb436ee4 Postman使用手冊4——API test


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM