一、預處理
Pre-request Scrip 1、Pre-request Script是集合中請求發送之前需要執行的代碼片段
2、請求參數中包含一個隨機數或者請求header中包括一個時間戳,或者你的請求參數需要加密
Pre-request Script常用代碼
右側提供了一些常用的代碼


二、斷言
postman斷言是JavaScript語言編寫的,在postman客戶端指定區域編寫即可。
斷言會在請求返回之后,運行,並根據斷言的pass\fail情況體現在最終測試結果中。
| 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; }); |
