postman tests 方法集合


postmanTests可設置/清除變量, 對比返回值信息。常用方法集合匯總如下:

 

1.設置環境變量

postman.setEnvironmentVariable("key", "value");
pm.environment.get("key", "value");

 

2.設置全局變量

postman.setGlobalVariable("key", "value");
pm.globals.set("variable_key", "variable_value");

 

3.檢查response body中是否包含某個string

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

pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});

 

4.檢測JSON中的某個值是否等於預期的值

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

JSON.parse()方法,把json字符串轉化為對象。parse()會進行json格式的檢查是一個安全的函數。 

如:檢查json中某個數組元素的個數(這里檢測programs的長度)

var data = JSON.parse(responseBody);
tests["program's lenght"] = data.programs.length === 5;

 

5.轉換XML body為JSON對象

var jsonObject = xml2Json(responseBody);

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

 

6.檢查response body是否與某個string相等

7.測試response Headers中的某個元素是否存在(如:Content-Type)

//getResponseHeader()方法會返回header的值,如果該值存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); 

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

上面的方法,不區分大小寫。下面的方法,要區分大小寫。 

 

8.驗證Status code的值

tests["Status code is 200"] = responseCode.code === 200;

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});//5.0以上版本方法

 

9.驗證Response time是否小於某個值

tests["Response time is less than 200ms"] = responseTime < 200;

//5.0以上版本方法
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

 

10.name是否包含某個值

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

pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});

 

11.POST 請求的狀態響應碼是否是某個值

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

//5.0以上版本方法
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

 

12.很小的JSON數據驗證器

復制代碼
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);
復制代碼
 
13.獲取request.data值:
var Json = JSON.parse(request.data); 
  • data {object}:
    this is a dictionary of form data for the request. (request.data["key"]=="value")

  • headers {object}:
    this is a dictionary of headers for the request (request.headers["key"]=="value")

  • method {string}:
    GET/POST/PUT etc.

  • url {string}:
    the url for the request.

假設requestBody中有"version":"1.0";這個值,如果想獲取到version的value值,代碼如下
var Json = JSON.parse(request.data); 
var version = Json["version"];

 

14.JSON.parse()和JSON.stringify()

復制代碼
 
          
JSON.parse()【從一個字符串中解析出json對象】
JSON.stringify()【從一個對象中解析出字符串】

var data={name:'goatling'}
JSON.parse(data)
結果是:
'{"name":"goatling"}'

JSON.stringify(data)
結果是:
name:"goatling"
 
復制代碼

 

15.判斷字段值是否為空typeof()

var Jsondata = JSON.parse(responseBody);
if( typeof(Jsondata.data) != "undefined" )

 

 

注:文章轉自https://www.cnblogs.com/JHblogs/p/6418802.html

  原文中14描述有誤,已在文中修改。


免責聲明!

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



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