重點:2.x以上版本引數化都需要借助testsuite進行引數化,yml需嚴格縮進格式,不能用tab控制縮進,只能用空格控制
1、直接參考串列進行引數化
2、參考csv檔案進行引數化
3、借助debugtalk輔助函式進行引數化
定義專案的檔案框架
建立四個檔案夾:api、testcases、testsuites、data,分別用來存放介面、用例、用例集、測驗資料
1、撰寫介面yml腳本
在api檔案下,新建一個yml檔案,檔案名為login.yml
腳本必須包含:name--介面名、request---請求、validate--斷言
補充:
request:
headers:
傳參:(post請求:content-type為json時-----json,content-type為form時----可用params/data)
(get請求:用params)
method: POST/GET
url:xxxx/xxxx/xx
login.yml 腳本入下:
name: login_api
base_url: http://xxxxx.xxx.com
request:
headers:
Content-Type: "application/json;charset=UTF-8"
User-Agent:
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30
(KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1 Edg/92.0.4515.159"
# post: content-type為json時-----json,content-type為form時----可用params、data
# get: 用params
json:
account: $account
password: $password
method: POST
url: /nbugs-auth-center-test/xyhauth/checkPwd
validate:
- eq:
- status_code
- 200
- eq:
- body.code
- 200
2、參考介面,撰寫測驗場景腳本
在testcases檔案下,新建一個yml檔案,檔案名為test_case_login.yml
ps:可能是我的專案創建問題,參考api需要使用絕對路徑,正常情況用api/login_test.yml就OK了
- config:
name: login
- test:
name: login_case
api: "test_httprunner/api/login_test.yml"
validate:
- eq:
- status_code
- 200
- eq:
- body.code
- 200
3、測驗用例集,加載用例
方式一:直接參考串列進行引數化
在testsuites檔案下,新建一個yml檔案,檔案名為test_suite_login.yml
參考testcase
參考串列引數
config:
name: testcase description
testcases:
nametest type suites:
testcase: test_httprunner\testcases\test_case_login.yml
parameters:
account-password:
- ["1782683xxxx", "69f600a1be1d7aa6318ea5b274c09ae6"]
- ["user2", "222222"]
- ["1782683xxxx", "333333"]
方式二:參考csv檔案進行引數化
在data檔案夾里面創建一個csv檔案,檔案名test_data_login
account,password
1782683xxxx,69f600a1be1d7aa6318ea5b274c09ae6
user2,222222
1782683xxxx,333333
在testsuites檔案下,新建一個yml檔案,檔案名為test_suite_login.yml
參考testcase
參考引數:${P(路徑)}
config:
name: testcase description
testcases:
nametest type suites:
testcase: test_httprunner\testcases\test_case_login.yml
parameters:
account-password: ${P(test_httprunner/data/test_data_login)}
方式三:使用輔助函式debugtalk引數化
創建一個debugtalk.py檔案
定義一個方法,放引數----嵌套字典的串列
def get_user_login():
user_info = [
{"account": "1782683xxxx", "password": "69f600a1be1d7aa6318ea5b274c09ae6"},
{"account": "1111", "password": "69f600a1be1d7aa6318ea5b274c09ae6"},
{"account": "1782683xxxx", "password": "123752324"}
]
return user_info
在testsuites檔案下,新建一個yml檔案,檔案名為test_suite_login.yml
參考testcase
參考引數:${方法}
config:
name: testcase description
testcases:
nametest type suites:
testcase: testcases\test_case_login.yml
parameters:
account-password: ${get_user_login()}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/355308.html
標籤:其他
上一篇:一、Jmeter總結
