如何获得邮递员的测试状态(即通过,失败或出错)?

问题描述 投票:3回答:2

这是我的邮递员测试用例

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);
    }
}

现在,如果此测试的状态为PASS,那么我不想执行下一个测试用例但是如果状态为FAIL,则应该执行下一个测试用例下一个测试用例是-

pm.test("verify the JSON object keys for machines- ", function() {
        pm.expect(data[1]).to.have.property('timeStamp');
    }
javascript postman testcase postman-testcase
2个回答
2
投票

也许这可以通过以编程方式跳过测试来实现。这是语法

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

});

获取一个变量,如果通过第一个测试的结果则更新它

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');
});

跳过结果

enter image description here

没有跳过的结果

enter image description here


1
投票

逻辑上,您需要“ OR”功能,但是邮递员中没有这样的功能。我建议的是得到正确/错误的结果,请与邮递员一起检查一下。

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.