一、官方推荐的pytest格式
httprunner可以支持三种格式的用例,分别是pytest、yaml和json。yaml和json是以前的版本所使用的用例格式,但是在3.x版本上,官方强烈建议使用的是pytest格式的用例。

image.png
官方的用例格式关系图,可以看出来,httprunner在对于第三方导出的har文件进行了转换处理,有的人喜欢转换成json,有的人喜欢转换成yaml。但是最终,还是通过解析json格式的文件,生成pytest的python文件。
二.pytest用例格式分析
from httprunner import HttpRunner, Config, Step, RunRequest
from utils.FakerData import *
from api.dlvopenapi import unified_order, order_query
from utils.SigleData import FakerSingleData
class TestCaseDlvOpenApiSuccess(HttpRunner):
config = (
Config("发放成功的测试案例")
.variables(
**{
"appId": "200803014515363414",
"accountName": get_name(),
"idCard": "420624199109037552",
"amount": "0.02",
"signType":"rsa2",
"version":"1.0",
"accountNo": "6225380092315252",
"phone":get_phone(),
"serviceCompanyId": "10000032",
"key": "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOI4fc5POr0cZmyyGuwAonQGNVUvEd8KWtYk2ozNM1S6la7BK7DwWyk6kI10/NsY+Wues+C0fN86Ol0uckTF+s4NqMqw/W8PZ0"
}
)
.base_url("http://1127.0.0.1:8002")
.verify(False)
.export(*["reqNo","outOrderNo"])
)
teststeps = [
Step(
RunRequest("发放收单成功").
setup_hook('${setup_request($request,$key)}')
.with_variables(
**{
"attach": "附件",
"bank": "ICBC1",
"batchNo": "20200801",
"depositBank": "工商",
"extInfo": "API001-商户004-攻城狮",
"foreignNationality": "CN",
"memo": "API_AUTO1",
"outOrderNo": get_outOrderNo(),
"personalIncomeTax": "0",
"phone": "$phone",
"postId": "10468",
"postName": "tester",
"serviceFee": "0",
"serviceType": "10002",
"shouldAmount": "0",
"totalFee": "0",
"nonce": get_nonce(),
"timestamp": get_current_time()}
)
.post(unified_order.UnifiedOrderApi.path)
.with_json(unified_order.UnifiedOrderApi.json)
.teardown_hook('${polling_assert(state_,30)}')
.extract()
.with_jmespath("body.data.reqNo", "reqNo")
.with_jmespath("body.data.outOrderNo", "outOrderNo")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.code", "0000")
.assert_equal("body.msg", "处理成功")
.assert_equal('${db_filed_states_with_parm(state_)}', 30)
.assert_equal('${db_filed_states_with_parm(balance_state_)}', 39)
.assert_equal('${db_filed_states_with_parm(channel_balance_state_)}', 39)
),
Step(
RunRequest("发放订单查询")
.setup_hook('${setup_request($request,$key)}')
.with_variables(**{
"method": "ayg.salary.payQuery",
"nonce": get_nonce(),
"timestamp": get_current_time(),
})
.post(order_query.OrderQueryApi.path)
.with_json(order_query.OrderQueryApi.json)
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.code", "0000")
.assert_equal("body.data.code", "30")
.assert_equal("body.data.msg", "支付成功")
)
]
if __name__ == "__main__":
TestCaseDlvOpenApiSuccess().test_start()
说明:由于喜欢2版本的api分层机制,在3版本并没有强制要求此分层,我还是将单个接口通过类的方式封装了起来,在用例层调用接口类属性完成参数传递。
可以看到:
- 每个
python文件格式的testcase都是HttpRunner的子类 - 每个测试类,必须有两个类属性:
config(元组)和teststeps(列表)。 -
teststeps列表中的单个Step内部通过链式调用RunRequest().setup_hook().with_variables().post().with_json().with_header().with_cookies().validate().assert_equal().....
config
是测试用例级别的配置,包括base_url、verify、variables、export
teststeps:
teststeps是包含测试步骤的列表(list[Step]),每个步骤对应于一个API请求RunRequest(),当然也可以调用另一个testcase,对应的类是RunTestCase()。此外,还支持variables/extract/validate/hooks机制来创建极其复杂的测试场景
链式调用:
链式调用是httprunner 3.x版本一大亮点。在如pycharm等IDE环境编写代的话,会智能提示用例方法。有编写代码的流畅度,再也不用担心yaml格式不正确,没有智能提示或者代码不能智能跳转等痛点了。
httprunner与pytest的联系
-
httprunner中的testcase,就是一个Python文件。因为hrun命令是对pytest命令的封装,所有想要使用hrun或者pytest命令,则用例文件的命名必须要使用xxxx_test.py的格式,才能被pytest检测出来是需要执行的用例文件 -
teststeps列表中的Step,其实就是自己编写pytest脚本时候的一个个def test_xxx():pass。而每一个Step内部,依然是按照传参=>调用接口=>断言的步骤进行。