以前我在寫接口自動化用例的時候,為了保證用例的獨立性,需要在setUp里調用各種滿足用例的一些前置條件,其中就不乏調用了其他測試用例中的方法。
而httprunner也是支持了這一項很重要的特性,通過RunTestCase對其他測試用例進行調用,並且還可以導出用例中你所需要的變量,來滿足后續用例的的運行。
首先還是來看下RunTestCase的用法,然后再用實例去實踐。
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")
),
1. RunTestCase(name)
這個參數呢還是一個名稱,畢竟RunTestCase還是一個Step,這個名稱同樣會在日志和報告中顯示。

2. .with_variables
這個變量跟RunRequest里的用法一樣。
3. .call
這里就是指定你要引用的testcase類名稱了。

4. .export
可以指定要導出的變量,以供后續Step引用。
可以看的.export()內部是一個列表[],這里可以用來導出多個變量。

