HttpRunner3.X - 專案例項一
阿新 • • 發佈:2021-07-17
一、前言
前面講的比較理論,本篇主要用實際專案,體現下HttpRunner的一些基本用法。
二、專案場景例項說明
1、業務流程:登入——建立版單——領取版單
2、介面資訊如下:
- 登入:/auth/login_by_password
- 建立版單:type/add
- 領取版單:type/received
3、介面依賴說明:
- 建立版單的前提是要先登入系統
- 領取版單需要獲取前一個介面步驟返回的id
三、測試用例程式碼
1、登入介面用例
# NOTE: Generated By HttpRunner v3.1.5 # FROM: har\login.har from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase class TestCaseLogin(HttpRunner): config = Config("登入")\ .verify(False)\ .base_url("https://XXX.com")\ #在config定義url後,teststeps裡就直接寫介面路徑就行了 .variables( **{"username":"李白","psw":"123456"} #在config定義變數,step裡都可以用 )\ .export(*["userId","userNo","userName"]) #下面step提取引數設為變數後,在config裡匯出,好處是其他步驟跟用例都可以引用 teststeps = [ Step( RunRequest("登入介面") .post("/auth/login_by_password") .with_headers( **{ "accept": "application/json, text/plain, */*", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "content-type": "application/json;charset=UTF-8", "accept-language": "zh-CN,zh;q=0.9", } ) .with_json( { "employeeName": "$username", #引用config定義的變數 "loginType": "PASSWORD", "password": "$psw", #引用config定義的變數 } ) .extract() #提取返回引數 .with_jmespath("body.data.userId","userId") .with_jmespath("body.data.userNo","userNo") .with_jmespath("body.data.userName","userName") .validate() #斷言 .assert_equal("status_code", 200) .assert_equal("body.successful", True) .assert_equal("body.code", "200") .assert_equal("body.message", "請求成功") ), ] if __name__ == "__main__": TestCaseLogin().test_start()
2、業務流程用例
# NOTE: Generated By HttpRunner v3.1.5 # FROM: har\received.har from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase from testcases.login_test import TestCaseLogin #因為要引用登入用例,所以要先import class TestCaseReceived(HttpRunner): config = Config("業務主流程")\ .verify(False)\ .base_url("https://XXX.cn")\ .export(*["prototypeId"]) teststeps = [ Step(
#呼叫另一個用例時,注意是用RunTestCase("自己隨意命名").call(另一個用例的類名) RunTestCase("呼叫登入用例").call(TestCaseLogin).export(*["userId","userNo","userName"]) ), Step( RunRequest("建立版單介面") .post("/XXX/type/add") .with_headers( **{ "OSChannel": "web", "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "CURRENT-USER-ID": "100", "Content-Type": "application/json;charset=UTF-8", "CURRENT-USER-NAME": "%E6%E5%B9%B3", "Accept-Language": "zh-CN,zh;q=0.9", } ) .with_json( { "pictureList": [ { "url": "https://794802905c9a3699256.png", "pictureTypeEnum": "POSITIVE", }, ], "purchaserInfo": { "bdId": "100", }, "purchaserSource": "", "saleGroup": "國內-全國", "deliveryTime": "2021-07-16 00:00:00", "category": "男裝-上裝-襯衫-", "referType": "IMG", "referRemark": "同款復版", ) .extract() #提取返回引數,下面的領取介面需要用到這個id,注意要在config裡export .with_jmespath("body.data.prototypeId","prototypeId") .validate() .assert_equal("status_code", 200) .assert_equal('headers."Content-Type"', "application/json") .assert_equal("body.successful", True) .assert_equal("body.code", "200") .assert_equal("body.message", "請求成功") ), Step( RunRequest("領取介面") .put("/XXXtype/received") .with_headers( **{ "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "CURRENT-USER-ID": "100", "Content-Type": "application/json;charset=UTF-8", "CURRENT-USER-NAME": "%EB7%E5%B9%B3", "Accept-Language": "zh-CN,zh;q=0.9", } ) .with_json( { "prototypeId": "$prototypeId", #直接$引用變數就行了 "currentUserId": "1078", "workerId": "1078", "currentUserName": "李白", "currentUserCode": "00878", } ) .validate() .assert_equal("status_code", 200) .assert_equal('headers."Content-Type"', "application/json") .assert_equal("body.successful", True) .assert_equal("body.code", "200") .assert_equal("body.message", "請求成功") .assert_equal("body.data", None) ), ] if __name__ == "__main__": TestCaseReceived().test_start()