Implement "hooks" to Postman to improve maintainability of test collections (Before, BeforeEach, AfterEach etc.)

tests
improvements
dry
hooks

#1

Along with merging/forking collections (which has been released as of 6.7.0!), I always wished Postman had the ability to use “hooks”.

The absence of merging/forking and no hooks were one of the main reasons we moved away from Postman in my last place of work, as maintaining tests was a nightmare.

Let’s say we have a test collection that is testing a couple of scenarios.
In this example:

  • I’m logging in as User,
  • Performing a product search
  • Adding this item to my bag
  • Checking out using a specific region

Notice how there is a lot of repetition where I’ve duplicated the same requests.
Ideally, I’d like to keep my tests DRY (Don’t Repeat Yourself)
In its current state, it is already causing too much maintenance if something were to change with the Login, Search or Add to Bag requests.

I know if we click the “Edit” button on a collection/sub-collection we get the below window which allows us to run code BEFORE EACH request.

Ideally, in the same window, I’d like to specify to run a bunch of requests:

  • Before all requests (These requests will run first in the collection/sub-collection)
  • Before Each request (These requests will run before each request in the collection/sub-collection)

This way, I’ll only have to maintain the “Login, Search and Add To Bag” in one place.

Look how clean the collection looks!

image

It might be worth adding some kind of visual identifier next to the collection/sub-collection that have requests running in the before,beforeEach, after, afterEach hooks?

I could probably try to replicate this behaviour in the current version of Postman by doing this:

image

But it would be using lots of postman.setNextRequest("requesName") and would be pretty chaotic