Scripting the reset of collection variable to its initial value

Our team has established a set of collection variables used for testing. The initial value for the variables is usually the happy path values. We then script tests. For non-happy path tests we set the collection variable(s) to different values (which become current values). After the tests complete we want to add to the script a line of code that resets the current value back to the initial value. We know we can explicitly set the variable value however this is a maintenance nightmare if we ever change the default values for the variables. In a perfect world there would be a method such as the following:

pm.collectionVariables.reset(ā€œvehicleSlugā€, ā€œ6BEB239E-9A76-4F69-B0B3-62643F0FE0F9ā€);

I have tried unset() and clear() but unset() destroys the variable altogether and clear() simply clears the value. Is there a way to request the ā€œinitial valueā€ of a variable? If so I will just set() the value that way.

Thanks in advance for your assistance.

1 Like

@ScottFinkle if you donā€™t want collection variables to get affected you can use local variables

pm.variables.set and pm.variables.get

it will get the collection variable value also according to precedence

meaning

if you call pm.variables.get(ā€œnameā€) , then it will check for the lowest scope value for that variable . so if no lcoal variable if defined then it will get value from data variable , if no data varaible ā€œnameā€ is defined then environment variable and so on

if you still want to reset then use :

Add this to first requestā€™s pre-request script:

 pm.environment.set("resetValue",pm.collectionVariables.toJSON().values)

This wiil store all initial value to resetValue variable

And in the last requestā€™s test script use

pm.environment.get("resetValue").forEach((a)=>pm.collectionVariables.set(a.key,a.value))

Now we are resetting all collectionVariables to the initially saved value

1 Like

I sometimes observe ā€˜nullā€™ when applying this and I havenā€™t been able to figure out why. Only for the variable Iā€™ve just set/updated via the request body to test this, though. All my other environment variables adjusted during the course of the test run are correctly reset to initial value.

My variable: firstName : Peter | Peter
requestbody: firstName: John
responsebody: firstName: John set firstName to jsonData.firstName
Observed: firstName : Peter | null
Expect: firstname: Peter | Peter

I donā€™t know the precise condition to reproduce it. Do you know why it might sometimes set current value to ā€˜nullā€™ (aside from the obvious: initial valu: null)?

is first request being send multiple time in your setup or only once ?

in the first request add:

 pm.environment.get("resetValue") ? null:pm.variables.set("resetValue",pm.collectionVariables.toJSON().values)

Here we store data only if resetValue variable is not set

And in the last request:

 pm.environment.get("resetValue").forEach((a)=>pm.collectionVariables.set(a.key,a.value))
 pm.environment.unset("resetValue")

This make sure you donā€™t set resetValue incorrectly if you are running first request multiple time

you can also use pm.variables if you donā€™t any variable to be affected , thats the better way

1 Like

only being sent once. I have multiple requests in between the first and last, however. itā€™s an end-to-end test and there are many dependencies to get to the final requestā€™s tests.

Thanks for the additional details. Iā€™ll give that a shot!

1 Like

Ya make sure the first request runs only once and the last request always gets executed with the reset step

Just wanted to follow up and let you know I havenā€™t had further issues with the occasional ā€˜nullā€™ or empty restored value with the new code. Thank you!! (Could have never figured this out myself!)

2 Likes

:heart_eyes: :smiling_face_with_three_hearts: Glad to hear back and thanks for letting the community know .

Hello, Iā€™m curious if there is a way of doing this variable reset within a Postman Monitor (given you canā€™t use environment ā€˜setā€™ functions in a monitor execution). Iā€™m currently using pm.sendRequest to PUT a bearer token variable into the environment, but that value only populates the initial value field.

1 Like