postman斷言應用詳解
| clear a global variable | 清除全域變數 | pm.globals.unset("variable_key"); |
| Clear an environment variable | 清除環境變數 | pm.environment.unset("variable_key"); |
| get a global variable | 得到一個全域變數 | pm.globals.get("variable_key"); |
| get a variable | 得到一個變數 | pm.variables.get("variable_key"); |
| Get an environment variable | 得到一個環境變數 | pm.environment.get("variable_key"); |
| response body:contains string | 檢查response body包含字串 | pm.test("Body matches string", function () { pm.expect(pm.response.text()).to.include("string_you_want_to_search"); }); |
| response body:convert XML body to a JSON object | response body:將XML轉換為JSON物件 | var jsonObject = xml2Json(responseBody); |
| response body:is equal to a string | 檢查response body等于指定字串 | pm.test("Body is correct", function () { pm.response.to.have.body("response_body_string"); }); |
| response body:JSON value check | 檢查response body中JSON某欄位值 | pm.test("Your test name", function () { var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(100); }); |
| response headers:content-Type header check | 檢查content-Type是否包含在header回傳 | pm.test("Content-Type is present", function () { pm.response.to.have.header("Content-Type"); }); |
| response time is than 200ms | 回應時間超過200ms | pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); }); |
| send s request | 發送一個請求 | pm.sendRequest("https://postman-echo.com/get", function (err, response) { console.log(resp onse.json()); }); |
| set a global variable | 設定一個全域變數 | pm.globals.set("variable_key", "variable_value"); |
| set an environment variable | 設定一個環境變數 | pm.environment.set("variable_key", "variable_value"); |
| status code:Code is 200 | 狀態碼:代碼是200 | pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); |
| status code:code name has string | 狀態碼:代碼中有指定字串 | pm.test("Status code name has string", function () { pm.response.to.have.status("Created"); }); |
| status code:successful POST request | 狀態碼:成功的post請求 | pm.test("Successful POST request", function () { pm.expect(pm.response.code).to.be.oneOf([201,202]); }); |
| use tiny validator for JSON data | 為json資料使用tiny驗證器 | 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; }); |
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/225958.html
標籤:其他
