Data file integration (CSV, JSON) in Postman - Build an Automation Script
/* Postman test script example for data-driven test using CSV or JSON */ // In the request body or URL, use variables like {{username}}, {{password}} // Example test script to verify response pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); pm.test('Response contains username', () => { const jsonData = pm.response.json(); pm.expect(jsonData.username).to.eql(pm.iterationData.get('username')); }); // No additional code needed to load data files; handled by Collection Runner // To run: // 1. Open Collection Runner // 2. Select collection // 3. Select CSV or JSON file with columns/keys like username, password // 4. Run and observe test results for each data row/object // Example CSV file content: // username,password // user1,pass1 // user2,pass2 // Example JSON file content: // [ // {"username": "user1", "password": "pass1"}, // {"username": "user2", "password": "pass2"} // ]
This Postman test script uses variable placeholders like {{username}} and {{password}} in the request to inject data from the CSV or JSON file during the Collection Runner execution.
The test script verifies that the response status code is 200 and that the response body contains the expected username matching the input data for each iteration.
The Collection Runner automatically reads the data file and runs the request once per data row (CSV) or object (JSON), replacing variables accordingly.
This approach keeps the test simple and reusable for any data file with matching keys.
Now add data-driven testing with 3 different inputs using both CSV and JSON files
Practice
Solution
Step 1: Understand data file role in Postman
Data files provide different sets of input data for tests.Step 2: Connect data files to Collection Runner
Collection Runner uses these files to repeat tests with varied inputs.Final Answer:
To run the same test multiple times with different input data -> Option AQuick Check:
Data files = multiple test runs [OK]
- Thinking data files create APIs
- Confusing data files with UI settings
- Assuming data files store results
username in a Postman test script?Solution
Step 1: Identify how Postman accesses data file variables
Postman usespm.iterationData.get('variableName')to get data from CSV or JSON files.Step 2: Match the correct syntax for
The correct method isusernamepm.iterationData.get('username').Final Answer:
pm.iterationData.get('username') -> Option CQuick Check:
Use pm.iterationData.get() for data file variables [OK]
- Using pm.variables.get() which accesses environment variables
- Trying to access data as object properties
- Confusing request body with data file variables
email,password user1@example.com,pass123 user2@example.com,pass456
And this test script snippet:
const email = pm.iterationData.get('email');
const password = pm.iterationData.get('password');
pm.test('Check email format', () => {
pm.expect(email).to.include('@');
});What will be the result of running the Collection Runner with this data file?
Solution
Step 1: Analyze CSV data values
Both emails contain '@' symbol: 'user1@example.com' and 'user2@example.com'.Step 2: Check test condition for each iteration
The test checks if email includes '@', which is true for both rows.Final Answer:
Both iterations pass the test -> Option AQuick Check:
Email includes '@' = true for all data [OK]
- Assuming test fails without checking data
- Confusing syntax error with logic error
- Ignoring multiple iterations in Collection Runner
[
{"user": "alice", "age": 30},
{"user": "bob", "age": "twenty-five"}
]When running tests that expect
age to be a number, you get unexpected failures. What is the most likely cause?Solution
Step 1: Inspect JSON data values
The first age is 30 (number), second is "twenty-five" (string).Step 2: Understand test expectation
Tests expect age to be a number, but second value is text, causing failure.Final Answer:
The second age value is a string, not a number -> Option BQuick Check:
Data type mismatch causes test failure [OK]
- Assuming JSON format is invalid
- Blaming Postman for reading JSON
- Ignoring data type differences
[
{"user": {"name": "John", "id": 101}, "active": true},
{"user": {"name": "Jane", "id": 102}, "active": false}
]Which is the correct way to access the
name property inside the test script?Solution
Step 1: Understand how Postman accesses nested JSON data
Postman requires getting the parent object first withpm.iterationData.get('user'), then access nested properties using JavaScript dot notation.Step 2: Identify correct syntax for nested property
Usingpm.iterationData.get('user').namecorrectly accesses the nestedname.Final Answer:
pm.iterationData.get('user').name -> Option DQuick Check:
Get parent object first, then .name [OK]
- Trying to access nested objects as JS objects directly
- Using .get() on returned object
- Using bracket notation inside pm.iterationData.get()
Ready to prove it?
Pass the test and this pattern moves unknown
