HttpRunner 3.x 支持三種測試格式危虱,pytest拌消,YAML 和 JSON置蜀。極力推薦使用 pytest 格式編寫和維護(hù)測試用例镣煮,而不是以前的 YAML/JSON 格式姐霍。
格式關(guān)系如下所示:
記錄并生成測試用例
如果 SUT (測試中的系統(tǒng))準(zhǔn)備就緒,最有效的方法是首先捕獲 HTTP 流量,然后使用 HAR 文件生成測試用例镊折。更多詳細(xì)信息請參考 使用HAR文件生成測試用列(testcase),根據(jù)生成的 pytest testcase胯府,您可以根據(jù)需要進(jìn)行一些調(diào)整,因此需要了解 testcase 格式的詳細(xì)信息恨胚。
testcase 的結(jié)構(gòu)
每個(gè) testcase 都是 HttpRunner 的一個(gè)子類骂因,必須具有兩個(gè)類屬性: config 和 teststeps
- config: configure testcase level settings, including 配置 testcase 級別的設(shè)置,包括
base_url
,verify
,variables
,export
. - teststeps: teststep 的列表,每一步都對應(yīng)一個(gè) API 請求或另一個(gè)測試用例引用調(diào)用,支持創(chuàng)建極其復(fù)雜的測試場景的機(jī)制
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()
鏈?zhǔn)秸{(diào)用
HttpRunner3.x 最棒的特性之一是鏈?zhǔn)秸{(diào)用赃泡,您不需要記住任何測試用例格式的細(xì)節(jié)侣签,當(dāng)您在 IDE 中編寫測試用例時(shí),您可以得到智能完成急迂。
config
每個(gè)測試用例都應(yīng)該有一個(gè)配置部分影所,您可以在其中配置測試用例級別的設(shè)置。
name (必須提供)
指定測試用例名稱僚碎。這將顯示在執(zhí)行日志和測試報(bào)告中猴娩。
base_url (可選)
指定 SUT 的公共模式和主機(jī)部分,例如 https://postman-echo.com勺阐。如果指定了 base _ url卷中,則 teststep 中的 url 只能設(shè)置相對路徑部分。如果您想在不同的 SUT 環(huán)境之間進(jìn)行切換渊抽,這尤其有用蟆豫。
variables (可選)
指定是否驗(yàn)證服務(wù)器的 TLS 證書。如果我們想要記錄測試用例執(zhí)行的 HTTP 流量懒闷,這尤其有用十减,因?yàn)槿绻?verify 沒有設(shè)置或者設(shè)置為 True,就會(huì)發(fā)生 SSLError愤估。
SSLError (SSLCertVerificationError (1,’[ SSL: CERTIFICATE _ verify _ failed ]證書驗(yàn)證失敗: 證書鏈中的自簽名證書(_ SSL.c: 1076)’)))
export (可選)
指定 testcase 的導(dǎo)出會(huì)話變量帮辟。將每個(gè)測試用例視為一個(gè)黑盒,config 變量是輸入部分玩焰,config export 是輸出部分由驹。特別是,當(dāng)一個(gè)測試用例在另一個(gè)測試用例的步驟中被引用昔园,并且將被提取一些會(huì)話變量用于后續(xù)的測試步驟蔓榄,那么提取的會(huì)話變量應(yīng)該在配置導(dǎo)出部分中配置。
teststeps
每個(gè)測試用例應(yīng)該有一個(gè)或多個(gè)有序的測試步驟(List [ Step ]) 默刚,每個(gè)步驟都對應(yīng)于一個(gè) API 請求或另一個(gè)測試用例引用調(diào)用甥郑。
注意: 為了簡化,HttpRunner v2.x 中的 API 概念已被否定羡棵。您可以將 API 視為只有一個(gè)請求步驟的測試用例壹若。
RunRequest(name)
RunRequest 用于向 API 發(fā)出請求,并對響應(yīng)進(jìn)行提取或驗(yàn)證。
RunRequest 的參數(shù)名用于指定 teststep 名稱店展,它將顯示在執(zhí)行日志和測試報(bào)告中养篓。
.with_variables
指定 teststep 變量。每個(gè)步驟的變量是獨(dú)立的赂蕴,因此柳弄,如果您想在多個(gè)步驟中共享變量,您應(yīng)該在配置變量中定義變量概说。此外碧注,步驟變量將覆蓋在配置變量中具有相同名稱的變量。
.method(url)
指定 HTTP 方法和 SUT 的 url糖赔,它們對應(yīng)于 requests.request 的方法和 url 參數(shù)萍丐。
.with_params
為請求 url 指定查詢字符串。這對應(yīng)于 requests.request 的 params 參數(shù)放典。
.with_headers
為請求指定 HTTP 頭逝变。這對應(yīng)于 requests.request 的頭參數(shù)。
.with_cookies
指定 HTTP 請求 cookie奋构。這對應(yīng)于 requests.request 的 cookie 參數(shù)壳影。
.with_data
指定 HTTP 請求體,它對應(yīng)于 requests.request 的數(shù)據(jù)參數(shù)弥臼。
.with_json
在 json 中指定 HTTP 請求體宴咧,它對應(yīng)于 requests.request 的 json 參數(shù)。
extract
用 jmespath 提取 JSON 響應(yīng)體径缅。
使用 _ jmespath (jmes _ path: Text掺栅,var _ name: Text)
- jmes_path: 表達(dá)式請參閱JMESPath Tutorial 教程,了解詳情
- var_name: 存儲(chǔ)提取值的變量名芥驳,可以通過后續(xù)的測試步驟引用它
validate
使用 jmespath 提取 JSON 響應(yīng)體并使用期望值進(jìn)行驗(yàn)證柿冲。
assert_XXX(jmes_path: Text, expected_value: Any, message: Text = "")
- jmes_path: 表達(dá)式請參閱JMESPath Tutorial 教程,了解詳情
- expected_value: 這里也可以使用指定的期望值兆旬、變量或函數(shù)引用
- message (可選): 用于指示斷言錯(cuò)誤原因
下面的圖片顯示了 HttpRunner 內(nèi)置驗(yàn)證器。
RunTestCase(name)
在一個(gè)步驟中使用 RunTestCase 來引用另一個(gè) testcase 調(diào)用怎栽。
RunTestCase 的參數(shù)名用于指定 teststep 名稱丽猬,它將顯示在執(zhí)行日志和測試報(bào)告中。
.with_variables
與 RunRequest 的. with _ variables 相同熏瞄。
.call
指定引用的 testcase 類脚祟。
.export
指定會(huì)話變量名稱以便從引用的測試用例導(dǎo)出。導(dǎo)出的變量可以由后續(xù)的測試步驟引用强饮。
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()