Test looping through response body array

My response body is the array below every ten minutes in the last 24 hours, only the first two are shown below. I would like to test each for lossPercent < 5, latencyMs < 100, and jitter < 10

[
{
“startTs”: “2022-03-25T19:52:00Z”,
“endTs”: “2022-03-25T19:53:00Z”,
“lossPercent”: 0,
“latencyMs”: 3.93,
“goodput”: 100000,
“jitter”: 0.07
},
{
“startTs”: “2022-03-25T19:53:00Z”,
“endTs”: “2022-03-25T19:54:00Z”,
“lossPercent”: 0,
“latencyMs”: 3.91,
“goodput”: 100000,
“jitter”: 0.07
},

The problem is, when I run the test below it always passes, even if the data is above the threshold I’ve set, as you can see I’ve set “latencyMs” to be below 1, and it passes. Also, I’m not sure it’s even looping through each array?

var response = JSON.parse(responseBody);
pm.test(‘Latency is less than 100MS’, () => {
_.each(pm.response.json().latencyMs, (arrItem) => {
pm.expect(arrItem.latencyMs.value).to.be.below(1)
})
});
pm.test(‘Loss percentage is less than 5’, () => {
_.each(pm.response.json().lossPercent, (arrItem) => {
pm.expect(arrItem.lossPercent.value).to.be.below(5)
})
});
pm.test(‘Jitter is less than 10MS’, () => {
_.each(pm.response.json().jitter, (arrItem) => {
pm.expect(arrItem.jitter.value).to.be.below(10)
})
});

I have a working test but it doesn’t loop, it only tests against the first array

var response = JSON.parse(responseBody);
pm.test(“Loss percentage is less than 5”,function () {
pm.expect(.get(response, ‘0.lossPercent’)).to.be.below(5);
});
pm.test(“Latency in MS is less than 100”,function () {
pm.expect(
.get(response, ‘0.latencyMs’)).to.be.below(100);
});
pm.test(“Jitter is below 10MS”,function () {
pm.expect(_.get(response, ‘0.jitter’)).to.be.below(10);
});

Any help would be much appcreiated, thanks in advance

Hi @cpham213

Try something like this;
(The try/catch allows all assertions to execute before failing the overall test).

const response = pm.response.json();

//set count so that pm.expect.fail can be triggered if any of the assertions fail.
let count = 0;

pm.test("My Tests", function () {
    for (let i = 0; i < response.length; i++) {

        try{pm.expect(response[i].latencyMs).to.be.below(100); //change this to 1 to simulate a fail
        console.log(response[i].latencyMs);
        }catch(e){pm.test("latencyMs", () => {throw new Error(e.message)}), count++;}

        try{pm.expect(response[i].lossPercent).to.be.below(5);
        console.log(response[i].lossPercent);
        }catch(e){pm.test("lossPercent", () => {throw new Error(e.message)}), count++;}

        try{pm.expect(response[i].jitter).to.be.below(10);
        console.log(response[i].jitter);
        }catch(e){pm.test("jitter", () => {throw new Error(e.message)}), count++;}

    }

    //Count how many tests above fail, and fail the test case using pm.expect.fail if there is 1 or more failures
    if(count > 0) {
        pm.expect.fail("There are " + count + " failures within the above checks, please check...")
    } 

});

Some of my responses are “null” and it’s failing my test, is there a way to ignore? Thank you much for your help!

{
“startTs”: “2022-03-27T17:45:00Z”,
“endTs”: “2022-03-27T17:46:00Z”,
“lossPercent”: null,
“latencyMs”: null,
“goodput”: null,
“jitter”: null
},

It kinda depends on what you expect the result to be, ie; is ‘null’ valid / expect?

You could try something like this;

const response = pm.response.json();

//set count so that pm.expect.fail can be triggered if any of the assertions fail.
let count = 0;

pm.test("My Tests", function () {
    for (let i = 0; i < response.length; i++) {

        if (response[i].latencyMs === null){
            console.log('jitter is null');
        }else{
            try{pm.expect(response[i].latencyMs).to.be.below(100); //change this to 1 to simulate a fail
            //console.log(response[i].latencyMs);
            }catch(e){pm.test("latencyMs", () => {throw new Error(e.message)}), count++;}
        }

        if (response[i].lossPercent === null){
            console.log('jitter is null');
        }else{
            try{pm.expect(response[i].lossPercent).to.be.below(5);
            //console.log(response[i].lossPercent);
            }catch(e){pm.test("lossPercent", () => {throw new Error(e.message)}), count++;}
        }

        if (response[i].jitter === null){
            console.log('jitter is null');
        }else{
            try{pm.expect(response[i].jitter).to.be.below(10);
            //console.log(response[i].jitter);
            }catch(e){pm.test("jitter", () => {throw new Error(e.message)}), count++;}
        }
    }

    //Count how many tests above fail, and fail the test case using pm.expect.fail if there is 1 or more failures
    if(count > 0) {
        pm.expect.fail("There are " + count + " failures within the above checks, please check...")
    } 

});
2 Likes

Thank you, appreciate your help!

1 Like