# Data-driven testing with different data sets for each request

**URL:** <https://community.postman.com/t/data-driven-testing-with-different-data-sets-for-each-request/33491>\
**Category:** Help Hub\
**Created:** [March 15, 2022, 11:12am UTC](https://community.postman.com/t/data-driven-testing-with-different-data-sets-for-each-request/33491 "2022-03-15T11:12:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [March 15, 2022, 11:12am UTC](https://community.postman.com/t/data-driven-testing-with-different-data-sets-for-each-request/33491/1 "2022-03-15T11:12:42Z")

</div>

Hi @vdespa  
Hope you are doing well.  
I have watched your video on [YouTube](https://www.youtube.com/watch?v=MOdMKrjTOi4). You deliver great content. Now I need your help. Here I have to make some changes in your pre-request script.  
I have CSV file with a lot of request data(rows), and columns as variables.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/b/7b431baf684d70badec2ca843ca98642aca398af.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/6/d673146f2e9bd6ceba46ffbd4dc73e064067d169.png)

First request should be run one time(has no data defined), second request should get data from the file and iterate with count of it. Then again the same . Collection will contain many requests, but the main logic is the same.

```auto
// 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);
}

```

This is your pre-request script from the mentioned YouTube video, how can I change and use it in my case? I am not much experienced that is why I expect your help 🙃

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [March 15, 2022, 12:08pm UTC](https://community.postman.com/t/data-driven-testing-with-different-data-sets-for-each-request/33491/2 "2022-03-15T12:08:36Z")

</div>

Handling such scripts is not easy.

I recommend understanding the concepts around JavaScript, Postman variables and Postman workflows.

Begin with some JavaScript basics:

[![](https://img.youtube.com/vi/juuhb3W8xT4/maxresdefault.jpg "Learn JavaScript for Postman API testing - Full Course for Beginners") ](https://www.youtube.com/watch?v=juuhb3W8xT4)

---

<div class="post-metadata">

**Author:** ![harutharut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/harutharut/32/6996_2.png) [@harutharut](https://community.postman.com/u/harutharut)\
**Post date:** [March 15, 2022, 12:31pm UTC](https://community.postman.com/t/data-driven-testing-with-different-data-sets-for-each-request/33491/3 "2022-03-15T12:31:26Z")

</div>

Okay, thanks  
I will try to do my best and will let you know about my progress.
