httprunner怎么引用变量和方法

以登录为例,展示httprunner中怎么引用变量和方法。代码如下:

# NOTE: Generated By HttpRunner v3.1.6
# FROM: har\login.har

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


class TestCaseLogin(HttpRunner):
config = (
Config("登录接口")
.base_url("xx.xx.xx.xx:xxxx")
.verify(False)
.export(*["cks"])
)

teststeps = [
Step(
RunRequest("/login")
.with_variables(
**{
"username": 'xx', #首次定义变量
"password": 'xx', #首次定义变量
"account": "${accountmethod($username,$password)}" # accountmethod是debugtalk中定义的方法,通过${}引用,方法中引用变量直接$+变量名不需要引号。单独引用变量则需要引号,看下面
                }
)
.post("/login")
.with_headers(
**{
"Accept": "application/json, text/javascript, */*; q=0.01",
}
)
.with_data(
{"data": "$account"} #
单独引用变量则需要引号
            )
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.code", 0)
.assert_equal("body.message", "登录成功")
.assert_equal("body.url", "/xxx"
.assert_contains("cookies","xxx")
),
]


if __name__ == "__main__":
TestCaseLogin().test_start()
posted @ 2022-02-22 16:09  我是YINGZI  阅读(224)  评论(0)    收藏  举报