在 Postman 中请求重用

Mar*_*rey 1 automated-tests postman postman-collection-runner

我们的团队希望自动化我们的 REST API 测试。现在,我们有一组 Postman 请求,并让它们手动跳过箍。

我们可以为每个测试场景创建一个集合/文件夹,但这意味着大量重复。我们的 API 仍在大量开发中,我真的不想在更改后在 20 个地方修复相同的内容。

我希望每个端点在一个集合中只请求一次,以及某种可以以任意顺序执行它们的独立逻辑。我知道 Postman不支持以任何干净的方式请求重用,所以我正在寻找至少一种如何做到这一点的 hacky 方式。

Mar*_*rey 5

创建一个文件以加载到 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"
}]
Run Code Online (Sandbox Code Playgroud)

将所有测试序列放在该文件中,然后让 Postman 在每次请求后检查列表并决定下一步执行什么。这可以在例如整个集合的“测试块”中完成:

// 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您的请求在不同的运行中需要使用不同的数据,您可以在输入文件中定义数据,并将它们用作请求中的变量