首页 文章

如果在 Postman 中测试失败,如何在控制台上获得预期和实际结果

提问于
浏览
0

我是新尝试的邮递员测试脚本,用于测试 API。我发现挑战的一件事是测试失败时的预期和实际测试结果。我应该如何实现它。我尝试使用 console.log 但如果测试用例失败则不会打印。如何使用单个函数为所有测试实现更通用的解决方案。

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    console.log("TestCase: Status Code should be 200"+", 
        Expected: "+"Response code should be 200"+", Actual: "+pm.response.code);
});

2 回答

  • 0

    如果测试失败,则断言错误消息会自动推送到测试结果部分:

    Status code is 200 | AssertionError: expected response to have status code 201 but got 200
    

    你可以使用它,但它会重复 Postman 在测试失败时告诉你的内容:

    pm.test(`Status code is 200 - Actual Status Code: ${pm.response.code}`, () => {
        pm.response.to.have.status(200)
    })
    
    Status code is 200 - Actual Status Code: 404 | AssertionError: expected response to have status code 200 but got 404
    
  • 0

    Postman Sandbox API 参考中,您有一个从服务器预期状态 ok(200)的通用示例:

    
    

    pm.sendRequest('https://postman-echo.com/get',function(err,res){

    if(错误){console.log(err); }

    pm.test('响应应该可以处理',function(){

    pm.expect(err).to.equal(null);

    pm.expect(res).to.have.property('code',200);

    pm.expect(res).to.have.property('状态','确定');

    });
    });

    
    

相关问题