httprunner(6)配置信息config

前言

每个测试用例都应该有config部分,可以配置用例级别。比如name、base_url、variables、verify、export等等
 

案例演示

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


class TestCaseRequestWithFunctions(HttpRunner):
    config = (
        Config("request methods testcase with functions")
        .variables(
            **{
                "foo1": "config_bar1",
                "foo2": "config_bar2",
                "expect_foo1": "config_bar1",
                "expect_foo2": "config_bar2",
            }
        )
        .base_url("https://postman-echo.com")
        .verify(False)
        .export(*["foo3"])
    )

    teststeps = [
        Step(
            RunRequest("get with params")
            .with_variables(
                **{"foo1": "bar11", "foo2": "bar21", "sum_v": "${sum_two(1, 2)}"}
            )
            .get("/get")
            .with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"})
            .with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
            .extract()
            .with_jmespath("body.args.foo2", "foo3")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.args.foo1", "bar11")
            .assert_equal("body.args.sum_v", "3")
            .assert_equal("body.args.foo2", "bar21")
        ),
        Step(
            RunRequest("post form data")
            .with_variables(**{"foo2": "bar23"})
            .post("/post")
            .with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}",
                    "Content-Type": "application/x-www-form-urlencoded",
                }
            )
            .with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.form.foo1", "$expect_foo1")
            .assert_equal("body.form.foo2", "bar23")
            .assert_equal("body.form.foo3", "bar21")
        ),
    ]


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

name(必填)

指定用例名称,将在log和报告中展示,下面指定用例名称为testcase baidu案例
用cat命令查看日志中用例的名称

(httprunner_env) ➜  logs cat f39480a1-e4c4-42a0-b301-b2777408cf0c.run.log 
2021-02-07 10:05:53.142 | INFO     | httprunner.runner:test_start:451 - Start to run testcase: testcase baidu案例, TestCase ID: f39480a1-e4c4-42a0-b301-b2777408cf0c
2021-02-07 10:05:53.142 | INFO     | httprunner.runner:__run_step:292 - run step begin: / >>>>>>
2021-02-07 10:05:53.229 | DEBUG    | httprunner.client:request:186 - client IP: 192.168.1.141, Port: 56803
2021-02-07 10:05:53.229 | DEBUG    | httprunner.client:request:194 - server IP: 180.101.49.12, Port: 443


 

base_url(选填)

一般通过的完整url=host地址+path路径(比如:'https://www.baidu.com/s')
base_url就是通用的host地址,实际使用中,通常被用作切换环境
如果base_url被指定,测试步骤中的url只能写相对路径。当你要在不同环境下测试时,这个配置非常有用。
 

案例演示

比如公司目前有2套环境,一套测试环境1地址:192.168.1.100, 一套生成环境2地址:172.111.222.333,两套环境都要执行某用例,这个时候base_url就起到了作用,来看下面演示

 

variables(选填)

测试用例的公共参数,每个测试步骤都可以引用他,比如我一个测试用例中所有的步骤都需要用到version,那么version就可以放在config的variables中。
另外,Step里的变量优先级是比config里的变量要高的,如果有2个同名的变量,那么引用的时候,是优先引用步骤里的变量。
 

verify(选填)

指定是否验证服务器的TLS证书。通常设置为False
当请求https请求时,就会跳过验证。如果你运行时候发现抛错SSLError,可以检查一下是不是verify没传,或者设置了True。
 

export(选填)

指定输出的测试用例变量,主要是用于Step之间参数的传递
比如最常见的面试题:接口测试,下一个接口依赖上一个接口的返回数据?
答:在httpruner中上一个接口使用export导出,下一个接口引用该变量即可

原文地址:https://www.cnblogs.com/jiakecong/p/14384420.html