Can the global function be replaced by collection/folder level pre-request and test scripts?

The post https://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/ describe a few advanced tips.

However, it was published 2 years ago. It sounds a bit dated.

The reuse code section is cool. But postman already have collection/folder level pre-request and test scripts. does it make the global function idea irrelevant?

Thanks

2 Likes

You can reuse some code by storing it in the collection / folder but you need to initialize the run from there.

What this means is the following: you can define a function foo in the collection / folder but you cannot call it from a request. You still need to run the function from there (after defining it).

So reusing code is, at least for now, still limited but it has improved.

Does this help?

1 Like

One thing that I’ve started to do, and would apply here, is to define variables from folders’ pre-scripts, and to update the stored variables. I use it to set certain common variables to specifics needed for tests/APIs within a given folder structure. I have tests that are similar, but some values need to be set specific to the tests being run. This lets me use a single environment, and flip values dynamically from the folder level.

The way this could apply here is to combine folder variable definition with setting global level variables to contain functions - this lets a folder level applied value to be used anywhere. It also allows for global variables to be portable - by copying and moving folders, or sharing collections.

I’m in the process of trying to simplify the mess of code and variables and environments I use day to day, in to a compact standalone and easy to use Collection - which sets itself up via folder level environment setup and inheritance of pre/post scripts at the folder level so as others use and enhance this setup - their calls just automatically use the framework/code that is stored in folders/globals/active-environment.