Data driven testing per request without using data file

Adding another answer on how to run data driven from same request:

Create a environment variable called “csv” and copy the below content and paste it as value:

username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1

Now in pre-request add :

if (!pm.variables.get("index")) {

    const parse = require('csv-parse/lib/sync')
    //Environmental variable where we copy-pasted the csv content
    const input = pm.environment.get("csv");
    const records = parse(input, {
        columns: true,
        skip_empty_lines: true
    })

    pm.variables.set("index", 0)
    pm.variables.set("records", records)
}

records = pm.variables.get("records")
index = pm.variables.get("index")
if (index !== records.length) {
    for (let i of Object.entries(records[index])) {
        pm.variables.set(i[0], i[1])
    }
    pm.variables.set("index", ++index)
    postman.setNextRequest(pm.info.requestName)
}

Now you can run data driven for that one particular request:

Eg collection:

https://www.getpostman.com/collections/eb144d613b7becb22482

use the same data as environment variable content , now run the collection using collection runner or newman

Output:

4 Likes

Hi @praveendvd ,

This looks interesting, will try this for sure. Thank you for sharing :relaxed:

1 Like

Thank you :blush: let me know your thoughts

Please complete by added this line in body
image

you have add this line of code other wise it will duplicate in the end

image