Learn how to do data-driven testing with different data sets for each request in Postman. This video will show you how to use Postman's variables and collections to automate your testing and ensure that your application is working as expected for all possible data inputs.

 

In this tutorial, we will take a look at how to use different data sets for different requests when you are doing data-driven testing in Postman.

combined-data.json

[
    {
        "requests": [
            {
                "name": "Create employee",
                "data": [
                    {
                        "firstName": "Jessica",
                        "lastName": "Quinn"
                    },
                    {
                        "firstName": "James",
                        "lastName": "Dan"
                    },
                    {
                        "firstName": "Maria",
                        "lastName": "Nelson"
                    }    
                ]
            },
            {
                "name": "Create job position",
                "data": [
                    {
                        "position": "Developer",
                        "company": "Google"
                    },
                    {
                        "position": "QA",
                        "company": "Facebook"
                    },
                    {
                        "position": "Manager",
                        "company": "Amazon"
                    },
                    {
                        "position": "Store Manager",
                        "company": "Walmart"
                    },
                    {
                        "position": "Manager",
                        "company": "Amazon"
                    }
                ]
            }
        ]
    }
]

My company API.postman_collection.json

{
	"info": {
		"_postman_id": "f28b954b-1ace-4cf1-896c-378d8435ba29",
		"name": "My company API",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "Create employee",
			"event": [
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							""
						],
						"type": "text/javascript"
					}
				},
				{
					"listen": "test",
					"script": {
						"exec": [
							"pm.test(\"Check args\", () => {",
							"    const response = pm.response.json();",
							"    pm.expect(response.args.fName).to.eql(pm.variables.get('firstName'));",
							"    pm.expect(response.args.lName).to.eql(pm.variables.get('lastName'));",
							"});"
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [],
				"url": {
					"raw": "{{baseUrl}}/post?fName={{firstName}}&lName={{lastName}}",
					"host": [
						"{{baseUrl}}"
					],
					"path": [
						"post"
					],
					"query": [
						{
							"key": "fName",
							"value": "{{firstName}}"
						},
						{
							"key": "lName",
							"value": "{{lastName}}"
						}
					]
				}
			},
			"response": []
		},
		{
			"name": "Get reference number",
			"event": [
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "GET",
				"header": [],
				"url": {
					"raw": "httpbin.org/uuid",
					"host": [
						"httpbin",
						"org"
					],
					"path": [
						"uuid"
					]
				}
			},
			"response": []
		},
		{
			"name": "Create job position",
			"event": [
				{
					"listen": "test",
					"script": {
						"exec": [
							"pm.test(\"Check args\", () => {",
							"    const response = pm.response.json();",
							"    pm.expect(response.args.jobPos).to.eql(pm.variables.get('position'));",
							"});"
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [],
				"url": {
					"raw": "httpbin.org/post?jobPos={{position}}",
					"host": [
						"httpbin",
						"org"
					],
					"path": [
						"post"
					],
					"query": [
						{
							"key": "jobPos",
							"value": "{{position}}"
						}
					]
				}
			},
			"response": []
		}
	],
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"exec": [
					"// Load data from file",
					"if (typeof pm.variables.get('requestsData') !== 'object') {",
					"    pm.variables.set('requestsData', pm.iterationData.toObject());",
					"}",
					"const requestsData = pm.variables.get('requestsData');",
					"",
					"if (typeof requestsData !== 'object' || Object.keys(requestsData).length === 0) {",
					"    console.log('No external data provided or object is empty.');",
					"    return;",
					"}",
					"",
					"// Find the current request",
					"const currentRequest = requestsData.requests.filter(({name}) => name === pm.info.requestName)[0];",
					"",
					"// Skip the rest since we have no data",
					"if (!currentRequest) {",
					"    console.log(`Request ${pm.info.requestName} has no data defined.`);",
					"    return;",
					"}",
					"",
					"// Expose variables",
					"const variables = currentRequest.data.shift();",
					"",
					"for (const [key,value] of Object.entries(variables)) {",
					"    pm.variables.set(key, value);",
					"}",
					"",
					"pm.variables.set('requestsData', requestsData);",
					"",
					"// Decide where to go next",
					"if (currentRequest.data.length > 0) {",
					"    postman.setNextRequest(pm.info.requestName);",
					"}"
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"key": "baseUrl",
			"value": "httpbin.org"
		},
		{
			"key": "firstName",
			"value": "Foo"
		},
		{
			"key": "lastName",
			"value": "Bar"
		},
		{
			"key": "position",
			"value": "Some job"
		}
	]

 

The Postman collection and the data-set are available here:

https://gist.github.com/vdespa/6680d41bfc969f1853b0fb546df34c04

#

Data-Driven Testing with Different Data Sets for Each Request-Postman
1.00 GEEK