序言
httprunner的使用和其中的一些概念在官方的文檔中已經(jīng)詳細(xì)說(shuō)明,寫這個(gè)學(xué)習(xí)記錄是為了記錄下自己的學(xué)習(xí)歷程,能夠在今后快速的深入學(xué)習(xí)和實(shí)踐。沒(méi)有提及的部分,請(qǐng)查看官方文檔
版本:2.2.5
參考
testcases的編寫 ?
testcase是包含多個(gè)測(cè)試步驟的集合。每一個(gè)測(cè)試步驟是一次請(qǐng)求,各個(gè)測(cè)試之間可能有相同的請(qǐng)求參數(shù),他們相互關(guān)聯(lián),又有順序依賴。
測(cè)試用例register.yml中可以只有一個(gè)請(qǐng)求
- test:
name: 開發(fā)者注冊(cè)
request:
method: POST
url: https://api.apiopen.top/developerRegister
data:
name: o_chen
passwd: 123456
email: 2282440078@qq.com
validate:
- eq: [content.code,200]
多個(gè)請(qǐng)求之前存在關(guān)聯(lián)關(guān)系
測(cè)試用例user_register.yml中多個(gè)請(qǐng)求之前存在關(guān)聯(lián)關(guān)系
開發(fā)者登錄,獲取apikey,用戶注冊(cè)是需要傳入apikey
- test:
name: 開發(fā)者登錄
request:
method: POST
url: https://api.apiopen.top/developerLogin
data:
name: o_chen
passwd: 123456
extract:
apikey: content.result.apikey
validate:
- eq: [content.code,200]
- test:
name: 用戶注冊(cè)
request:
method: POST
url: https://api.apiopen.top/registerUser
data:
apikey: $apikey
name: user1
passwd: 123456
nikeName: user1
validate:
- eq: [content.code,200]
公共處理的config
將請(qǐng)求中公有的部分可以獨(dú)立出來(lái),寫入到config中
- config:
name: 開發(fā)者登錄后注冊(cè)
base_url: https://api.apiopen.top
variables:
developer_name: o_chen
passwd: 123456
success_code: 200
- test:
name: 開發(fā)者登錄
request:
method: POST
url: /developerLogin
data:
name: $developer_name
passwd: $passwd
extract:
apikey: content.result.apikey
validate:
- eq: [content.code,$success_code]
- test:
name: 用戶注冊(cè)
request:
method: POST
url: /registerUser
data:
apikey: $apikey
name: user2
passwd: $passwd
nikeName: user2
validate:
- eq: [content.code,$success_code]
獨(dú)立的api接口
將請(qǐng)求獨(dú)立到api中,方便在不同的用例中引用,api編寫接口時(shí),動(dòng)態(tài)的參數(shù)以變量的形式編寫,每個(gè)api盡可能的能夠單獨(dú)調(diào)用成功。在測(cè)試用例中通過(guò)關(guān)鍵字api引用接口的yaml文件
開發(fā)者登錄接口
name: 開發(fā)者登錄
base_url: https://api.apiopen.top
variables:
developer_name: o_chen
passwd: 123456
success_code: 200
request:
method: POST
url: /developerLogin
data:
name: $developer_name
passwd: $passwd
extract:
apikey: content.result.apikey
validate:
- eq: [content.code,$success_code]
用戶注冊(cè)接口
name: 用戶注冊(cè)
base_url: https://api.apiopen.top
variables:
user_name: user0
passwd: 123456
success_code: 200
request:
method: POST
url: /registerUser
data:
apikey: $apikey
name: $user_name
passwd: $passwd
nikeName: $user_name
extract:
apikey: content.result.apikey
validate:
- eq: [content.code,$success_code]
用例中引用接口
- config:
name: 開發(fā)者登錄后注冊(cè)
base_url: https://api.apiopen.top
variables:
developer_name: o_chen
passwd: 123456
success_code: 200
- test:
name: 開發(fā)者登錄
api: api/apiopen/developer_login.yml
- test:
name: 用戶注冊(cè) user3
api: api/apiopen/register_user.yml
variables:
user_name: user3
- test:
name: 用戶注冊(cè) user4
api: api/apiopen/register_user.yml
variables:
user_name: user4
- test:
name: 用戶注冊(cè) user5
api: api/apiopen/register_user.yml
variables:
user_name: user5
用例中引用testcase
teststeps:
-
testcase: testcases/sale/login.yml
output:
- token
-
testcase: testcases/sale/order_list.yml
output:
- orderList
其中,output輸入的是變量名稱,將引入的子測(cè)試用例中的變量添加到當(dāng)前的測(cè)試用例中。output關(guān)鍵字也可以寫在子測(cè)試用例的config節(jié)點(diǎn)中。同時(shí)在運(yùn)行時(shí)會(huì)把變量打印出來(lái)。
testcase中的優(yōu)先級(jí)
base_url
testcase test > testcase config > testsuite test > testsuite config > api
variables
testcase config > testcase test > testcase_def config > testcase_def test > api
verify
testcase teststep (api) > testcase config > testsuite config