一、前言
前面講的比較理論,本篇主要用實際項目,體現下HttpRunner的一些基本用法。
二、項目場景實例說明
1、業務流程:登錄——創建訂單——領取訂單
2、接口信息如下:
- 登錄:/auth/login_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_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%B93",
"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"
)
.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()
