httprunner3.x debugtalk.py 函数返回值赋值给变量

httprunner3.x 将debugtalk.py返回值赋值给变量

之前看的博客都是直接在用例中引用debugtalk.py的返回值,但是并没有和变量进行关联。

最近我遇到了一个场景:

首先去调用一个保险的投保接口,之后需要去数据库里取order_status值,判断是不是50,不是的话就算用例失败

解决方法:

在runner.py里面 可以发现有这么一段说明

    def __call_hooks(
        self, hooks: Hooks, step_variables: VariablesMapping, hook_msg: Text,
    ) -> NoReturn:
        """ call hook actions.

        Args:
            hooks (list): each hook in hooks list maybe in two format.

                format1 (str): only call hook functions.
                    ${func()}
                format2 (dict): assignment, the value returned by hook function will be assigned to variable.
                    {"var": "${func()}"}

            step_variables: current step variables to call hook, include two special variables

                request: parsed request dict
                response: ResponseObject for current response

            hook_msg: setup/teardown request/testcase

        """

里面举了两个调用hook方法的例子

  • 第一种是${func()} 这种是只调用hook方法,有返回值的话就会拿到返回值
  • 第二种是{"var": "${func()}"} 这种在调用hook方法之后,还可以将返回值赋值给一个变量,在之后的Step中引用

我们需要的正是第二种用法

看一下代码

        Step(
            RunRequest("投保")
                .post("https://*******************/doinsure")
                .with_json({
    
    "channel": "PC"})
                # 这里会将调用返回值给到order_status变量
                .teardown_hook({
    
    "order_status": "${db_select_order_status_by_order_no($orderNo)}"})
                .extract()
                .with_jmespath("cookies.auth_token", "auth_token")
                .validate()
                # 进行断言
                .assert_equal('$order_status', 50)
        ),

运行结果

2021-02-25 10:30:46.000 | INFO     | httprunner.response:validate:246 - assert 50 equal 50(int)	==> pass

猜你喜欢

转载自blog.csdn.net/zjxht62/article/details/114065062