0

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

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

}

回复:

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

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

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

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

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

4

2 回答 2

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/

于 2021-03-18T20:38:51.490 回答
0

添加有关如何运行从同一请求驱动的数据的另一个答案:

创建一个名为“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 运行集合

输出

在此处输入图像描述

于 2021-03-18T22:39:12.390 回答