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

问题描述

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

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

}

回复

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

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

  1. 忘记用户名
  2. 忘记密码
  3. 密码或用户名不正确

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

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

解决方法

创建一个 csv:

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

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

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

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

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

,

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

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

username,errorcode1

enter image description here

现在在 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 运行集合

输出

enter image description here