具體的TEST 方法:
說明:紅色部分是對test方法的說明;橙黃色區域是Postman內test方法的原樣例;下面黑色部分的斷言方法是根據退出登錄接口編寫的具體實操。
1.檢測返回的結果包含字段 Response body:Contains a string
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
pm.test("檢測返回結果中包含字段", function () {
? ? pm.expect(pm.response.text()).to.include("操作成功");
});
//2.獲取環境變量的值:pm.variables.get("variable_key");
var data=pm.environment.get("something_uat1");
tests['檢測設置的環境變量值']=data==="http://shomething.uat1.rs.com";
//3.測試返回的結果包含code:200
/*
pm.test("Status code is 200", function () {
? ? pm.response.to.have.status(200);
});
*/
pm.test("Status code is 200", function () {
? ? pm.response.to.have.status(200);
});
//4.測試響應低于規定時長
/*
pm.test("Response time is less than 200ms", function () {
? ? pm.expect(pm.response.responseTime).to.be.below(200);
});
*/
pm.test("Response time is less than 200ms", function () {
? ? pm.expect(pm.response.responseTime).to.be.below(200);
});
//5.檢測返回結果字符串?
/*pm.test("Body is correct", function () {
? ? pm.response.to.have.body("response_body_string");
});*/
pm.test("檢測返回的整段文字是否相等", function () {
? ? pm.response.to.have.body('{"code":"200","message":"操作成功","dataMap":true}');
});
//6.檢測返回字段值與預期是否相等.下面有三種寫法Response body:JSON value check
/*
pm.test("Your test name", function () {
? ? var jsonData = pm.response.json();
? ? pm.expect(jsonData.value).to.eql(100);
});
*/
//1).postman方法將字符串轉換成json對象
pm.test("測試返回的message的值", function () {
? ? var jsonData = pm.response.json();
? ? pm.expect(jsonData.message).to.eql("操作成功");
});
//2).JSON語法將字符串轉換成json對象
pm.test("測試返回的dataMap的值", function () {
? ? var jsonData =JSON.parse(responseBody);
? ? jsonData.dataMap===true;
});
//3)使用tests方法來進行斷言
var jsondata =JSON.parse(responseBody);
tests["測試返回的code=200"]=jsondata.code==="200";
//7.返回code包含任意元素
/*
pm.test("Successful POST request", function () {
? ? pm.expect(pm.response.code).to.be.oneOf([201,202]);
});*/
pm.test("返回code包含任意元素", function () {
? ? pm.expect(pm.response.code).to.be.oneOf([200,202]);
});
//8 校驗JSON的文檔格式 Use Tiny Validator for JSON data
/* 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;
});? ? ? */
//注意:type類型內的 string,boolean都要小寫 且加引號?
//解釋說明,下方定義一個schema描述JSON文檔格式
var schema = {
? ? "properties":{
? ? ? ? "code": {
? ? ? ? "type": "string",
? ? ? ? ?"description":"return code"
? ? ? ? ? ? ? ? ?},
? ? ? ? "message":{
? ? ? ? "type": "string",
? ? ? ? "description":"return message"
? ? ? ? ? ? ? ? },
? ? ? ? "dataMap": {
? ? ? ? "type": "boolean",
? ? ? ? "description":"return if it? is successful"
? ? ? ? ? ? ? ? }
? ? ? ? },
? ? "required": ["code", "message", "dataMap"]
};
//獲取接口返回的JSON格式的數據
var data=JSON.parse(responseBody);
//校驗返回的responseBody和定義的schema是否相符
pm.test('Schema is valid', function() {
? pm.expect(tv4.validate(data, schema)).to.be.true;
?
});
————————————————
版權聲明:本文為CSDN博主「馬艷云」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/mayanyun2013/article/details/88697748