Ignoring tests and requests

Is to possible to ignore some requests and tests?
For example, user choose production environment and some tests, requests can’t be completed, And we need to ignore this tests and requests for this environment.

1 Like

@pieceofquality

  1. Requests can be ignored using the inbuilt postman.setNextRequest('Request Name'); function . For instance, if a collection has requests A, B, and C in order, request B can optionally be skipped by calling postman.setNextRequest('C'); in the test script of A.

  2. Tests can be skipped using pm.test.skip, like so:

(skipTest ? pm.test.skip : pm.test)('should be valid', function () {
    // do something here
});

Here, skipTest is an example variable that will be set to true if the test has to be skipped, and false otherwise.

2 Likes

we should use {{url}} comparing for such methods? if ({{url}} == prodUrl) {postman.setNextRequest(‘Request Name’)} if {{url}} == stageUrl {…}, else …
Or it can be better decision? Can we figure out what environment chosen by another method?

For such cases, you can use the following snippet:

var url = pm.environment.get('url'); // this fetches the value from {{url}}

// now, url can be used in any required condition
1 Like

I have more complicated case. i have 20 requests in my collection and make test for whole collection, one request in this collection is secondary (get definitions from html page) and i don’t want to run test for this request…

1 Like

@pieceofquality The suggested code snippet can work in your described case as well. If the snippet is placed inside a collection/folder level pre-request script, the same test can be made to work on a conditional basis for all your requests. For cases where your request is secondary, the skip condition based on the url environment variable should evaluate to true, which will lead to your test being skipped.

@kunagpal Is it possible to skip the request using the pre-request script of that test ? This is because the request can called from multiple different sources. For example I would like to skip the request if the data file does not have the right data for that request.

1 Like

That wasn’t possible the last time I tried. By the time the pre-request block is run your request is already queued up to run

If you, like me, strongly dislike the idea of replacing multiple instances of pm.test with that ternary, you can use a Proxy to do it based on some condition

var _nevermind = false;
const pmProxy = {
    get: function(pm, key) {
        if (key == 'test') {
            return (_nevermind ? pm.test.skip : pm.test);
        }
        return pm[key];
    }
};
pm = new Proxy(pm, pmProxy);
try {
    var jsonData = pm.response.json();
} catch (ex) {
    _nevermind = true;
}

Our particular use case was to skip API contract checking if the API doesn’t respond, YMMV

2 Likes

Would you mind explaining your code snippet, I do not have much idea on proxy?

Everything I know about Proxies I learned from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy so start there and experiement in the browser console

As this topic is still pulling a lot of views I’ve created two collections that you can fork to see it working in Postman directly:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.