前言
参数化是自动化测试离不开的话题,httprunner里面只要把上一篇声明变量学会了,参数化也就自然会了。
不同的地方在于声明变量时对应值只有一个,参数化是多个值,存放在list里面。
httprunner==1.5.8
登录参数化
先准备测试数据,准备四组登录用的账号和密码,账号为test1,test2,test3,test4,密码统一设置为123456。
参数user账号数据,设置对应的值 ["test1", "test2", "test3", "test4"],定义单个参数用variables,定义参数化用parameters
parameters:
- user: ["test1", "test2", "test3", "test4"] # 参数化
- psw: ["123456"]
如果参数化里面的数据只有一个,比如psw对应的值只有一个,使用variables和parameters对应都可以
parameters:
- user: ["test1", "test2", "test3", "test4"] # 参数化
variables:
psw: 123456
完整的test_parameters.yml脚本如下
# 上海悠悠,QQ交流群:750815713
- config:
name: logincase
parameters:
- user: ["test1", "test2", "test3", "test4"] # 参数化
- psw: ["123456"]
- test:
name: login case1
request:
url: http://127.0.0.1:8000/api/v1/login/
method: POST
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: $user # 引用变量
password: $psw
extract:
- token: content.token # 提取token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.msg, login success!]
- eq: [content.code, 0]
运行用例
运行用例,会自动生成四个测试用例
D:softuntitled>hrun test_parameters.yml
login case1
INFO POST http://127.0.0.1:8000/api/v1/login/
INFO status_code: 200, response_time(ms): 523.77 ms, response_length: 110 bytes
INFO start to extract from response object.
INFO start to validate.
.
login case1
INFO POST http://127.0.0.1:8000/api/v1/login/
INFO status_code: 200, response_time(ms): 525.67 ms, response_length: 110 bytes
INFO start to extract from response object.
INFO start to validate.
.
login case1
INFO POST http://127.0.0.1:8000/api/v1/login/
INFO status_code: 200, response_time(ms): 436.3 ms, response_length: 110 bytes
INFO start to extract from response object.
INFO start to validate.
.
login case1
INFO POST http://127.0.0.1:8000/api/v1/login/
INFO status_code: 200, response_time(ms): 487.6 ms, response_length: 110 bytes
INFO start to extract from response object.
INFO start to validate.
.
----------------------------------------------------------------------
Ran 4 tests in 2.037s
OK
INFO Start to render Html report ...
INFO Generated Html report: D:softuntitled
eports1569118975.html
查看报告
关联参数
上面的案例多个账号,密码都是一样的,如果密码不一样呢?上面的就行不通了,所以针对于一个账号对应一个密码,这种一一对应的关系,可以用关联性的参数化
parameters:
- user-psw:
- ["test1", "123456"]
- ["test2", "123456"]
- ["test3", "123456"]
- ["test3", "123456"]
完整的测试用例test_param_psw.yml
# 上海悠悠,QQ交流群:750815713
- config:
name: logincase
parameters:
- user-psw:
- ["test1", "123456"]
- ["test2", "123456"]
- ["test3", "123456"]
- ["test3", "123456"]
- test:
name: login case1
request:
url: http://127.0.0.1:8000/api/v1/login/
method: POST
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: $user
password: $psw
extract:
- token: content.token # 提取token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.msg, login success!]
- eq: [content.code, 0]
笛卡尔积组合
比如测试账号有四种["test1", "test2", "test3", "test4"],密码也有四种 ["123456", "123456", "123456", "123456"]
用笛卡尔积组合的话,就是4*4=16种组合
# 上海悠悠,QQ交流群:750815713
- config:
name: logincase
parameters:
- user: ["test1", "test2", "test3", "test4"]
- psw: ["123456", "123456", "123456", "123456"]
- test:
name: login case1
request:
url: http://127.0.0.1:8000/api/v1/login/
method: POST
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
json:
username: $user
password: $psw
extract:
- token: content.token # 提取token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.msg, login success!]
- eq: [content.code, 0]
这样运行会生成16组用例,很显然,这种不适用与登录-密码一对一的情况。可以用在其它的测试场景
转:https://www.cnblogs.com/yoyoketang/p/11566567.html