【问题标题】:How to test if something is greater than certain number with POSTMAN如何使用 POSTMAN 测试某物是否大于某个数字
【发布时间】:2018-05-03 07:36:12
【问题描述】:

我正在尝试在 POSTMAN 中进行测试,其中大小必须大于 0,但我无法正确进行测试。

我所做的是在大小小于 0 时使其失败。

邮递员有检查大小是否大于x个数的功能吗?

    pm.test("Step 7/ Getting the resources and availabilites list " , function(){

    pm.expect(pm.response.code).to.be.oneOf([200]);
    if(pm.response.code === 200){
        var jsonData = JSON.parse(responseBody);
        var sizeOK= 1;
        if(jsonData.resources.length>0){

        }else{
            //I will make the test fail if there is not data available on the response.
            pm.test("Response body is empty ", function () {
                pm.expect(pm.response.json().resources.length).to.equal(1);
            });

        }
        console.log(Boolean(jsonData.resources.length>1))
    }

});

【问题讨论】:

  • 您要恢复哪种尺寸?来自响应正文中的元素?
  • 我只希望它大于0,然后我将遍历列表。

标签: javascript postman


【解决方案1】:
pm.expect(pm.response.json().resources.length).to.be.above(0);

见http://www.chaijs.com/api/bdd/

【讨论】:

    【解决方案2】:

    Postman 使用 chai 库的扩展实现。 你可以在这里查看源代码: https://github.com/postmanlabs/chai-postman

    所以从逻辑上讲,只有当您抛出错误并且测试捕获它时,测试才会失败。 否则它只是通过。 因此,expect 调用实际上会引发一个错误,从而导致测试失败。 如果你只返回任何东西或者什么都不返回,即使这样测试也会通过。

    考虑一个简单的 try and catch 块。 因此,要立即解决您的问题,您只需抛出一个错误,您的测试就会失败。

    你可以像这样修改你的代码:

    pm.test("Step 7/ Getting the resources and availabilites list " , function(){
    
        pm.expect(pm.response.code).to.be.oneOf([200]);
        if(pm.response.code === 200){
            var jsonData = JSON.parse(responseBody);
            var sizeOK= 1;
            if(jsonData.resources.length>0){
    
            } else {
                pm.test("Response body is empty ", function () {
                   throw new Error("Empty response body"); // Will make the test fail.
                });
    
            }
            console.log(Boolean(jsonData.resources.length>1))
        }
    
    });
    

    此外,您还可以使用简单的 javascript 来轻松测试长度/大小,就像这样(只是一个例子):

    pm.test("Step 7/ Getting the resources and availabilites list " , function(){
    
            pm.expect(pm.response.code).to.be.oneOf([200]);
            if(pm.response.code === 200){
                var jsonData = JSON.parse(responseBody);
                var sizeOK= 1;
                if(jsonData.resources.length>0){
    
                } else {
                    pm.test("Response body is empty ", function () {
                       if(jsonData.length < 3) {
                          throw new Error("Expected length to be greater than 3");
                       }
                    });
    
                }
                console.log(Boolean(jsonData.resources.length>1))
            }
    
        });
    

    【讨论】:

      【解决方案3】:

      虽然我不确定您需要的精确度,但您可以在 Postman 中获得响应大小。它由 Body size 和 Headers size 组成(只需在应用程序中指向 Size 的值)。在您的测试区,您可以通过以下方式恢复体型:

      var size=0;
      for (var count in responseBody) {
          if(responseBody.hasOwnProperty(count))
              size += 1;
      }
      console.log("BODY SIZE = " + size); // you'll see the correct value in the console for the body part
      

      然后针对这个值进行测试...

      【讨论】:

      • 嗨 A.Joly,感谢您的回答,但您可能不理解我的意思。我想在测试中做的是检查 resources.length 是否大于 2 做这样的事情: pm.expect(pm.response.json().resources.length).to.greaterThan(2);或类似:pm.expect(pm.response.json().resource.length > 2)。但是即使长度结果小于 2 测试也是成功的
      • 啊,好吧,对不起...我对 pm.xx 方法不够熟悉...您尝试不使用 Chai 吗?用老式的 javascript 编码?
      • 我没有,但我会检查它。谢谢!
      猜你喜欢
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2012-04-30
      • 1970-01-01
      • 2017-07-21
      • 2020-10-26
      • 1970-01-01
      相关资源
      最近更新 更多