咱們來看一些Postman測試的例子。其中大多數都是Postman內部的片斷。您能夠根據須要爲請求提供儘量多的測試。npm
設置環境變量json
pm.environment.set("variable_key", "variable_value");app
將嵌套對象設置爲環境變量less
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));post
獲取環境變量測試
pm.environment.get("variable_key");ui
獲取環境變量(其值是字符串化對象)this
// 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"));
清除環境變量
pm.environment.unset("variable_key");
設置全局變量
pm.globals.set("variable_key", "variable_value");
獲取全局變量
pm.globals.get("variable_key");
清除全局變量
pm.globals.unset("variable_key");
獲得一個變量
此函數在全局變量和活動環境中搜索變量。
pm.variables.get("variable_key");
檢查響應主體是否包含字符串
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");});
檢查響應主體是否等於字符串
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");});
檢查JSON值
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);});
內容類型存在
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");});
響應時間小於200毫秒
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);});
狀態代碼是200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);});
代碼名稱包含一個字符串
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");});
成功的POST請求狀態代碼
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);});
使用TinyValidator獲取JSON數據
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;});
JSON模式驗證器
var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
"properties": {
"alpha": {
"type": "boolean"
}
}
};
pm.test('Schema is valid', function() {
pm.expect(ajv.validate(schema, {alpha: true})).to.be.true;
pm.expect(ajv.validate(schema, {alpha: 123})).to.be.false;});
解碼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});
發送異步請求
此功能既可用做預請求腳本,也可用做測試腳本。
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());});
將XML主體轉換爲JSON對象
var jsonObject = xml2Json(responseBody);
JSON文件由鍵/值對組成。
對於CSV文件,頂行須要包含變量名稱。
較舊的Postman測試編寫風格依賴於特殊tests對象的設置值。您能夠爲對象中的元素設置描述性鍵,而後說明它是真仍是假。例如,tests["Body contains user_id"] = responsebody.has("user_id");將檢查響應主體是否包含user_id字符串。
您能夠根據須要添加任意數量的密鑰,具體取決於您要測試的內容。在響應查看器下的「 測試」選項卡下,您能夠查看測試結果。選項卡標題顯示傳遞了多少測試,並在此處列出了您在tests變量中設置的鍵。若是值的計算結果爲true,則測試經過。
設置環境變量
postman.setEnvironmentVariable("key", "value");
將嵌套對象設置爲環境變量
var array = [1, 2, 3, 4];
postman.setEnvironmentVariable("array", JSON.stringify(array, null, 2));var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
postman.setEnvironmentVariable("obj", JSON.stringify(obj));
獲取環境變量
postman.getEnvironmentVariable("key");
獲取環境變量(其值是字符串化對象)
// These statements should be wrapped in a try-catch block if the data is coming from an unknown source.var array = JSON.parse(postman.getEnvironmentVariable("array"));var obj = JSON.parse(postman.getEnvironmentVariable("obj"));
清除環境變量
postman.clearEnvironmentVariable("key");
設置全局變量
postman.setGlobalVariable("key", "value");
獲取全局變量
postman.getGlobalVariable("key");
清除全局變量
postman.clearGlobalVariable("key");
檢查響應主體是否包含字符串
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
將XML主體轉換爲JSON對象
var jsonObject = xml2Json(responseBody);
檢查響應主體是否等於字符串
tests["Body is correct"] = responseBody === "response_body_string";
檢查JSON值
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
存在Content-Type(不區分大小寫的檢查)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
內容類型存在(區分大小寫)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
響應時間小於200毫秒
tests["Response time is less than 200ms"] = responseTime < 200;
響應時間在特定範圍內(包含下限,上限不包括)
tests["Response time is acceptable"] = _.inRange(responseTime, 100, 1001); // _ is the inbuilt Lodash v3.10.1 object, documented at https://lodash.com/docs/3.10.1
狀態代碼是200
tests["Status code is 200"] = responseCode.code === 200;
代碼名稱包含一個字符串
tests["Status code name has string"] = responseCode.name.has("Created");
成功的POST請求狀態代碼
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
使用TinyValidator獲取JSON數據
var schema = {
"items": {
"type": "boolean"
}};var data1 = [true, false];var data2 = [true, 123];
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
console.log("Validation failed: ", tv4.error);
解碼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
tests["Contents are valid"] = CryptoJS.enc.Utf8.stringify(intermediate); // a check for non-e