mdl1999
mdl1999

Reputation: 55

Is it possible to have several pm.test() inside a pm.test()

Is it possible to have several pm.test() inside a pm.test()? In such a way that if any of the inner pm.test() fails, the outer pm.test() will also fail. And if no inner pm.test() fails, the outer pm.test() passes.

My requirement is similar to how pm.expect() works. But i want to use pm.test() instead so i can the status of each test in the Test Results tab.

My code is below.

var response = pm.response.json()
pm.test("Test scenario 1", ()=> {
    
    pm.test("Checking response code", ()=> {
        pm.expect(response.code).to.eql(200);
    });
    
    pm.test("Checking response message", ()=> {
        pm.expect(response.message).to.eql('OK');
    });
   
    //more pm test
});

Thanks a lot.

Upvotes: 0

Views: 700

Answers (2)

mdl1999
mdl1999

Reputation: 55

By implementing some counter I was able to achieve what I want. I incremented the counter at the end of the inner pm.test(). If the pm.expect() fails, the counter is not incremented.

When all inner pm.test() are done, I compare the counter+1 with pm.test.index(). If they are equal, I do nothing (which basically passes the outer pm.test()). If they are not equal, I throw pm.expect.fail().

Note: pm.test.index() returns the number of tests executed at that point of time.

pm.test("To test able to do that", () => {
    let numberOfPassedTest = 0;
    
    pm.test("This test will pass", () => {
        //put assertion here
        pm.expect(200).to.eql(200)
        numberOfPassedTest += 1;
    });
    
    pm.test("This test will fail", () => {
        pm.expect(200).to.eql(201)
        numberOfPassedTest += 1; // this line is not executed because the previous line (pm.expect()) failed.
    });
    
    
    if(pm.test.index() === numberOfPassedTest+1){
        //do nothing. meaning no test failed and the whole pm.test() will be passed
    } else {
        pm.expect.fail("At least one of the tests failed. So this test case is marked as failed.");
    }
});

Sample result with failure: enter image description here

Upvotes: 0

PDHide
PDHide

Reputation: 19949

pm.test("Test scenario 1", ()=> {
    
    pm.test("Checking response code", ()=> {
        pm.expect(pm.response.code).to.eql(200);
    });
    
    pm.test("Checking response message", ()=> {
        pm.expect(pm.response.status).to.eql('5OK');
    });
   
    //more pm test
});

you can have but the output will show this as three tests only and result of internal test won't affect the out side abstraction

enter image description here

so in short you can but it won't give the behavior as you except

Upvotes: 1

Related Questions