在 Postman 中请求重用

Request reuse in Postman

我们的团队想要自动化我们的 REST API 测试。现在,我们有一组 Postman 请求并让它们手动跳转。

我们可以为每个测试场景创建一个 collection/folder,但这意味着大量重复。我们的 API 仍在大力开发中,我真的不想在更改后在二十个地方修复相同的东西。

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

创建一个文件以加载到 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。在整个集合的 "tests block" 中:

// 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