In the past, when I was writing interface automation use cases, in order to ensure the independence of the use cases, I needed to call various preconditions in setUp that met the use case, and some of them called methods in other test cases.

Httprunner supports this important feature as well. Calls to other test cases via RunTestCase can also export variables that you need for subsequent use cases.

Let’s look at the use of RunTestCase first, and then practice with an example.

    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")
        ),
Copy the code

1. RunTestCase(name)

This parameter is also a name, after all RunTestCase is a Step, and this name will also be displayed in the log and report.

2. .with_variables

This variable is used the same way as in RunRequest.

3. .call

This is where you specify the name of the TestCase class you want to reference.

4. .export

You can specify variables to export for reference by subsequent steps.

Inside export () is a list [], which can be used to export multiple variables.