【问题标题】:Postman Test Script邮递员测试脚本
【发布时间】:2017-02-21 11:27:19
【问题描述】:

是否可以在 Postman 中打印出测试结果?我在测试脚本中编写了一个测试,我希望能够在我运行测试时输出失败的键的值。

这可能吗?

谢谢。

【问题讨论】:

    标签: javascript testing postman


    【解决方案1】:

    +1 为 Oguzhan Kircali。 pm.test / pm.expect 是 Postman 中用于编写测试的最新 BDD 和酷语法。

    我为 Oguzhan 消息添加了一个精度,这个语法允许在检查失败的情况下定义一个消息,使用这个语法:

    pm.test("Content does not have IP address.", function(){
        return pm.expect(pm.response.text(), "Content has IP address").to.not.include("IP Address");
    });
    

    最后的细节: 最好在最后一个 pm.expect() 或所有 pm.expect() 之后发送一个“RETURN”,否则如果您循环检查,您会收到一些错误。 示例:

    pm.test("Content does not have IP address.", function(){
        pm.expect(pm.response.text()).to.not.include("IP Address");
        return pm.expect(pm.response, "Response status code is not correct").to.have.status(200);
    });
    
    //OR
    
    pm.test("Content does not have IP address.", function(){
        pm.expect(pm.response.text()).to.not.include("IP Address");
        pm.expect(pm.response, "Response status code is not correct").to.have.status(200);
        return true;
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用这些测试在测试结果标签上显示警报:

      pm.test("Status code is 200", function () {
          pm.response.to.have.status(200);
      });
      

      或

      pm.test("Content does not have IP address.", function(){
          pm.expect(pm.response.text()).to.not.include("IP Address");
      });
      

      【讨论】:

        【解决方案3】:

        是的,你可以:

        var jsonData = JSON.parse(responseBody);
        tests["value is " + jsonData.value] = jsonData.value === 100;
        

        您也可以从环境中打印出值,例如:

        const value = postman.getEnvironmentVariable('value');
        tests["value should be " + value] = jsonData.value === 100;
        

        如果你愿意,你可以在 Postman 中使用 ES6 特性来测试脚本。所以让我们重构:

        tests[`value is ${jsonData.value}`] = jsonData.value === 100;
        

        【讨论】:

          猜你喜欢
          • 2021-01-16
          • 2021-10-26
          • 2019-05-14
          • 2019-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多