【问题标题】:Postman parameterized tests with actual values and expected errors for same requestPostman 参数化测试,具有相同请求的实际值和预期错误
【发布时间】:2021-06-16 04:11:51
【问题描述】:

我有多个测试用例、相同端点、不同实际值、不同预期错误消息的请求。 我想创建发送特定值的参数化请求,并从列表中检查所有情况下的特定错误消息。 请求正文:

{
"username": "{{username}}",
"password": "{{password}}",
 ...

}

回复:

{
"error_message": "{{error_message}}",
"error_code": "{{error_code}}"
}

不同情况下的错误信息变化:

  1. 缺少用户名
  2. 密码丢失
  3. 密码或用户名不正确

现在,我对每种情况都有单独的要求。 问题:

是否有 1 个请求具有一组不同的值,检查 特定的错误消息/代码?

【问题讨论】:

  • 这就是你如何进行数据驱动测试,使用 csv 或 r json 并运行你的脚本
  • 我会让@PDHide 留下一个完整的答案,但这正是他提到的。基本上,创建一个 CSV 文件,其中变量名称作为标题,每个测试的值在标题下的新行上。在 Collection runner 中使用该数据文件。
  • @DannyDainton 当然,谢谢 danny 正在等待知道这是否是 ops 想要的,因为它是一个直接的用例。正如 danny 所说,邮递员有一个非常丰富的文档,您可以使用 learning.postman.com/docs/running-collections/…

标签: postman parametrized-testing


【解决方案1】:

创建一个 csv:

username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1

现在在 collection runner 或 newman 中将其用作数据文件。

变量名与列名相同,并且对于每次迭代,您将有相应的行列值作为变量值。例如,对于迭代 1,用户名将是 username1

。正如 danny 所说,邮递员有一个非常丰富的文档供您使用

https://learning.postman.com/docs/running-collections/working-with-data-files/

【讨论】:

  • 它看起来真的很棒。但是我必须在我的收藏和环境中存储其他文件。它有机会在测试请求中解决它吗?
  • 你能说得更清楚些吗?我无法理解评论
  • 我的意思是,有没有办法在没有额外文件的情况下实现它?只是收集和环境
  • 请看添加的答案
  • 如果我需要通过共享工作区在 CI 中运行我的邮递员集合而没有机会导出 csv 文件怎么办?本地似乎解决了我的问题
【解决方案2】:

添加另一个关于如何运行来自同一请求的数据的答案:

创建一个名为“csv”的环境变量并复制以下内容并将其粘贴为值:

username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1

现在在 pr-request 中添加:

if (!pm.variables.get("index")) {

    const parse = require('csv-parse/lib/sync')
    //Environmental variable where we copy-pasted the csv content
    const input = pm.environment.get("csv");
    const records = parse(input, {
        columns: true,
        skip_empty_lines: true
    })

    pm.variables.set("index", 0)
    pm.variables.set("records", records)
}

records = pm.variables.get("records")
index = pm.variables.get("index")
if (index !== records.length) {
    for (let i of Object.entries(records[index])) {
        pm.variables.set(i[0], i[1])
    }
    pm.variables.set("index", ++index)
    pm.variables.get("index")===records.length?null:postman.setNextRequest(pm.info.requestName)
}

现在您可以为该特定请求运行数据驱动:

例如收集:

https://www.getpostman.com/collections/eb144d613b7becb22482

使用与环境变量内容相同的数据,现在使用 collection runner 或 newman 运行集合

输出

【讨论】:

  • 当我们重新运行该请求时,不应该在 nextRequest 前提条件下每次运行 pm.variables.set("index", 0) 吗?
  • pm.variable 创建一个局部变量,它的生命周期是完整的集合或 newman 运行。因此,当您开始运行时,您将获得索引值 undefined ,并将其设置为 0 ,然后它会继续递增。
  • 运行完成后,所有变量都将被销毁。对于下一次运行,该值从 undefined 开始
  • @Vova 查看更新后的答案,即使是最后一次迭代,它也会设置下一个请求,我添加了检查其最后一次迭代的步骤,然后不发送下一个请求
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 2012-08-17
  • 2021-08-25
  • 2022-07-20
  • 2013-09-07
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多