0
0
Postmantesting~15 mins

Data file integration (CSV, JSON) in Postman - Build an Automation Script

Choose your learning style9 modes available
Run API tests using data file integration with CSV and JSON in Postman
Preconditions (3)
Step 1: Open Postman and create a new collection
Step 2: Add a new request to the collection with the API endpoint
Step 3: Set the HTTP method and required headers
Step 4: Click on the Runner button to open the Collection Runner
Step 5: Select the collection containing the request
Step 6: Click on 'Select File' and upload the CSV data file
Step 7: Start the run and observe the results
Step 8: Repeat the run using the JSON data file instead of CSV
Step 9: Verify that each request uses data from the file and responses are as expected
✅ Expected Result: The API requests run multiple times using each row/object from the CSV and JSON files. Each response matches the expected output for the input data.
Automation Requirements - Postman Collection Runner
Assertions Needed:
Verify response status code is 200 for each request
Verify response body contains expected data matching input
Verify all data rows/objects from the file are used
Best Practices:
Use variable placeholders in the request URL, headers, or body to inject data
Use pre-request scripts or tests to validate data-driven execution
Keep data files clean and well-structured
Use descriptive names for variables and data columns/keys
Automated Solution
Postman
/* 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.

Common Mistakes - 4 Pitfalls
Not using variable placeholders in the request to inject data
Uploading data file with incorrect format or missing headers/keys
{'mistake': 'Not verifying response data against input data', 'why_bad': 'Tests may pass without confirming the API processed the input correctly, missing bugs.', 'correct_approach': "Write assertions comparing response fields to input data using pm.iterationData.get('variableName')."}
Running tests without using Collection Runner
Bonus Challenge

Now add data-driven testing with 3 different inputs using both CSV and JSON files

Show Hint