前言
當我寫了一個登錄的接口用例后,后面會繼續寫查詢個人信息的接口,但是查詢個人信息接口是依賴於先登錄(獲取token)。
像這種業務上操作有先后關系的情況,可以把登錄當一個step來引用。
httprunner3.x 框架弱化了API層的概念,直接寫testcase了,如果是單個接口的用例,可以當step去引用。
場景案例
我現在有一個登陸接口A,登陸成功后返回一個token值。有一個獲取綁定卡號的接口B,但是接口B必須要先登錄后傳登錄的token才能訪問
A接口登錄接口文檔基本信息
- 訪問地址:http://127.0.0.1:8000/api/v1/login/
- 請求類型:POST
- 請求頭部:application/json
- 請求參數:{"username":"test", "password":"123456"}
B接口獲取綁定卡號的接口文檔基本信息
- 訪問地址:http://127.0.0.1:8000/api/v1/userinfo/
- 請求類型:GET
- 請求頭部:Content-Type: application/json
- 請求頭部token參數: Authorization: Token xxxxx login token xxxxx
先不帶token去訪問接口B,使用命令行工具httpie測試接口
C:\Users\dell>http http://127.0.0.1:8000/api/v1/userinfo/
HTTP/1.1 401 Unauthorized
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 58
Content-Type: application/json
Date: Sat, 21 Sep 2019 14:06:15 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept
WWW-Authenticate: Token
X-Frame-Options: SAMEORIGIN
{
"detail": "Authentication credentials were not provided."
}
不帶token會提示沒權限訪問:401 Unauthorized
登錄接口用例
先寫一個登錄的接口用例
# NOTE: Generated By HttpRunner v3.1.4
# FROM: test_login.yml
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseTestLogin(HttpRunner):
config = Config("logincase").base_url("http://127.0.0.1:8000").export(*["token"])
teststeps = [
Step(
RunRequest("steplogin")
.with_variables(**{"user": "test", "psw": "123456"})
.post("/api/v1/login")
.with_json({"username": "$user", "password": "$psw"})
.extract()
.with_jmespath("body.token", "token")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.code", 0)
.assert_equal("body.msg", "login success!")
.assert_length_equal("body.token", 40)
),
]
if __name__ == "__main__":
TestCaseTestLogin().test_start()
查詢個人信息
查詢個人信息接口先用到登錄的token,前面已經寫過登錄了,於是可以直接引用登錄用例, 當成一個測試步驟
RunTestCase(name)
- RunTestCase 在一個步驟中用於引用另一個測試用例調用。
- name RunTestCase 的參數用於指定測試步驟名稱,該名稱將顯示在執行日志和測試報告中。
- .with_variable() 與 RunRequest 的.with_variables 一樣,定義變量
- .call() 指定引用的測試用例類,需先導入這個類。
- .export() 指定要從引用的測試用例導出的會話變量名稱。導出的變量可以被后續的測試步驟引用。
# NOTE: Generated By HttpRunner v3.1.4
# FROM: testcases\login_userinfo.yml
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from testcases.test_login_test import TestCaseTestLogin as TestLogin
class TestCaseLoginUserinfo(HttpRunner):
config = Config("logincase").base_url("http://127.0.0.1:8000").export(*["token"])
teststeps = [
Step(RunTestCase("step1 login")
.call(TestLogin)
.export(*["token"])),
Step(
RunRequest("step2 get user info")
.get("/api/v1/userinfo/")
.with_headers(
**{
"Content-Type": "application/json",
"User-Agent": "python-requests/2.18.4",
"Authorization": "Token $token",
}
)
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "application/json")
.assert_equal("body.code", 0)
.assert_equal("body.data[0].mail", "283340479@qq.com")
),
]
if __name__ == "__main__":
TestCaseLoginUserinfo().test_start()
運行結果:

運行結果是執行2個用例,先執行登錄,再執行后面的查詢。
