前言
參數化是自動化測試離不開的話題,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:\soft\untitled>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:\soft\untitled\reports\1569118975.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組用例,很顯然,這種不適用與登錄-密碼一對一的情況。可以用在其它的測試場景