httprunner 3.x學習4 - 測試用例參數關聯(export)


前言

如何將上個接口的返回token,傳給下個接口當做請求參數? 這是最常見的一個問題了。
解決這個問題其實很簡單,我們只需取出token值,設置為一個中間變量a,下個接口傳這個變量a就可以了。那么接下來就是解決兩個問題:

  • 如何取出token值?
  • 如何參數關聯?

環境: httprunner==3.1.4

場景案例

我現在有一個登陸接口A,登陸成功后返回一個token值。有一個獲取綁定卡號的接口B,但是接口B必須要先登錄后傳登錄的token才能訪問
A接口登錄接口文檔基本信息

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

接口測試

先使用接口測試工具測試下,用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/userinfo/ Authorization:"Token b7e02c959fbae4c2a0d9094f6f9b9a35fa8aaa1e" -v
GET /api/v1/userinfo/ 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.3



HTTP/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

{
    "msg":"success!",
    "code":0,
    "data":[
        {
            "id":105,
            "name":"test",
            "sex":"F",
            "age":24,
            "mail":"xxx@qq.com",
            "create_time":"2020-06-12"
        }]
}

傳頭部參數用xx:xxxx格式,中間用冒號:,如:User-Agent:demo-agent/1.0 'Cookie:a=b;b=c' ,由於Authorization參數中間有空格,用雙引號包起來

yml 格式參數關聯

提取登錄接口返回的 token 值,使用 extract 提取器

    extract:
        token: content.token         # 提取token

在 config 中使用 export 關鍵字導出變量,設置為全局變量,方便后面的步驟調用

config:
    name: logincase
    variables: {}
    export:
    - token

下個接口的用例引用token參數使用$token,完整的用例userinfo.yml如下

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

config:
    name: logincase
    base_url: http://127.0.0.1:8000
    variables: {}
    export:
    - token
teststeps:
-
    name: step1 login
    request:
        url: /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]
-
    name: step2 get user info
    request:
        url: /api/v1/userinfo/
        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.code, 0]
        - eq: [content.data.0.mail, 283340479@qq.com]

pytest 用例結構

config 中 export 導出,主要是為了關聯做准備。導出的變量,主要是用於Step之間參數的傳遞。
如果導出多個變量,格式如下:

       .export(*["token", "var1", "var2"]

userinfo_test.py 對應的代碼如下

# NOTE: Generated By HttpRunner v3.1.4
# FROM: userinfo.yml
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseUserinfo(HttpRunner):

    config = Config("logincase").base_url("http://127.0.0.1:8000").export(*["token"])

    teststeps = [
        Step(
            RunRequest("step1 login")
            .post("/api/v1/login/")
            .with_headers(
                **{
                    "Content-Type": "application/json",
                    "User-Agent": "python-requests/2.18.4",
                }
            )
            .with_json({"username": "test", "password": 123456})
            .extract()
            .with_jmespath("body.token", "token")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal('headers."Content-Type"', "application/json")
            .assert_equal("body.msg", "login success!")
            .assert_equal("body.code", 0)
        ),
        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__":
    TestCaseUserinfo().test_start()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM