postman斷言使用大全

1.postman 的檢查點比較全面的例子(來自postman的官網)https://learning.getpostman.com/docs/postman/scripts/test_examples/。

2.針對postman的官網例子,做了如下測試:

1》設置環境變量Setting an environment variable

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));
 

2》設置全局變量Set a global variable

 

3》檢查響應躰是否包含某字符串:

pm.test("test body include",function(){pm.expect(pm.response.text()).to.include("abc");})

4》檢查響應躰是否等於某字符串:

pm.test("test body equeal",function(){pm.response.to.have.body("abc");})

5》檢查content-type是否存在:

pm.test("content-type is present",function(){pm.response.to.have.header("content-type");})

6》檢查響應時間不超過200ms:

pm.test("test time not 200ms",function(){pm.expect(pm.response.responseTime).to.be.below(200);})

7》檢查狀態嗎是200:
pm.test("test statu is 200",function(){pm.response.to.have.status(200);})

8》檢查code name 是否包含某個字符串:

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

9》檢查post的正確狀態嗎:
pm.test("success post code",function(){pm.expect(pm.response.code).to.be.oneOf([201,202]);})

10》用小型校驗器校驗json

下例是校驗json數組中是否都是boolean的值。
var schema = {
 "items": {
 "type": "boolean"
 }
};
var data1 = [false, false];
var data2 = [true, 123];
pm.test('Schema is valid', function() {
  pm.expect(tv4.validate(data1, schema)).to.be.true;
});

11》發送異步請求:

pm.sendRequest("https://www.baidu.com",function(erro,reponse){
    console.log(response.json())
});

12》xml轉爲json:

var jsonObject=xml2Json(responsebody);

猜你喜欢

转载自www.cnblogs.com/testchy/p/10671022.html