mercredi 6 novembre 2019

POSTman - Test with custom function throws an assertion error

I am doing some test with Postman analysing JSON files. I realised that most of the time I end up making the same test over a bunch of properties, so I decided to make a custom function and add it to the test as a parameter.

Once I do that, whenever an assertion fails it will throw an assertion error instead of failing the test itself. The test won't appear in the Tests Results window.

It works when the assertion is successful though. The test passes and shows in the Test Results window.

It works without a custom function, but in that case, every test will have its own function, with the same line of code, and that's what I wanted to avoid.

function myFunction(propertyName,testData) {
    {   
            return pm.expect(testData).to.have.property(propertyName);
    }
}

var data = JSON.parse(responseBody);

var testData = data.entries;
/*TESTS USING CUSTOM FUNCTION*/
for (var i = 0; i < 5; i++) { 
    try{
    //This test PASSES and shows in the Test Results window.
        pm.test("test1", myFunction('summonerId',testData[i])); 
    }catch(error){
        console.log(error);
    }
    try{
    //This test PRODUCES AN ERROR and DOESN'T shows in the Test Results window.
        pm.test("test2", myFunction('somethingElse',testData[i])); 

    }catch(error){
        console.log(error);
    }
 }
/*TESTS USING ONE FUNCTION PER TEST*/
for (var i = 0; i < 5; i++) { 

    try{
    //This test PASSES and shows in the Test Results window.
        pm.test("test1", function(){
           pm.expect(testData).to.have.property('summonerId'); 
        }); 

    }catch(error){
        console.log(error);
    }
    try{
    //This test FAILS and shows in the Test Results window, without errors
        pm.test("test2", function(){
            pm.expect(testData).to.have.property('somethingElse'); 
        });
    }catch(error){
        console.log(error);
    }
 }

So when using a custom function, if an assertion is successfull everything is fine and the test is shown as PASSED, BUT if an assertion fails, it throws an error instead of failing the test, and the test is not shown as 'FAILED'.

ASSERTION ERROR:

message: "expected { Object (summonerId, summonerName, ...) } to have property 'somethingElse'"
name: "AssertionError"
showDiff: false
stack: "AssertionError: expected { Object (summonerId, summonerName, ...) } to have property 'somethingElse'
    at myFunction (eval at exec (evalmachine.<anonymous>:13040:2548), <anonymous>:9:52)
    at Object.eval (eval at exec (evalmachine.<anonymous>:13040:2548), <anonymous>:24:30)
    at Uniscope.exec (evalmachine.<anonymous>:13040:2583)
    at module.exports (evalmachine.<anonymous>:69:538)
    at Object.<anonymous> (evalmachine.<anonymous>:72:1871)
    at evalmachine.<anonymous>:16:26
    at Array.forEach (<anonymous>)
    at Object.emit (evalmachine.<anonymous>:15:54)
    at evalmachine.<anonymous>:52:24
    at evalmachine.<anonymous>:5:21"

Aucun commentaire:

Enregistrer un commentaire