httprunner(8)用例調用-RunTestCase
- 2021 年 2 月 9 日
- 筆記
- httprunner
前言
一般我們寫介面自動化的時候,遇到複雜的邏輯,都會調用API方法來滿足前置條件,Pytest的特性是無法用例之間相互調動的,我們一般只調用自己封裝的API方法。
而httprunner支援用例之間的調用,通過RunTestCase
對其他測試用例進行調用,並且還可以導出用例中你所需要的變數,來滿足後續用例的的運行。
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")
),
]
RunTestCase(name)
用於指定測試步驟名稱,該名稱將顯示在執行日誌和測試報告中。
RunTestCase("request with functions")
.with_variables
與RunRequest里的用法相同
.call
指定你要引用的testcase類名稱
.call(RequestWithFunctions)
調用RequestWithFunctions類
.export
指定要導出的變數(可以指定多個),後續的測試步驟可以引用導出的變數
.export(*["foo3", "foo4"])