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