Unable to get reference CSV file for JS Tests

tests
collections
csvdatafile

#1

I am using a REST API with a POST request. I have created a CSV file to load in various inputs and using the Collection Runner to submit my requests and run the associated JS Tests iteratively. I am trying to figure out how I can also have an entry in each row of the CSV to reference for my JS Test in order to make the JS dynamic. I’ve searched the POSTMAN documentation and forums, as well as Google and Stackoverflow, but haven’t found anything that works. Here is a basic example of what I’m trying to accomplish.

Let’s say I have a basic adding API. Here is my Request:
{
“Numbers”: {
“Value_1”: {{val1}},
“Value_2”: {{val2}},
}
}

The CSV file is as follows.
val1,val2,sum
1,1,2
2,2,4
3,3,6

For this example, lets assume that the API returns a response that includes the sum of val1 and val2; something like this:
{
“Numbers”: {{sum}},
}

I am able to load val1 and val2 into my request and iterate through the request for each row, but I am having trouble incorporating the sum values (from the same CSV) into the JS Test.

I am trying to do something like the test below where I can reference the sum value from my spreadsheet, but Postman doesn’t like my syntax.

pm.test(“Adding machine”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.Numbers === {{sum}});
});

Does anyone have any suggestions? Is this even possible to do?


#2

@Kamoshin You cannot use {{variable_name}} syntax in the pre-request / test scripts.

Refer the following:

You’ve to use pm.variables / pm.globals / pm.environment api according to where you’re storing the value of the variable to get the value of the variable in the test script.

If you’re taking the value of ‘sum’ from CSV file then you’ve to use the following syntax:

let sum = data.sum;

or you can also use the following syntax

let sum =  pm.iterationData.get('sum');

Refer the docs here.

Then you can use sum in your test.