如何解决邮递员参数化测试,具有相同请求的实际值和预期错误
我有多个测试用例、相同的端点、不同的实际值、不同的预期错误消息的请求。 我想创建发送特定值的参数化请求,并在所有情况下检查列表中的特定错误消息。 请求正文:
{
"username": "{{username}}","password": "{{password}}",...
}
回复:
{
"error_message": "{{error_message}}","error_code": "{{error_code}}"
}
错误信息因情况不同而变化:
现在,我对每个案例都有单独的要求。 问题:
有没有办法有 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
现在在 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 运行集合
输出
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。