文章目录
一、没有使用参数化
请求url:
https://postman-echo.com/post
json请求体:
{"user":"admin","pwd":"abc1234"}
断言代码:
pm.test("用户名密码正常,登陆成功",function(){var jsonData = pm.response.json();
pm.expect(jsonData.data.user).to.eql("admin");
pm.expect(jsonData.data.pwd).to.eql("abc1234");});
- 没有参数化,如果有多个用例,即需在postman设置多个请求,重复率高
二、参数化
- 参数化支持的文件格式:json、csv
- 这里以json格式举例
- 注意:- 保存为UTF-8编码格式- 一个测试用例一条数据- - 一条数据内容一般包含 - 用例名称- 测试数据- 预期结果-
1. 测试数据部分字段参数化
1.1 参数化文件login1.json内容如下:
[{"case_name":"用户名密码正常,登陆成功","username":"admin","password":"abc1234","expect_user":"admin","expect_pwd":"abc1234"},{"case_name":"用户名错误,登陆失败","username":"adminxxx","password":"abc1234","expect_user":"adminxxx","expect_pwd":"abc1234"},{"case_name":"密码错误,登陆失败","username":"admin","password":"abc1234mike","expect_user":"admin","expect_pwd":"abc1234mike"}]
1.2 Postman设置
json请求体:
{"user":"{{username}}","pwd":"{{password}}"}
断言代码:
pm.test(data.case_name,function(){var jsonData = pm.response.json();
pm.expect(jsonData.data.user).to.eql(data.expect_user);
pm.expect(jsonData.data.pwd).to.eql(data.expect_pwd);});
1.3 关联文件,发送请求
- 注意:引用参数化文件不能直接点send按钮发送请求
- 需通过批量运行run按钮选择文件,再运行
2. 请求体参数化:请求体作为字符串处理
- 处理方式- 最外层加上双引号- 里面本身的双引号左边加上反斜杠-
2.1 参数化文件login2.json内容如下:
[{"case_name":"用户名密码正常,登陆成功","body":"{\"user\": \"admin\", \"pwd\": \"abc1234\"}","expect_user":"admin","expect_pwd":"abc1234"},{"case_name":"用户名错误,登陆失败","body":"{\"user\": \"adminxxx\", \"pwd\": \"abc1234\"}","expect_user":"adminxxx","expect_pwd":"abc1234"},{"case_name":"密码错误,登陆失败","body":"{\"user\": \"admin\", \"pwd\": \"abc1234mike\"}","expect_user":"admin","expect_pwd":"abc1234mike"}]
2.2 Postman设置
json请求体:
{{body}}
- 断言设置,关联文件,发送请求,和上面一样,这里不再重复截图
- 断言设置,关联文件,发送请求,和上面一样,这里不再重复截图
3. 请求体参数化:通过Pre-request Scrip预处理
3.1 参数化文件login3.json内容如下
[{"case_name":"用户名密码正常,登陆成功","body":{"user":"admin","pwd":"abc1234"},"expect_user":"admin","expect_pwd":"abc1234"},{"case_name":"用户名错误,登陆失败","body":{"user":"adminxxx","pwd":"abc1234"},"expect_user":"adminxxx","expect_pwd":"abc1234"},{"case_name":"密码错误,登陆失败","body":{"user":"admin","pwd":"abc1234mike"},"expect_user":"admin","expect_pwd":"abc1234mike"}]
3.2 Pre-request Scrip预处理
// json转string
body_value =JSON.stringify(data.body);// 设置全局变量
pm.globals.set("body_value", body_value);
3.3 json请求体处理
json请求体:
{{body_value}}
- 断言设置,关联文件,发送请求,和上面一样,这里不再重复截图
- 断言设置,关联文件,发送请求,和上面一样,这里不再重复截图
本文转载自: https://blog.csdn.net/tennysonsky/article/details/121101476
版权归原作者 Mike江 所有, 如有侵权,请联系我们删除。
版权归原作者 Mike江 所有, 如有侵权,请联系我们删除。