【问题标题】:Using IF/ELSE in Postman Test scripts在 Postman 测试脚本中使用 IF/ELSE
【发布时间】:2022-02-03 00:40:40
【问题描述】:

我正在使用 Postman 进行测试,但我坚持在脚本中使用 if/else。

我的目标是测试一个属性在响应正文中是否可见。如果该属性不存在,我希望邮递员期望它是空的。并继续下一个测试。属性嵌套在一个数组中。

这是我到目前为止想出的:

//Test of correct sub properties rijbewijs zijn meegestuurd
pm.test(\"Test if correct sub properties rijbewijs are sent\", () => {
    if (pm.expect(matchesVacatures.rijbewijs).to.be.empty);
    else { pm.expect(matchesRijbewijs).to.have.property(\'codeSoortRijbewijs\');}  
});

但是,如果数组不为空,测试总是“失败”。 我该怎么做,这可能是我在测试中错误放置的一些小东西,但我无法弄清楚。

  • 为什么不使用if (!pm.expect(matchesVacatures.rijbewijs).to.be.empty) pm.expect(matchesRijbewijs).to.have.property(\'codeSoortRijbewijs\') 而不是使用分号
  • 感谢您的评论,但是!邮递员似乎忽略了它,它产生了相同的结果。
  • 我没想到它会解决您的问题,只是制作更好的代码

标签: javascript if-statement postman-testcase


【解决方案1】:

弄清楚了:

pm.test("Test if correct sub properties rijbewijs are sent", () => {
    if ((matchesVacatures.rijbewijs).length !== 0)
    {pm.expect(matchesVacatures.rijbewijs[0]).to.have.property('codeSoortRijbewijs');}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多