httprunner學習3-extract提取token值參數關聯
- 2019 年 10 月 6 日
- 筆記
前言
如何將上個介面的返回token,傳給下個介面當做請求參數?這是最常見的一個問題了。 解決這個問題其實很簡單,我們只需取出token值,設置為一個中間變數a,下個介面傳這個變數a就可以了。那麼接下來就是解決兩個問題:
- 如何取出token值?
- 如何參數關聯?
場景案例
我現在有一個登陸介面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/user/info/
- 請求類型:GET
- 請求頭部:Content-Type: application/json
- 請求頭部token參數:Authorization: Token xxxxx login token xxxxx
先不帶token去訪問介面B,使用命令行工具httpie測試介面
C:Usersdell>http http://127.0.0.1:8000/api/v1/user/info/ 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
介面測試
先使用介面測試工具測試下,用postman,或者fiddler都可以,我這裡為了查看報文資訊方便,用httpie命令行工具
先訪問介面A獲取token值234af73571da46ade79ea6a74961b1d23d609b79
D:>http http://127.0.0.1:8000/api/v1/login/ username=test password=123456 -v POST /api/v1/login/ HTTP/1.1 Accept: application/json, */* Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 42 Content-Type: application/json Host: 127.0.0.1:8000 User-Agent: HTTPie/1.0.3{ "password": "123456", "username": "test" }HTTP/1.1 200 OK Allow: POST, OPTIONS Content-Length: 109 Content-Type: application/json Date: Sat, 21 Sep 2019 15:37:06 GMT Server: WSGIServer/0.2 CPython/3.6.0 Vary: Accept, Cookie X-Frame-Options: SAMEORIGIN{ "code": 0, "msg": "login success!", "token": "234af73571da46ade79ea6a74961b1d23d609b79", "username": "test" }
傳給下個介面B
D:>http http://127.0.0.1:8000/api/v1/user/info/ Authorization:"Token b7e02c959fbae4c2a0d9094f6f9b9a35fa8aaa1e" -v GET /api/v1/user/info/ HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate Authorization: Token b7e02c959fbae4c2a0d9094f6f9b9a35fa8aaa1e Connection: keep-alive Host: 127.0.0.1:8000 User-Agent: HTTPie/1.0.3HTTP/1.1 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Length: 96 Content-Type: application/json Date: Sat, 21 Sep 2019 16:04:25 GMT Server: WSGIServer/0.2 CPython/3.6.0 Vary: Accept X-Frame-Options: SAMEORIGIN[ { "age": 20, "create_time": "2019-09-15", "id": 1, "mail": "[email protected]", "name": "yoyo", "sex": "M" } ]
傳頭部參數用xx:xxxx格式,中間用冒號:,如:User-Agent:demo-agent/1.0 'Cookie:a=b;b=c'
,由於Authorization參數中間有空格,用雙引號包起來
extract提取token
提取登錄介面返回的token值,使用extract提取器
extract: - token: content.token
下個介面的用例引用token參數使用$token
,完整的用例test_info.yml如下
# 上海悠悠,QQ交流群:750815713 - config: name: logincase variables: {} - test: name: login case1 request: url: http://127.0.0.1:8000/api/v1/login/ method: POST headers: Content-Type: application/json User-Agent: python-requests/2.18.4 json: username: test password: 123456 extract: - token: content.token # 提取token validate: - eq: [status_code, 200] - eq: [headers.Content-Type, application/json] - eq: [content.msg, login success!] - eq: [content.code, 0] # 上海悠悠,QQ交流群:750815713 - test: name: get user info case1 request: url: http://127.0.0.1:8000/api/v1/user/info/ method: GET headers: Content-Type: application/json User-Agent: python-requests/2.18.4 Authorization: Token $token # 引用token validate: - eq: [status_code, 200] - eq: [headers.Content-Type, application/json] - eq: [content.0.age, 20] - eq: [content.0.name, yoyo] - eq: [content.0.mail, [email protected]]
運行用例
hrun test_info.yml
D:softuntitled>hrun test_info.yml login case1 INFO POST http://127.0.0.1:8000/api/v1/login/ INFO status_code: 200, response_time(ms): 516.0 ms, response_length: 109 bytes INFO start to extract from response object. INFO start to validate. . get user info case1 INFO GET http://127.0.0.1:8000/api/v1/user/info/ INFO status_code: 200, response_time(ms): 188.8 ms, response_length: 96 bytes INFO start to validate. .---------------------------------------------------------------------- Ran 2 tests in 0.713sOK INFO Start to render Html report ... INFO Generated Html report: D:softuntitledreports1569079832.html
查看report報告
打開report目錄下生成的報告文件

打開報告詳情,可以看到token引用成功了