postman接口返回結(jié)果驗(yàn)證

說了這么多接口請求的驗(yàn)證,接下來和大家聊聊怎么驗(yàn)證返回吧!
1.最基本的返回驗(yàn)證
這部分內(nèi)容隨便搜一搜,比比皆是,為了方便大家尋找還是貼一下吧

//1.驗(yàn)證返回狀態(tài)碼是否是200
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
//2.驗(yàn)證返回body內(nèi)是否含有某個值
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
//3.驗(yàn)證某個返回值是否是100
pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});
//驗(yàn)證返回body中是否含有某個字符串
pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});
//驗(yàn)證返回頭類型
pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});
//驗(yàn)證請求時長是否小于200ms
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});
//驗(yàn)證返回碼是否為200
pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
//驗(yàn)證返回?cái)?shù)據(jù)中是否包含某個字符串
pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});
//驗(yàn)證json數(shù)據(jù)的微小驗(yàn)證器
var jsonObject = xml2Json(responseBody);
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;
});

下面我們來著重講一下微小驗(yàn)證器:
該方法請參閱文章:https://blog.csdn.net/willcaty/article/details/65631811
關(guān)于schema個人覺得寫起來很麻煩,特地找了個在線工具:https://jsonschema.net/#/
復(fù)制當(dāng)前的接口返回,然后將格式弄下來,進(jìn)行改動即可,親試有效

由于個人原因目前我所掌握的已經(jīng)在這啦,歡迎大神不略賜教,嘿嘿

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容