zl程序教程

您现在的位置是:首页 >  其他

当前栏目

[Postman & Integration Tests] Using Postman to Define Integration Tests

amppostman to Using define Integration tests
2023-09-14 09:00:46 时间

In Postman, you can create a collection of API requests. Those collection can be save into json file as well.

{
	"info": {
		"_postman_id": "9c5a8003-1c58-4207-b843-85f2355aaf4a",
		"name": "udacity-c2-basic-server",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "exercise",
			"item": [
				{
					"name": "/cars all",
					"request": {
						"method": "GET",
						"header": [],
						"body": {
							"mode": "raw",
							"raw": ""
						},
						"url": {
							"raw": "http://localhost:8082/cars/",
							"protocol": "http",
							"host": [
								"localhost"
							],
							"port": "8082",
							"path": [
								"cars",
								""
							]
						}
					},
					"response": []
				},
				{
					"name": "/cars filter",
					"request": {
						"method": "GET",
						"header": [],
						"body": {
							"mode": "raw",
							"raw": ""
						},
						"url": {
							"raw": "http://localhost:8082/cars/?make=toyota",
							"protocol": "http",
							"host": [
								"localhost"
							],
							"port": "8082",
							"path": [
								"cars",
								""
							],
							"query": [
								{
									"key": "make",
									"value": "toyota"
								}
							]
						}
					},
					"response": []
				},
				{
					"name": "/cars by id",
					"request": {
						"method": "GET",
						"header": [
							{
								"key": "Content-Type",
								"name": "Content-Type",
								"value": "application/json",
								"type": "text"
							}
						],
						"body": {
							"mode": "raw",
							"raw": ""
						},
						"url": {
							"raw": "http://localhost:8082/cars/2",
							"protocol": "http",
							"host": [
								"localhost"
							],
							"port": "8082",
							"path": [
								"cars",
								"2"
							]
						}
					},
					"response": []
				},
				{
					"name": "/cars",
					"request": {
						"method": "POST",
						"header": [
							{
								"key": "Content-Type",
								"name": "Content-Type",
								"value": "application/json",
								"type": "text"
							}
						],
						"body": {
							"mode": "raw",
							"raw": "{ \"make\": \"ford\", \"type\": \"sedan\", \"model\": \"focus\", \"cost\": 12, \"id\": 9 }\n"
						},
						"url": {
							"raw": "http://localhost:8082/cars",
							"protocol": "http",
							"host": [
								"localhost"
							],
							"port": "8082",
							"path": [
								"cars"
							]
						}
					},
					"response": []
				}
			]
		},
		{
			"name": "http://localhost:8082/",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "9b4015b7-faf5-4924-8661-7728ba94e410",
						"exec": [
							"pm.test(\"Status code is 200\", function () {",
							"    pm.response.to.have.status(200);",
							"});",
							"",
							"pm.test(\"Body matches string\", function () {",
							"    pm.expect(pm.response.text()).to.include(\"Welcome to the Cloud!\");",
							"});"
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"body": {
					"mode": "raw",
					"raw": ""
				},
				"url": {
					"raw": "http://localhost:8082/",
					"protocol": "http",
					"host": [
						"localhost"
					],
					"port": "8082",
					"path": [
						""
					]
				}
			},
			"response": []
		},
		{
			"name": "http://localhost:8082/persons/george",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "277a3edb-b69d-4dcd-9b13-00d00faaca25",
						"exec": [
							"pm.test(\"Status code is 200\", function () {",
							"    pm.response.to.have.status(200);",
							"});",
							"",
							"pm.test(\"Body matches string\", function () {",
							"    pm.expect(pm.response.text()).to.include(`Welcome to the Cloud, george!`);",
							"});",
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"body": {
					"mode": "raw",
					"raw": ""
				},
				"url": {
					"raw": "http://localhost:8082/persons/:name",
					"protocol": "http",
					"host": [
						"localhost"
					],
					"port": "8082",
					"path": [
						"persons",
						":name"
					],
					"variable": [
						{
							"key": "name",
							"value": "george"
						}
					]
				}
			},
			"response": []
		},
		{
			"name": "http://localhost:8082/persons/:name Copy",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "7df7eec7-78b4-46f4-b2f5-2a7b994747e9",
						"exec": [
							"pm.test(\"Status code is 200\", function () {",
							"    pm.response.to.have.status(200);",
							"});",
							"",
							"pm.test(\"Body matches string\", function () {",
							"    var query = {};",
							"    pm.request.url.query.all().forEach((p) => { query[p.key] = p.value});",
							"    ",
							"    pm.expect(pm.response.text()).to.include(`Welcome to the Cloud, ${query['name']}!`);",
							"});",
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"body": {
					"mode": "raw",
					"raw": ""
				},
				"url": {
					"raw": "http://localhost:8082/persons?name=george",
					"protocol": "http",
					"host": [
						"localhost"
					],
					"port": "8082",
					"path": [
						"persons"
					],
					"query": [
						{
							"key": "name",
							"value": "george"
						}
					]
				}
			},
			"response": []
		},
		{
			"name": "http://localhost:8082/persons/",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "23433ca1-6ffc-4149-9e3c-97514d889ac5",
						"exec": [
							"pm.test(\"Status code is 200\", function () {",
							"    pm.response.to.have.status(200);",
							"});",
							"",
							"pm.test(\"Body matches string\", function () {",
							"    pm.expect(pm.response.text()).to.include(`Welcome to the Cloud, james!`);",
							"});",
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [
					{
						"key": "Content-Type",
						"name": "Content-Type",
						"value": "application/json",
						"type": "text"
					}
				],
				"body": {
					"mode": "raw",
					"raw": "{\n    \"name\": \"james\"\n}"
				},
				"url": {
					"raw": "http://localhost:8082/persons",
					"protocol": "http",
					"host": [
						"localhost"
					],
					"port": "8082",
					"path": [
						"persons"
					]
				}
			},
			"response": []
		},
		{
			"name": "400 http://localhost:8082/persons/:name",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "444e71e3-ae8c-469a-9da1-22aa3ee0792d",
						"exec": [
							"pm.test(\"Status code is 400\", function () {",
							"    pm.response.to.have.status(400);",
							"});"
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"body": {
					"mode": "raw",
					"raw": ""
				},
				"url": {
					"raw": "http://localhost:8082/persons/",
					"protocol": "http",
					"host": [
						"localhost"
					],
					"port": "8082",
					"path": [
						"persons",
						""
					]
				}
			},
			"response": []
		}
	],
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"id": "6f442aeb-7c6b-401b-a76d-03a2c7b2cca9",
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"id": "fae73fa3-4d3b-4528-a1af-a150ef18a902",
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"id": "0db39ab1-774e-4108-8078-7a4d7df27657",
			"key": "endpoint",
			"value": "http://localhost:8082",
			"type": "string"
		}
	]
}

  

You can run the test against each request:

 

Also you can run the test against all the requests: