【HttpRunner学习笔记】5- HttpRunner 抽离变量

引言

前面的章节介绍了,如何去抽离base_url做一个全局变量,那么我们在实际应用中,不仅只有url是变动不频繁,还有一些不需要改动的变量。比如:当只有一个用户场景操作的业务,此时只需要一个用户即可,那么我们就可以选择将账号以及密码抽离出来放在config属性里面作为一个全局变量,或者声明变量名之后存储在.env文件中。

1. py测试用例抽离变量参数

首先在Step中需要引用变量的地方使用${变量名} 进行引用,然后在config >> .variables(**{})声明变量名以及它的值:

 config = Config("testcase description")\
        .variables(
        **{
            "pn": "1",
            "ps": "20"
            }
        )\
        ...

    teststeps = [
        Step(
            ...
            .with_params(**{"PN": "${pn}", "PS": "${ps}"})
            ...

在这里插入图片描述

2. yml测试用例抽离变量参数

config:
    name: testcase description
    variables:
        pn: '1'
        ps: '20'
    verify: false
teststeps:
    ...
    request:
        ...
        params:
            PN: ${pn}
            PS: ${ps}
        ...

3. 完整代码

# NOTE: Generated By HttpRunner v3.1.4
# FROM: har\woshipm_get_author.yml


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


class TestCaseWoshipmGetAuthor(HttpRunner):

    config = Config("testcase description").base_url("${ENV(base_url)}").verify(False)

    teststeps = [
        Step(
            RunRequest("/api2/user/followings/status")
            .get("/api2/user/followings/status")
            .with_params(**{"authorIds": "801994"})
            .with_headers(
                **{
                    "Accept": "application/json, text/plain, */*",
                    "Accept-Encoding": "gzip, deflate",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "COMMON_ACCESS_TOKEN": "",
                    "COMMON_ACCESS_TOKEN_SECRET": "",
                    "Connection": "keep-alive",
                    "Cookie": "",
                    "Host": "www.woshipm.com",
                    "Referer": "http://www.woshipm.com/u/801994",
                    "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",
                    "X-WP-Nonce": "452000896a",
                }
            )
            .with_cookies(
                **{
                    "Hm_lpvt_b85cbcc76e92e3fd79be8f2fed0f504f": "1628342649",
                    "Hm_lvt_b85cbcc76e92e3fd79be8f2fed0f504f": "1628163442,1628172667,1628334320",
                    "_DAU": "done%21",
                    "article_detail_normal": "article_user_profile@article_items",
                    "article_index": "",
                    "article_user_profile": "article_recom_users@author_items",
                    "dts_device_info": "",
                    "firstVisitTime": "1628342605147",
                    "isOverlayShowx": "VVVV",
                    "new_device_id": "device_0ca45ac5-91b5-48f1-989f-09d72908c13f",
                    "post_view_4751263": "2",
                    "s": "",
                    "t": "",
                    "wordpress_logged_in_41071e66c7856d347ba575b5339c5a86": "",
                }
            )
            .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.HOST_ID", "localhost")
        ),
    ]


if __name__ == "__main__":
    TestCaseWoshipmGetAuthor().test_start()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值