代码之家  ›  专栏  ›  技术社区  ›  Divyang Desai Jonny B'Good

检查Postman测试用例中的null值。not.eql()或.not.equal()不起作用

  •  0
  • Divyang Desai Jonny B'Good  · 技术社区  · 7 年前

    我正在处理一个API项目,并使用Postman测试API。我编写了几个测试用例来检查null,如下所示,

    //user id is present
    pm.test("user id is present", function() {
        pm.expect(jsonData.data[0].userId).not.eql(null);
    });
    

    也试过了 .not.equal() this 答复

    //user id is present
    pm.test("user id is present", function() {
        pm.expect(jsonData.data[0].userId).not.equal(null);
    });
    

    然而,即使用户ID是 null . 有没有想过在Postman测试用例中检查空值?

    1 回复  |  直到 7 年前
        1
  •  6
  •   Ângelo Polotto    7 年前

    pm.test("user id is present", function() {
       console.log(typeof jsonData.data[0].userId);
       console.log(typeof jsonData.data[0]);
       console.log(typeof jsonData);
    }
    

    如果你得到 undefined 对于 jsonData.data[0].userId ,您需要更改测试以断言 null 未定义 :

    pm.test("user id is present", function() {
       pm.expect(jsonData.data[0].userId).to.exist //check if it is not equal to undefined
       pm.expect(jsonData.data[0].userId).to.not.be.null; //if it is not equal to null
    }
    
    推荐文章