4

Here is my test case in postman

pm.test("verify the JSON object keys for machines - ", function() {
    if (Object.keys(data).length === 0) {
        pm.expect(Object.keys(data).length).to.eq(0);
    }
}

Now if status of this test is PASS then I don't want to execute next test case but if status is FAIL then next test case should get executed Next test case is -

pm.test("verify the JSON object keys for machines- ", function() {
        pm.expect(data[1]).to.have.property('timeStamp');
    }
Dev
  • 2,739
  • 2
  • 21
  • 34

2 Answers2

4

Perhaps this can be achieved by programmatically skip the tests. Here is the syntax

(condition ? skip : run)('name of your test', () => {

});

Take one variable, update it if the result of the first test is passed

var skipTest = false;

pm.test("verify the JSON object keys for machines - ", function() {
    if (Object.keys(data).length === 0) {
        pm.expect(Object.keys(data).length).to.eq(0);
        skipTest = true // if the testcase is failed, this won't be updated
    }
}

(skipTest ? pm.test.skip : pm.test)("verify timeStamp keys for machines-", () => {
     pm.expect(data[1]).to.have.property('timeStamp');
});

Result Skip

enter image description here

Result Without Skip

enter image description here

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • 1
    Thanks for suggesting the another approach and it seems to closer one what i was looking for but it is still the not the simplified to me, I need to implement it every time when I need to achieve it, was looking for the way postman has already simplified most things. I will still wait for others to answer, will accept your answer if not got any – Dev Nov 30 '19 at 08:00
1

Logically, you need the "OR" function, but there is no such one in the postman. What I would suggest is to get true/false result end check it with the postman.

pm.test("verify the JSON object keys for machines - ", function() {
    const result = 
        Object.keys(data).length === 0 || // true if there are no properties in the data object
        'timeStamp' in data; // or true if there is timeStamp property in the data object
    
    pm.expect(lengthEqualZero || hasPropertyTimeStamp).to.be.true;
}
AlexOwl
  • 869
  • 5
  • 11
  • Thanks for feedback, It can be done simple by using `if .. else` and i know the way you presented as well. But what lead me to this question is when we execute the test cases the result is displayed in the the Test Results tab in postman and it also displays how many test cases passed and failed out of total beside the tab name if they can provide this then there must be a way to that. In robot framework, testng it is possible to get the status of test cases and so I am expecting this feature in Postman – Dev Nov 24 '19 at 12:45