邮递员参数化测试具有相同请求的实际值和预期错误

Postman parameterized tests with actual values and expected errors for same request

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

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

}

回复:

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

错误信息因情况不同而变化:

  1. 缺少用户名
  2. 忘记密码
  3. 密码或用户名不正确
  4. 等等

现在,我对每个案例都有单独的请求。 问题:

Is there way have 1 request with set of different values, checking particular error messages/codes?

创建一个 csv:

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

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

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

。正如丹尼提到的邮递员有一个非常丰富的文档,你可以使用

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

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

创建一个名为“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)
}

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

例如collection:

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

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

输出