在Postman中请求重用

时间:2018-05-11 16:49:49

标签: automated-tests postman postman-collection-runner

我们的团队希望自动化我们的REST API测试。现在,我们收集了一些邮递员请求,并让他们手动跳过箍。

我们可以为每个测试场景创建一个集合/文件夹,但这意味着大量的重复。我们的API仍在大力开发中,我真的不想在它改变之后在二十个地方修复同样的东西。

我希望每个端点只在集合中请求一次,并且某种独立逻辑可以以任意顺序执行它们。我以任何干净的方式认识Postman doesn't support request reuse,所以我至少要寻找一种如何做到的hacky方式。

1 个答案:

答案 0 :(得分:3)

创建一个文件以加载到Postman Collection Runner中,具有以下结构:

[{
    "testSequence": ["First request name", "Second request name", "..." ],
    "anyOtherData":  "Whatever the request needs",
    "evenMoreData":  "Whatever the request needs",
    "...":           "..."
},{
    "testSequence": ["Login", "Check newsfeed", "Send a picture", "Logout" ],
    "username":  "Example",
    "password":  "correcthorsebatterystaple",
},{
    "...": "keep the structure for any other test scenario or request sequence"
}]

将所有测试序列放在该文件中,然后让Postman在每个请求后检查列表并确定接下来要执行的操作。这可以做到e。 G。在整个集合的“测试块”中:

// Use the mechanism only if there is a test scenario file
// This IF prevents the block from firing when running single requests in Postman
if (pm.iterationData.get("testSequence")) {

    // Is there another request in the scenario?
    var sequence = pm.globals.get("testSequence");
    if ((sequence instanceof Array) && (sequence.length > 0)) {

        // If so, set it as the next one
        var nextRequest = sequence.shift();
        pm.globals.set("testSequence", sequence);
        postman.setNextRequest(nextRequest);

    } else {
        // Otherwise, this was the last one. Finish the execution.
        postman.setNextRequest(null);
    }
}

如果您的请求需要在不同的运行期间使用不同的数据,您可以在输入文件中定义数据并在请求中将其用作variables