EDIT: I’ve attached an example collection/environment to see it in action.
Just click the Download link at the top and import them into Postman.
Postman Collection: { "info": { "_postman_id": "ae24430b-d0c0-4b0c-91ff-90a060d4611b", "name - Pastebin.com
Postman Environment: { "id": "4d43c020-4c8c-43d8-b1ae-acaaecf7bd40", "name": "Restful Booker", - Pastebin.com
For me, keeping your tests DRY in Postman is one of the biggest challenges and it’s something that I’ve changed every year. My talk at the Postman Customer Summit in London (I think it was back in February/March time) was about this and I’m how I’m “faking” BeforeEach hooks in Postman to stop the duplication of requests.
Using the Restful Booker API that’s available for testers, I done a little proof of concept for your needs (Bear in mind, I just bashed this out in less than 30 mins after a long day of work + 1 hour commute! Ok excuses out of the way)
So! Let’s say I have this “booking” POST request that creates a booking.
I want to change the properties used in each request body that is sent.
Here’s my collection variable. I guess this can be called the request body’s “natural state” ??? So if I decide to not change anything, this is what will be POST-ed.
And I can use it in my actual POST request like this:
In the Pre-request tab for my first request, I’ll change the “firstname” value to something else.
As I said before, I bashed this out in a couple of minutes so I’m willing to bet there’s a MUCH nicer/cleaner way of updating a property value from an object that is also an environment variable…but hey, this does exactly what I want it to *shrugs
If I now send this request, the modified request body will be sent and this is confirmed in the request body returned from the POST /booking endpoint:
The cool thing about collection variables is that every request our request body will be re-assigned so we don’t have to worry about this variable being constantly mutated with every request which is nice.
If you want to test you’re always getting a 201 status code back with this POST request and assert the schema etc. you can even add these re-usable tests at the collection level.
If your tests have a pre-requisite like a “Register User” request, feel free to have a look at my code examples from faking Before/BeforeEach hooks in Postman.
Hopefully, you were able to get some value from this reply.