Postman 中是否可以只运行部分请求?

问题描述 投票:0回答:3

我们在 Postman 中将很多 API 级别的自动化测试编写为请求集合。

我们有一个脚本可以自动运行所有集合。

有没有办法只标记/运行请求的子集,例如带有一些标签,例如作为烟雾套件,无需将请求复制到新集合并显式运行(因为这需要在两个地方维护相同的测试......)?

可能存在标签、组或某些跳过请求的脚本,设置了环境变量...

postman postman-collection-runner
3个回答
1
投票

您可以创建文件夹并组织测试,例如

  1. 烟雾与回归
  2. smoke_only 等

当使用 newman 作为命令行工具时,您可以使用 --folder argumentt 指定要运行的文件夹

您还可以使用 postman.setNextRequest 来控制执行流程。

你也可以将 newman 作为 npm 模块运行。

您只需要编写一个逻辑来读取集合文件并获取所有包含“smoke”的文件夹名称,例如并将其作为数组传递

const newman = require('newman'); // require newman in your project

 

// call newman.run to pass `options` object and wait for callback
newman.run({
    collection: require('./sample-collection.json'),
    reporters: 'cli',
    folder: folders
}, function (err) {
    if (err) { throw err; }
    console.log('collection run complete!');
});

只需更新评论:

在新旧 UI 中,您可以选择要在集合运行器中执行的文件夹

enter image description here


1
投票

获取集合中的所有请求:

您还可以使用以下方式获取有关集合中所有请求的信息:

https://api.getpostman.com/collections/{{collection_UUID}}

要获取 uuid 和 api 密钥,请转到:

https://app.getpostman.com

现在生成API密钥>

转到帐户设置 > api 密钥并生成 api 密钥。

要获取集合 uuid,请转到特定工作区和集合并从 url 复制 uuid 部分:

enter image description here

现在已在您的收藏中

将所有请求重命名为:

get user details [Regression][Smoke][Somethingelse]
get account details [Regression]

然后创建一个名为初始请求的新请求,并将其保留为集合中的第一个请求:

网址:https://api.getpostman.com/collections/8xxxxtheuuidyoucopied

授权: apikey-header :键:X-Api-Key 和值:yourapikey

测试脚本

pm.environment.unset("requestToRun")
reqeustlist = pm.response.json().collection.item.map((a) => a.name)
requestToRun = reqeustlist.filter((a) => a.includes(pm.environment.get("tag")))
let val = requestToRun.pop()
pm.environment.set("requestToRun", requestToRun)
val ? postman.setNextRequest(val) : postman.setNextRequest(null)

现在将环境变量设置为您想要查找的内容,例如:运行包含文本“回归”的脚本,然后设置

pm.environment.set("tag","Regression")

现在在您的收藏预请求中添加:

if (pm.info.requestName !== "initial request") {
    let requestToRun = pm.environment.get("requestToRun")
    let val = requestToRun.pop()
    pm.environment.set("requestToRun", requestToRun)
    val ? postman.setNextRequest(val) : postman.setNextRequest(null)
}

输出:

enter image description here

示例集合:

https://www.getpostman.com/collections/73e771fe61f7781f8598

Ran 仅请求名称中包含“Copy”的请求


0
投票

Postman Flow 解决了这个目的。 您可以为烟雾、回归创建流程... 有了这个,我现在有了单一的集合,没有任何分叉来维护烟雾或回归。

© www.soinside.com 2019 - 2024. All rights reserved.