推薦我的另一篇文章
淺談JSONObject解析JSON數據,這篇文章原理類似,使用java或者beanshell進行斷言解析json數據
介紹斷言之前,我們先測試1個接口:
接口地址:https://www.v2ex.com/api/nodes/show.json?name=python
- Method: GET
- Authentication: None
做測試的同學,我相信每個人都知道斷言,就是結果和預期對比,如果一致,則用例通過,如果不一致,斷言失敗,用例失敗。那么上面這個接口我們如何用postman來進行斷言呢?也很簡單,用到postman tests 這個模塊。
那么上面的接口我們如何斷言呢?很簡單,從響應內容做斷言。比如我們可以斷言 id=90,url = "http://www.v2ex.com/go/python",狀態碼等於200
我們在tests輸入框填寫:
pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); //斷言狀態碼是200 var jsonData = JSON.parse(responseBody); tests["Check respose id value"] = jsonData.id === 90; //斷言id是90 tests["Check respose url value"] = jsonData.url === "http://www.v2ex.com/go/python"; //斷言url
操作步驟:1、填寫斷言 2、點擊send
查看斷言結果:
當然上面斷言是比較簡單的斷言,如果返回的數據如下所示,我們應該如何斷言呢?
{
"status": 1,
"message": "success",
"data": [
{
"id": 1,
"title": "鄉愁",
"author": "余光中",
"content": "小時候,鄉愁是一枚小小的郵票,我在這頭,母親在那頭。長大后,鄉愁是一張窄窄的船票,我在這頭,新娘在那頭"
},
{
"id": 5,
"title": "鄉愁",
"author": "余光中",
"content": "小時候,鄉愁是一枚小小的郵票,我在這頭,母親在那頭。長大后,鄉愁是一張窄窄的船票,我在這頭,新娘在那頭"
}
]
}
跟上面斷言不同的是,data里面可能有多個json格式的數據,如果我們想斷言data里面第一個json應該如何斷言呢?其實也非常簡單,我們在tests輸入框填寫:
var jsonData = JSON.parse(responseBody); tests["Check respose status value"] = jsonData.status === 1; pm.test("判斷data里面第一個json數據的id為1", function () { var jsonData = pm.response.json(); pm.expect(jsonData.data[0].id).to.eql(1);});
postman常見斷言方法介紹:
Setting an environment variable (設置一個環境變量)
pm.environment.set("variable_key", "variable_value");
Setting a nested object as 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));
Getting an environment variable (獲取環境變量)
pm.environment.get("variable_key");
Getting an environment variable (whose value is a stringified object) 獲取一個環境變量(其值是一個字符串化的對象)
// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.
var array = JSON.parse(pm.environment.get("array"));
var obj = JSON.parse(pm.environment.get("obj"));
Clear an environment variable (清除一個環境變量)
pm.environment.unset("variable_key");
Set a global variable (設置一個全局變量)
pm.globals.set("variable_key", "variable_value");
Get a global variable (獲取一個全局變量)
pm.globals.get("variable_key");
Clear a global variable (清除全局變量)
pm.globals.unset("variable_key");
Get a variable (獲取一個變量)
該函數在全局變量和活動環境中搜索變量。
pm.variables.get("variable_key");
Check if response body contains a string (檢查響應主體是否包含字符串)
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
Check if response body is equal to a string (檢查響應主體是否等於一個字符串)
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
Check for a JSON value (檢查JSON值)
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
Content-Type is present (內容類型存在)
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
Response time is less than 200ms (響應時間小於200ms)
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
Status code is 200 (狀態碼是200)
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Code name contains a string (代碼名稱包含一個字符串)
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
Successful POST request status code (成功的POST請求狀態碼)
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
Use TinyValidator for JSON data (對於JSON數據使用TinyValidator)
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;
});
Decode base64 encoded data (解碼base64編碼的數據)
var intermediate,
base64Content, // assume this has a base64 encoded value
rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);
intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
pm.test('Contents are valid', function() {
pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
});
Send an asynchronous request (發送異步請求)
該功能既可以作為預先請求,也可以作為測試腳本使用。
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
Convert XML body to a JSON object (將XML正文轉換為JSON對象)
var jsonObject = xml2Json(responseBody);