【问题标题】:Request reuse in Postman在 Postman 中请求重用
【发布时间】:2018-10-22 03:21:10
【问题描述】:

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

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

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

【问题讨论】:

  • 是您使用的集合和子文件夹元素。引入这些是为了停止在您的请求中一遍又一遍地重复相同的事情,并将其提取到中心区域。它不会解决您的所有用例,但可能会有所帮助 - blog.getpostman.com/2017/12/13/…

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


【解决方案1】:

创建一个文件以加载到 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

【讨论】:

  • 运行 JavaScript 代码的唯一方法是发出请求。所以我需要做一个不相关的请求来执行 JavaScript 来加载测试序列和序列集的“第一个”请求。你有没有找到一种更优雅的方式来处理这个而不需要额外的请求?
  • 我们通常会运行一些没有副作用的东西作为第一个请求,例如 /ping 。它不是很优雅,但可以完成工作。
  • 嗨,我最终做了类似的事情。我的第一个也是最后一个请求只是加载谷歌。我将它们用作测试启动和拆卸,我在其中进行一些日志记录、设置/取消设置集合变量等...我希望 Postman 有一天允许将 js 与请求分开运行
猜你喜欢
  • 2022-07-04
  • 2019-07-05
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2020-08-19
  • 2018-04-14
相关资源
最近更新 更多