【问题标题】:Run different collection test based on the HTTP method type in PostmanPostman 中根据 HTTP 方法类型运行不同的采集测试
【发布时间】:2019-03-21 20:01:21
【问题描述】:

我已经定义了几个集合测试,它们在集合中的每个单独测试之后触发。见下文

pm.test("Status code is not of error type", function() {
    pm.expect(pm.response.code).to.not.eql(500);
});

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

pm.test("Response must be valid json", function () {
     pm.response.to.be.withBody;
     pm.response.to.be.json;
});

我想进一步扩展它们。理想情况下,我想根据发送的方法类型运行不同的测试。例如,我想测试以下每个 DELETE 请求。

pm.expect(pm.response.code).to.be.oneOf([204, 409])

是否可以在集合级别定义它?还是我需要将此行粘贴到我的每个删除请求中?

【问题讨论】:

    标签: postman postman-collection-runner


    【解决方案1】:

    为了提高可见性,最好将此类测试放在请求级别,或者将类似的请求分组到一个文件夹中,然后在文件夹级别应用测试。文件夹/集合级别测试的目的是在广泛的范围内应用相同的测试。

    如果您确实想这样做,可以将您的测试包装在 if 条件中:

    if (pm.request.method === 'DELETE') {
      pm.expect(pm.response.code).to.be.oneOf([204, 409]);
    }
    

    【讨论】:

    • 抱歉回复晚了。谢谢您,我不知道您也可以在文件夹级别定义测试。太完美了。
    猜你喜欢
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多