GUI で API をテストするツールとしては、Postman がかなり使われているようです。
Postman で作成した設定を CLI で実行可能になる Newman や、OpenAPI の仕様書から Postman の設定を生成する swagger2-to-postman を組み合わせることで、CI に組み込むことも可能とのことです。
今回はGUIのPostmanでテストケースを作成し、NewmanでCLIでの実行方法を紹介します。
Postmanの利用
Postmanのtest script
pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([200]);
});
var schema ={
“$schema”: “http://json-schema.org/draft-07/schema”,
“$id”: “http://example.com/example.json”,
“type”: “object”,
“required”: [
“status”
],
“properties”: {
“status”: {
“$id”: “#/properties/status”,
“type”: “object”,
“required”: [
“code”,
“text”
],
“properties”: {
“code”: {
“$id”: “#/properties/status/properties/code”,
“type”: “string”
},
“text”: {
“$id”: “#/properties/status/properties/text”,
“type”: “string”
}
}
}
}
};
var jsonResultsss = pm.response.json();
pm.test(‘Schema is valid’, function () {
pm.expect(tv4.validate(jsonResultsss, schema)).to.be.true;
});
pm.test(“status test”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.status.code).to.eql(“{{code}}”);
pm.expect(jsonData.status.text).to.eql(“{{text}}”);
});
Collectionsのエクスポート
newmanの利用
Postmanで作成したテストケースとテストデータ(csv,json)を指定したディレクトにいれます。
-rw-r–r–. 1 root root 305 5月 26 2021 test_date1.csv
-rw-r–r–. 1 root root 3316 5月 26 2021 auto_test.postman_collection.json
-rw-r–r–. 1 root root 3316 5月 26 2021 auto_test.postman_collection.json
実行コマンド:
$ newman run auto_test.postman_collection.json -d test_date1.csv