从 2.0.0 版本开始,HttpRunner 不再支持在测试用例文件中进行参数化配置;参数化的功能需要在 testsuite 中实现。如需实现数据驱动机制,需要创建一个 testsuite,在 testsuite 中引用测试用例,并定义参数化配置。

这是一个测试套件文件格式:

HttpRunner接口自动化测试框架--4.参数化操作(parameters)

 如需对某测试用例(testcase)实现参数化数据驱动,需要使用 parameters 关键字,定义参数名称并指定数据源取值方式。

参数名称的定义分为两种情况:独立参数单独进行定义;多个参数具有关联性的参数需要将其定义在一起,采用短横线(-)进行连接。

数据源指定支持三种方式:

1.在 YAML/JSON 中直接指定参数列表:该种方式最为简单易用,适合参数列表比较小的情况

2.通过内置的 parameterize(可简写为P)函数引用 CSV 文件:该种方式需要准备 CSV 数据文件,适合数据量比较大的情况

3.调用 debugtalk.py 中自定义的函数生成参数列表:该种方式最为灵活,可通过自定义 Python 函数实现任意场景的数据驱动机           制,当需要动态生成参数列表时也需要选择该种方式。

参数配置详解

1.独立参数 & 直接指定参数列表

 HttpRunner接口自动化测试框架--4.参数化操作(parameters)

 1 D:\Python_Script\Interface_Test>hrun testsuites/data_get_phone.yaml
 2 INFO     HttpRunner version: 2.4.2
 3 INFO     Loading environment variables from D:\Python_Script\Interface_Test\.env
 4 INFO     Start to run testcase: 获取手机号码归属地接口
 5 获取去手机号码归属地
 6 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 7 INFO     status_code: 200, response_time(ms): 276.39 ms, response_length: 133 bytes
 8 
 9 .
10 
11 ----------------------------------------------------------------------
12 Ran 1 test in 0.281s
13 
14 OK
15 INFO     Start to run testcase: 获取手机号码归属地接口
16 获取去手机号码归属地
17 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
18 INFO     status_code: 200, response_time(ms): 98.75 ms, response_length: 148 bytes
19 
20 .
21 
22 ----------------------------------------------------------------------
23 Ran 1 test in 0.102s
24 
25 OK
26 INFO     Start to run testcase: 获取手机号码归属地接口
27 获取去手机号码归属地
28 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
29 INFO     status_code: 200, response_time(ms): 78.79 ms, response_length: 134 bytes
30 
31 .
32 
33 ----------------------------------------------------------------------
34 Ran 1 test in 0.083s
35 
36 OK
37 INFO     Start to render Html report ...
38 INFO     Generated Html report: D:\Python_Script\Interface_Test\reports\1576583453.html
运行日志信息

相关文章: