I have a folder within a collection. I am using Collection Runner to run the queries within this folder. The second query uses data returned from the first. Therefore if the first query fails any of the tests in its test script, I need the run to stop immediately.
In practice this is not happening - the first query fails but the second still automatically runs.
Searching for this issue I found people making the opposite complaint - their queries were not dependent upon each other and therefore they wanted the run to keep going even if a query failed, but they were observing the behaviour that I want.
I can understand the Collection Runner having been enhanced with a new feature to allow it to continue upon the failure of a query in a collection, but as a rule, new features in products are added as options - they do not change default behaviour (because that, in general, will cause existing deployments that rely on the default behaviour to break).
So I am confused.
Does anyone have any insight into the behaviour that I am observing?
Any images of the app/runner or any of the test code you have in the requests, would help paint the picture of what you can see happening in front of you, hopefully it will in turn help others to suggest possible solutions.
Are you using postman.setNextRequest(null) to stop the run, if that request fails?
Hi Danny. I think you may have accidentally posted this comment to Kevin Swiber against the wrong forum item?
Re your initial response to myself, no I’m not using setNextRequest(null). Given the complaints I’ve read from others (see original post), surely that should not be necessary? Nevertheless, it sounds as though it might be a workaround for me.
I’m not sure what an actual example will add (other than confirming that I’m not using setNextRequest(null) ) - it either stops on an error or it doesn’t, and it doesn’t. Apart from setNextError, my expectation would be that it’s the configuration of the Collection Runner that one should be concentrating on rather than the test scripts - behaviours such as this should if anything be managed by configuration surely. But I can’t find a configuration option of relevance.
That said, I can see that one might want different behaviours in the event of different queries failing. Queries that have no interdependencies and are merely being run as a batch for convenience should allow the collection to keep going - it’s only queries that are dependent upon the result of previous queries that need to be interrupted. That being the case, it now seems reasonable to me that the collection runner should keep running by default. I should inject some code into the Pre-request Script of any query that has prerequisites in order to check that the pre-reqs have been satisfied before allowing the query to run. In that case how would I prevent my script running at that point - I’m assuming that setNextRequest only takes effect upon the completion of the existing request rather than causing it to be bypassed?
Regards,
Michael
[EDIT] Yes I’m correct in my assumption according to this post: How to explicitly stop calling a request in postman through pre-request script? . And it shows no solution either. It looks as though I have no choice but to use setNextRequest in the Test Script of the first test in order to bypass the dependent test. That’ll do.
I believe there was an edit, that you don’t have the full view of so that’s why it might have looked confusing to you. I wrote the wrong syntax and Kevin quite rightly, changed this for me
The reason I normally ask for an example, either through a series of images or a sample of the code that has been written is to help bring the problem to life. Other users cannot see what you have in front of you.
Sometimes just added a text based question/problem can lead to confusion and a series of follow up questions that could take longer to get you a solution.
The postman.setNextRequest() function is only available to control the workflow from the Tests tab of the Request and is actioned after that Request has been sent.
If your first request has failed for some reason, that’s the place to tell the runner to either continue or stop the collection run. This can also be done in a central place, like at the collection or folder level if you didn’t want to add the same code to each request.
You cannot currently stop a request from running from the Pre-request Script, you could add assertions to meet certain pre conditions that would be seen as failures but you would be able to stop the request from executing.
I would love to see more about what you’re doing, to be able to advise you on a different and more context based solution.
Ah, I wasn’t aware that visibility of replies could be constrained - that makes sense now, thanks.
In my case I am doing a test against a card payment gateway whereby I want to test the cancellation of a request. The test therefore has two queries - one to request a payment and then a follow-up to cancel the payment request before it goes any further (card payment is an asynchronous process - there’s a lot goes on in the background between the banks after the initial payment request has been made and authorisation given).
The initial payment request will return a unique transaction reference issued by the payment gateway in response to the payment request in addition to the authorisation code. That xn ref has to be passed in the follow-up request to cancel the payment. If the initial payment request fails for whatever reason, I want the test to stop there rather than attempting to issue a cancellation request with a xn ref that doesn’t exist (which it won’t if the payment request failed).
postman.setNextRequest(null) in the Test Script of the payment query should be sufficient, although if I have other independent tests in my collection I’ll need to know exactly which one to move on to and I won’t have the flexibility to move folders around, but then again I doubt I need that level of flexibility, so it shouldn’t matter.
I also need to stop my collection from continuing upon a test fail. My collection has 111 requests within approx 20 folders. So it’d be great if there was an overall setting that if a test fails, abort the collection run. But if I have to add a command to each of my test tabs, I will do that.
Please help me understand how to add this command upon failure. postman.setNextRequest(null)
I am a newer postman user and am not familiar with if it can do IF statements.
Here is my test, if this fails, I want to stop the collection runner.
pm.test(“Status code is 201”, function () {pm.response.to.have.status(201);});
I finally got it to work. I had to set the nextrequest to null, then do my test. If my test fails the next line is not executed so it stops. But if it passes then I clear the null so the collection moves on. whew…
I also moved my variable setting up into the pm.test so they do not occur if the test fails.
pm.test("Status code is 200", function () {
postman.setNextRequest(null);
pm.response.to.have.status(200);
postman.setNextRequest();
//convert to json format
var RespData = xml2Json(responseBody);
//set environment level variable
pm.environment.set("PolicyNumber", RespData['OnlineData.autoSaveDataRs']['$']['policyNumber']);
//return policy number in test tab
pm.test(pm.environment.get("PolicyNumber"));
});
Absolutely ingenious. Thank you for this brilliant workaround and clear example. (By the way I would like to add that I find it very weird that Newman has a --bail option but the collection runner in Postman itself does not have any equivalent setting.
Hi there. I’m new with Postman as well.
I may be wrong, but so far I found that “stop-run-when-failed” feature can be enhanced. I will try to explain below:
The current work-around (or usage) is to use postman.setNextRequest(null) with each request in Test-section;
But in Test-section I can provide assertion(s) that validates response and marks corresponding request as Passed or Failed.
So, the idea is to have an option when Collection is running to stop the run if a request is failing (based on assertion-results).
This option could be added to this UI-screen and to postman-APIs as well
Thanks @kclausing , that really helped me too!
The one thing I noticed, however, is that if you have other tests in the same request, they will still be executed. In my case, I added the setNextRequest(null) and setNextRequest() in both, so while the first test failed, the second passed and allowed the collection to continue.
My solution so far has been to add a bool flag, and setting it to false whenever we call setNextRequest(null), and to true when calling setNextRequest(). Then we add pm.expect(my_bool).to.be.true at the beginning of each additional test, so they can fail and return immediately.
Here is my implementation on this topic
pm.test(“Status code is 200 setNextRequest(Token) else stop iteration”, function () {
var status = pm.response.code
if(status == 200){
postman.setNextRequest(“Request name”);
}
else {
postman.setNextRequest(null);
}
});