postman testsの実例記録


この间テストアプリのインタフェースを用意していますが、postmanというツールはとても便利です.特に中のtestsのjavascript脚本です.
テストインターフェースでよく使われるtests検証の例を記録してください.
1.環境変数の設定
postman.setEnvironmentVariable("key", "value");
2.ネストを環境変数に独自に楽しむ
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));
3.環境変数の取得
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"));
4.環境変数をクリアする
postman.clearEnvironmentVariable("key");
5.グローバル変数を設定する
postman.setGlobalVariable("key", "value");
6.グローバル変数を取得する
postman.getGlobalVariable("key");
7.グローバル変数をクリアする
postman.clearGlobalVariable("key");
8.レスポンスに文字列が含まれているかどうかを確認する
tests["Body matches string"] = responseBody.has("string_you_want_to_search");
9.xmlをJSONオブジェクトに変換する
var jsonObject = xml2Json(responseBody);
10.レスポンスが文字列に等しいかどうかを確認する
tests["Body is correct"] = responseBody === "response_body_string";
11.JSON値をチェックする
var data = JSON.parse(responseBody);
tests["Your test name"] = data.value === 100;
12.コンテンツタイプが存在する(大文字と小文字の区別がない検査)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.
13.コンテンツタイプの存在(大文字と小文字の区別)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
14.応答時間が200 ms未満である.
tests["Response time is less than 200ms"] = responseTime < 200;
15.応答時間は、下限と上限を含む特定の範囲内である.
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
16.ステータスコードは200です.
tests["Status code is 200"] = responseCode.code === 200;
17.コードには文字列が含まれています.
tests["Status code name has string"] = responseCode.name.has("Created");
18.成功したPOST要求状態コード
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
19.JSONデータにTinyValidatorを使用する
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);
20.base 64符号化データを復号する
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-emptiness
公式文書の英文版の転送ゲート:
https://www.getpostman.com/docs/postman/scripts/test_examples