天天看点

【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
...
           

继续阅读