4.写测试用例Testcase

HttpRunner 3.x 支持三种测试格式,pytest,YAML 和 JSON。极力推荐使用 pytest 格式编写和维护测试用例,而不是以前的 YAML/JSON 格式。

格式关系如下所示:



记录并生成测试用例

如果 SUT (测试中的系统)准备就绪,最有效的方法是首先捕获 HTTP 流量,然后使用 HAR 文件生成测试用例。更多详细信息请参考 使用HAR文件生成测试用列(testcase),根据生成的 pytest testcase,您可以根据需要进行一些调整,因此需要了解 testcase 格式的详细信息。

testcase 的结构

每个 testcase 都是 HttpRunner 的一个子类,必须具有两个类属性: config 和 teststeps

  • config: configure testcase level settings, including 配置 testcase 级别的设置,包括base_url, verify, variables, export.
  • teststeps: teststep 的列表,每一步都对应一个 API 请求或另一个测试用例引用调用,支持创建极其复杂的测试场景的机制
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()

链式调用

HttpRunner3.x 最棒的特性之一是链式调用,您不需要记住任何测试用例格式的细节,当您在 IDE 中编写测试用例时,您可以得到智能完成。



config

每个测试用例都应该有一个配置部分,您可以在其中配置测试用例级别的设置。

name (必须提供)

指定测试用例名称。这将显示在执行日志和测试报告中。

base_url (可选)

指定 SUT 的公共模式和主机部分,例如 https://postman-echo.com。如果指定了 base _ url,则 teststep 中的 url 只能设置相对路径部分。如果您想在不同的 SUT 环境之间进行切换,这尤其有用。

variables (可选)

指定是否验证服务器的 TLS 证书。如果我们想要记录测试用例执行的 HTTP 流量,这尤其有用,因为如果 verify 没有设置或者设置为 True,就会发生 SSLError。

SSLError (SSLCertVerificationError (1,’[ SSL: CERTIFICATE _ verify _ failed ]证书验证失败: 证书链中的自签名证书(_ SSL.c: 1076)’)))

export (可选)

指定 testcase 的导出会话变量。将每个测试用例视为一个黑盒,config 变量是输入部分,config export 是输出部分。特别是,当一个测试用例在另一个测试用例的步骤中被引用,并且将被提取一些会话变量用于后续的测试步骤,那么提取的会话变量应该在配置导出部分中配置。

teststeps

每个测试用例应该有一个或多个有序的测试步骤(List [ Step ]) ,每个步骤都对应于一个 API 请求或另一个测试用例引用调用。



注意: 为了简化,HttpRunner v2.x 中的 API 概念已被否定。您可以将 API 视为只有一个请求步骤的测试用例。

RunRequest(name)

RunRequest 用于向 API 发出请求,并对响应进行提取或验证。
RunRequest 的参数名用于指定 teststep 名称,它将显示在执行日志和测试报告中。

.with_variables

指定 teststep 变量。每个步骤的变量是独立的,因此,如果您想在多个步骤中共享变量,您应该在配置变量中定义变量。此外,步骤变量将覆盖在配置变量中具有相同名称的变量。

.method(url)

指定 HTTP 方法和 SUT 的 url,它们对应于 requests.request 的方法和 url 参数。

.with_params

为请求 url 指定查询字符串。这对应于 requests.request 的 params 参数。

.with_headers

为请求指定 HTTP 头。这对应于 requests.request 的头参数。

.with_cookies

指定 HTTP 请求 cookie。这对应于 requests.request 的 cookie 参数。

.with_data

指定 HTTP 请求体,它对应于 requests.request 的数据参数。

.with_json

在 json 中指定 HTTP 请求体,它对应于 requests.request 的 json 参数。

extract

用 jmespath 提取 JSON 响应体。

使用 _ jmespath (jmes _ path: Text,var _ name: Text)

  • jmes_path: 表达式请参阅JMESPath Tutorial 教程,了解详情
  • var_name: 存储提取值的变量名,可以通过后续的测试步骤引用它

validate

使用 jmespath 提取 JSON 响应体并使用期望值进行验证。

assert_XXX(jmes_path: Text, expected_value: Any, message: Text = "")

  • jmes_path: 表达式请参阅JMESPath Tutorial 教程,了解详情
  • expected_value: 这里也可以使用指定的期望值、变量或函数引用
  • message (可选): 用于指示断言错误原因

下面的图片显示了 HttpRunner 内置验证器。


RunTestCase(name)

在一个步骤中使用 RunTestCase 来引用另一个 testcase 调用。

RunTestCase 的参数名用于指定 teststep 名称,它将显示在执行日志和测试报告中。

.with_variables

与 RunRequest 的. with _ variables 相同。

.call

指定引用的 testcase 类。

.export

指定会话变量名称以便从引用的测试用例导出。导出的变量可以由后续的测试步骤引用。

import os
import sys

sys.path.insert(0, os.getcwd())

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

from examples.postman_echo.request_methods.request_with_functions_test import (
    TestCaseRequestWithFunctions as RequestWithFunctions,
)


class TestCaseRequestWithTestcaseReference(HttpRunner):
    config = (
        Config("request methods testcase: reference testcase")
        .variables(
            **{
                "foo1": "testsuite_config_bar1",
                "expect_foo1": "testsuite_config_bar1",
                "expect_foo2": "config_bar2",
            }
        )
        .base_url("https://postman-echo.com")
        .verify(False)
    )

    teststeps = [
        Step(
            RunTestCase("request with functions")
            .with_variables(
                **{"foo1": "testcase_ref_bar1", "expect_foo1": "testcase_ref_bar1"}
            )
            .call(RequestWithFunctions)
            .export(*["foo3"])
        ),
        Step(
            RunRequest("post form data")
            .with_variables(**{"foo1": "bar1"})
            .post("/post")
            .with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}",
                    "Content-Type": "application/x-www-form-urlencoded",
                }
            )
            .with_data("foo1=$foo1&foo2=$foo3")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.form.foo1", "bar1")
            .assert_equal("body.form.foo2", "bar21")
        ),
    ]


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

你可能感兴趣的:(4.写测试用例Testcase)