Newman cant update new values to postman environment variables

Hello,
Im calling postman collection LOCALLY through newman and it cant update new values for environment variables. My code is below:

var sum = 250;
var totalPaidAmount = Number(pm.collectionVariables.get("ContractPaidAmount"));
var sumOfContractTotalPaidAmount = totalPaidAmount + sum;
pm.environment.set("ContractPaidAmount",sumOfContractTotalPaidAmount);
pm.collectionVariables.set("ContractPaidAmount",sumOfContractTotalPaidAmount);

whether it is possible at all to do these things in newman?
Already integrated postman tests through newman in Azure CI/CD and thought that impossible to newman modify file values without my manual interaction…

Hi @ciapas.laurynas

Using the same logic you shared above, I ran the following test collection in Newman with a pre-req script that updated my environment’s file and it appears to work.

Here is what I did;

Generated a random number between 1 and 36 and used the environment.set to save the variable.
image

Then set the variable where I needed the random number to be fed in.
image

Then ran it in Newman, with the -e option for including my environments file. It ran for 5 iterations and as you can see the page number changes for each.


When you run your code with logging, you get NaN (not a number)

This is because your second line is equal to “Number(A NUMBER)” … which is not a number. I suspect this line should be;

var totalPaidAmount = pm.collectionVariables.get("ContractPaidAmount");

Hello,
thanks for the advice, I have fixed it. It works only in the current session, current environment variable values updates in the current session. Example:


But if i run tests in another time, all variables has last EXPORTED collection values and it loads i guess initial environment variables values. How can i update initial environment values? My variables should keep information from the last run.

Try looking into the --export-environment option;

–export-collection
The path to the file where Newman will output the final collection file before completing a run.

Newman options can be found here;

1 Like

Thanks,
locally it works!