在 Postman 中请求重用 [英] Request reuse in Postman

查看:27
本文介绍了在 Postman 中请求重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Our team wants to automate our REST API testing. Right now, we have a collection of Postman requests and make them jump through hoops manually.

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

We could create a collection/folder for each testing scenario, but that would mean a ton of duplication. Our API is still under heavy development and I really don't want to fix the same thing at twenty places after it changes.

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

I would like to have each endpoint request only once in a collection and some kind of independent logic that can execute them in an arbitrary order. I know Postman doesn't support request reuse in any clean way, so I am looking for at least a hacky way how to do it.

推荐答案

创建一个文件加载到 Postman Collection Runner,结构如下:

Create a file to load into the Postman Collection Runner, with the following structure:

[{
    "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.在整个集合的测试块"中:

Put all your test sequences in that file, then make Postman check the list after each request and decide what to execute next. This can be done e. g. in a "tests block" of the whole collection:

// 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);
    }
}

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

If your requests need to use different data during different runs, you can define the data in the input file and use them as variables in the request.

这篇关于在 Postman 中请求重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆