天天看點

【HttpRunner學習筆記】6- HttpRunner 提取變量

引言

在我們在做接口測試的時候,經常會遇到一個一些動态變化的參數,或者是有依賴性的測試用例。也就是一個請求需要的參數是由上一個請求響應傳回的值;

介紹本章節前先抛出幾個場景:

  • 如果通路擷取指定作者的資料,但是這個作者

    ID

    如果寫死的話,就會造成後續對測試用例的維護難度,那麼能否根據去擷取到所有的作者清單,然後提取到這個作者

    ID

    ,然後将這個

    ID

    傳遞到我要擷取指定作者的

    API

    呢?在

    httprunner

    中測試用例依賴也可以看做是

    step

    的響應導出參數由另一個

    step

    調用;
  • 有些場景在校驗的時候會使用一個

    request_id

    ,與響應傳回的

    request_id

    進行驗證,這個

    request_id

    也是動态生成的;

1. 使用

extract

提取參數

擷取響應傳回的資料,在提取參數的時候,當HTTP請求響應的結果為

JSON

格式,則可以采用

.

運算符的方式,逐級往下擷取到參數值;

響應結果的整體内容引用方式為

content

或者是

body

,在我使用如下接口:

http://www.woshipm.com//api2/recommendation/authors/with-article-info

【HttpRunner學習筆記】6- HttpRunner 提取變量

如圖所示,如果我要擷取到傳回的

request_id

body.REQUEST_ID
           

示例:

teststeps = [
        Step(
            RunRequest("/api2/recommendation/authors/with-article-info")
            .get("/api2/recommendation/authors/with-article-info")
            .with_params(**{"PN": "${pn}", "PS": "${ps}"})
            .with_headers(
                **{
                    "Host": "www.woshipm.com",
                    "Accept": "application/json, text/plain, */*",
                    "X-WP-Nonce": "452000896a",
                    "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",
                    "Referer": "http://www.woshipm.com/users",
                    "Accept-Encoding": "gzip, deflate",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Cookie": ...
                    "Connection": "keep-alive",
                }
            )
            .with_cookies(
                **{
                    ...
                }
            )
            .extract()
            .with_jmespath("body.REQUEST_ID", "request_id")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal('headers."Content-Type"', "application/json;charset=UTF-8")
            .assert_equal("body.CODE", 200)
            .assert_equal("body.MESSAGE", "請求成功")
            .assert_equal("body.REQUEST_ID", "${request_id}")
            .assert_equal("body.HOST_ID", "localhost")
           

在示例中使用

.extract().with_jmespath('提取表達式','聲明引用的變量名')

,比如:

  • 提取表達式:

    body.REQUEST_ID

  • 聲明更引用過的變量名:

    request_id

  • 引用的時候使用

    ${request_id}

    即可

yml

格式的測試用例是如何使用的呢:

【HttpRunner學習筆記】6- HttpRunner 提取變量

2.使用

extract

提取多個參數

【HttpRunner學習筆記】6- HttpRunner 提取變量

3. 使用

export

導出提取的變量

config

中使用

export

是為了導出提取的變量,便于被調用依賴用例後續步驟中使用,比如我之前提到的,

step1

會傳回一個

authorId

,然後這個

authorId

将會傳遞到

step2

,提取表達式如下:

【HttpRunner學習筆記】6- HttpRunner 提取變量

如圖所示,如果我要擷取到傳回的

authorId

body.RESULT.authors[0].authorId
           

示例:

class TestCaseWoshipmRecommendedAuthor(HttpRunner):

    config = Config("request methods testcase with functions")\
        ...
        .export(*["author_id"])
    teststeps = [
        Step(
            RunRequest("/api2/recommendation/authors/with-article-info")
            ...
            
            .extract()
            .with_jmespath("body.RESULT.authors[0].authorId", "author_id")
            
            ...
        ),
        Step(
            RunRequest("/api2/user/followings/status")
                .get("/api2/user/followings/status")
            
                .with_params(**{"authorIds": "${author_id}"})
				...
        )
    ]
           

在示例中,我們可以看到在第一個請求

Step

中使用

.extract().with_jmespath

提取了

authorId

并聲明為

author_id

,然後在

config

導出提取的

author_id

,被第二個請求

Step

引用

${author_id}

那麼在

yml

格式中是怎麼應用的呢?

config:
    name: testcase description
    variables: {}
    verify: false
    export:
        - author_id
...
           

繼續閱讀