Skip to content

12 Data Driven Testing

DodaTech 3 min read

title: "Data-Driven Testing with CSV and JSON" description: "Run Postman collections with data files for parameterized testing. Learn CSV and JSON data file formats, iteration variables, data-driven assertions, and bulk testing with multiple test scenarios." weight: 12 date: 2026-06-28 lastmod: 2026-06-28 tags: [api-development, postman] }

Data-driven testing runs the same requests with different input data from CSV or JSON files. Each row or object represents one test iteration. Variables from the data file are available as {{variable}} in requests and scripts.

What You'll Learn

  • CSV data file format and structure
  • JSON data file format
  • Accessing data variables in scripts
  • Data-driven assertions
  • Bulk testing with Newman

Why It Matters

Data-driven tests cover more scenarios without duplicating requests. A single create-user request with 100 data rows tests 100 different user combinations. Data files make tests maintainable and comprehensive.

Real-World Use

Payment API tests use CSV files with different card numbers, amounts, and currencies. User registration tests use data files with valid and invalid inputs. Collection runners use data files for regression testing.

flowchart LR
    Collection[Postman Collection] --> Runner[Collection Runner]
    DataFile[Data File CSV/JSON] --> Runner
    Runner --> Iteration1[Iteration 1: Row 1]
    Runner --> Iteration2[Iteration 2: Row 2]
    Runner --> Iteration3[Iteration 3: Row 3]
    Iteration1 --> Request[API Request with Row 1 Data]
    Iteration2 --> Request2[API Request with Row 2 Data]

Teacher Mindset

Design data files with columns matching request variables. Use descriptive row names for easy debugging. Include positive and negative test cases. Validate all data-driven iterations produce expected results.

Code Examples

// Example 1: CSV data file
name,email,role,expected_status
Alice Johnson,alice@test.com,admin,201
Bob Smith,bob@test.com,user,201
,bad-email,user,422
Charlie,charlie@test.com,,422
David,david@test.com,unknown_role,422
// Example 2: JSON data file
[
  {
    "name": "Alice Johnson",
    "email": "alice@test.com",
    "role": "admin",
    "expected_status": 201
  },
  {
    "name": "Bob Smith",
    "email": "bob@test.com",
    "role": "user",
    "expected_status": 201
  },
  {
    "name": "",
    "email": "bad-email",
    "role": "user",
    "expected_status": 422
  }
]
// Example 3: Test script using data variables
const name = pm.iterationData.get('name');
const email = pm.iterationData.get('email');
const role = pm.iterationData.get('role');
const expectedStatus = parseInt(pm.iterationData.get('expected_status'));

pm.test(`[${name}] Status is ${expectedStatus}`, () => {
    pm.response.to.have.status(expectedStatus);
});

pm.test(`[${name}] Response matches expected`, () => {
    if (expectedStatus === 201) {
        const body = pm.response.json();
        pm.expect(body.name).to.eql(name);
        pm.expect(body.email).to.eql(email);
        pm.expect(body.role).to.eql(role || 'user');
    } else if (expectedStatus === 422) {
        pm.expect(pm.response.json()).to.have.property('errors');
    }
});

console.log(`Iteration: ${name}, Status: ${pm.response.code}`);

Common Mistakes

  • Forgetting to match CSV column names exactly to variable names
  • Not including header row in the CSV file
  • Using data files with inconsistent row structures
  • Not specifying expected outcomes in the data file
  • Running data-driven tests without checking iteration counts

Practice

  1. Create a CSV data file with 5 test cases for user creation (3 valid, 2 invalid).
  2. Write a test script that reads data variables and validates responses.
  3. Run the collection with the data file in the collection runner.
  4. Create a JSON data file version of the same test cases.
  5. Challenge: Create a data-driven test for a search endpoint with different query terms, filters, and expected result counts.

FAQ

What file formats does Postman support for data-driven testing?

CSV and JSON. CSV is simpler for tabular data. JSON supports nested objects.

How do I access data variables in scripts?

Use pm.iterationData.get('column_name') for CSV or pm.iterationData.get('key') for JSON.

Can I mix data variables with other variable scopes?

Yes. Data variables have the highest precedence, overriding global, environment, and collection variables.

What happens if a data file has more rows than iterations?

The collection runner uses the iteration count setting. Data rows beyond the limit are ignored.

How do I debug data-driven test failures?

Use console.log to print iteration data and response. Check which iteration failed in the runner results.

Mini Project

Create a data-driven Postman collection for user registration. Design a CSV file with columns: name, email, password, role, expected_status. Write test scripts that validate each iteration. Include at least 10 test cases covering: valid users, missing fields, invalid email, duplicate email, short password, and invalid role.

What's Next

Next, you will learn about running collections with the Collection Runner.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro