大家好,又见面了,我是你们的朋友全栈君。
编写测试用例
HttpRunner v3.x支持三种测试用例格式pytest
,YAML
和JSON
。官方强烈建议以pytest
格式而不是以前的YAML/JSON
格式编写和维护测试用例
格式关系如下图所示:
图文解析:
1.postman收集的用例,curl命令行、抓包工具charles/fiddler生成的har,都转换为JSON
格式
2.Web平台中的JSON格式用例也被收集
3.YAML文件与JSON格式相互转换
4.最后将所有的JSON格式文件转换为py文件,用pytest去执行用例
最后都是生成pytest去执行用例,之前就一直用的pytest框架,所以本人还是很喜欢httprunner的设计的
用例结构
上篇文章将百度的接口转换成pytest,如下:
代码语言:javascript复制from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseBaidu(HttpRunner):
config = Config("testcase description").verify(False)
teststeps = [
Step(
RunRequest("/")
.get("https://www.baidu.com/")
.with_headers(
**{
"Host": "www.baidu.com",
"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"sec-ch-ua": '"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36",
"Accept": "text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Sec-Fetch-Site": "none",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Cookie": "PSTM=1610459846; BAIDUID=A40AD6AD806FBBED1033903732FFA453:FG=1; BD_UPN=123253; BIDUPSID=898B917A8EF92E036B0F06DC792638A1; __yjs_duid=1_c391922164c20246fd49e4ad821d46c41610613507042; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=33423_33513_33403_33273_33594_33585_26350_33568; delPer=0; BD_CK_SAM=1; PSINO=5; BAIDUID_BFESS=A40AD6AD806FBBED1033903732FFA453:FG=1; COOKIE_SESSION=86630_1_9_6_41_5_0_0_8_2_1_0_0_0_71_0_1612340974_1611912344_1612427533|9#262013_235_1611912339|9; BD_HOME=1; ZD_ENTRY=baidu; BA_HECTOR=20ag2180aga18k21mm1g1pegu0r",
}
)
.with_cookies(
**{
"PSTM": "1610459846",
"BAIDUID": "A40AD6AD806FBBED1033903732FFA453:FG=1",
"BD_UPN": "123253",
"BIDUPSID": "898B917A8EF92E036B0F06DC792638A1",
"__yjs_duid": "1_c391922164c20246fd49e4ad821d46c41610613507042",
"BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
"H_PS_PSSID": "33423_33513_33403_33273_33594_33585_26350_33568",
"delPer": "0",
"BD_CK_SAM": "1",
"PSINO": "5",
"BAIDUID_BFESS": "A40AD6AD806FBBED1033903732FFA453:FG=1",
"COOKIE_SESSION": "86630_1_9_6_41_5_0_0_8_2_1_0_0_0_71_0_1612340974_1611912344_1612427533|9#262013_235_1611912339|9",
"BD_HOME": "1",
"ZD_ENTRY": "baidu",
"BA_HECTOR": "20ag2180aga18k21mm1g1pegu0r",
}
)
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "text/html;charset=utf-8")
),
]
if __name__ == "__main__":
TestCaseBaidu().test_start()
每个用例都是HttpRunner的子类,且必须包含属性:config和teststeps。
- config:配置测试用例,包含 base_url, verify, variables, export
- teststeps:测试步骤的列表,每个步骤都对应一个API请求或调用另一个测试用例,此外还支持variables/extract/validate/hooks创建极其复杂的测试用例
链式调用
HttpRunner v3.x最重要的功能之一就是支持链式调用,使用链式调用,不需要记住任何测试用例的信息,在IDE中编写测试用例时就可以智能完成
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/166283.html原文链接:https://javaforall.cn