One expected response for several requests

Hi,

I have one answer for several requests. I would like to automate this in clever way in order not to change expected results in several places.

Request 1/Request 4/Request 5:

Expected Answer for each request:

 pm.test("Number 1 is fine", function () {
     pm.expect(pm.response.text()).to.include("50000");
 });
 pm.test("Number 2 is fine", function () {
     pm.expect(pm.response.text()).to.include("600000");
 });
 pm.test("Number 3 is fine", function () {
     pm.expect(pm.response.text()).to.include("800");
 });

Hi @kalabuda. Welcome to the Postman Community.

I do not correctly understand your question. What do you mean by an “answer” to a requests? Are you referring to a specific test scenerio? Are the tests script you shared the expected answers?

The problem you have is that you only want to run those tests for certain requests, but not all requests, so adding the tests at the folder or collection level won’t work.

You might want to consider the new package library feature.

Reuse scripts in Postman | Postman Learning Center

Or the older method which is to define the tests in a function set as a global variable in a pre-request script. You can add this at the collection or folder level, so it will be available for all requests.

utils = {
    tests: function (pm) {
        pm.test("Number 1 is fine", function () {
            pm.expect(pm.response.text()).to.include("50000");
        });
        pm.test("Number 2 is fine", function () {
            pm.expect(pm.response.text()).to.include("600000");
        });
        pm.test("Number 3 is fine", function () {
            pm.expect(pm.response.text()).to.include("800");
        });
        return 'hello';
    },
};

Then in the post response scripts for the appropriate requests, you can call that function using…

utils.tests(pm);

image

If you want to change the tests, then you only need to update the global function.

Thank you for tips according to utils funcktion, but this can be done in one collection. What about bunch of collections?

DO you have any idea how it should look like using packeges?

I haven’t used packages yet, but that is probably the best way to do this now its available.

If you want this to run across collections, then that is probably the only way to achieve this.

You’ll have to read the instructions and guidelines on how that works.

There are limits on the number of packages you can have based on your plan, so this method will probably still be an option to consider for utility code.