Persisting a Fail as a test result with SetNextRequest

I’m setting up a automated test that runs through multiple requests.
If a test doesn’t get any data returned, then I want to set a next request.
This works. But i want to get notified that the test is failed in the Test Result.

Can someone help me with the code?

The response I could get and where I want to manualy set a new request:

{
    "response": {
        "ResultData": [
            {
                "valk": []
            }
        ]
    }
}

Test i made:

let json = pm.response.json();
if (json.response.ResultData[0].valk.length === 0) {
    pm.assert.fail("Test is failed and next request is set");
    postman.setNextRequest('d34b0b5c-5980-41ab-81e4-d30ed7cef085')
} else {
    pm.collectionVariables.set("valk1RowKey", json.response.ResultData[0].valk[0].RowKey);
}
  • Here I want to give a Fail with a comment so i can see it in my Test Results.
  • I made up the ‘pm.assert.fail’ as an example. I don’t know the right syntax for it or if it even exists.

My recommendation would be to write a proper test checking the array, and have the IF statement outside of this.

let json = pm.response.json();

pm.test("Expect valk to be an array that is not empty", () => {
    pm.expect(json.response.ResultData[0].valk).to.be.an("array").that.is.not.empty;
})
    
if (json.response.ResultData[0].valk.length === 0) {
    postman.setNextRequest('d34b0b5c-5980-41ab-81e4-d30ed7cef085')
}

If you really want to write something directly to the test results tab, the following should work.

pm.test("my test name", () => { throw new Error("error message") });

image

This workes like a charm. Thanks for your help and recommendations.
This is helping me learn to write better tests :).

For anyone else coming across this topic, I just found another way of writing to the test results.

You can use the old tests method which is as simple as the following.

tests['test 1'] = true
tests['test 2'] = false

image