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

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

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

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

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

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

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

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

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

参数配置详解

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

 

 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
运行日志信息

2.关联参数 & 直接指定参数列表

 1 D:\Python_Script\Interface_Test>hrun testsuites/jinrong.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     POST http://192.168.200.144:8080/futureloan/mvc/api/member/register
 7 INFO     status_code: 200, response_time(ms): 53.86 ms, response_length: 60 bytes
 8 
 9 .
10 
11 ----------------------------------------------------------------------
12 Ran 1 test in 0.091s
13 
14 OK
15 INFO     Start to run testcase: 这是第一个测试用例
16 金融项目注册接口
17 INFO     POST http://192.168.200.144:8080/futureloan/mvc/api/member/register
18 INFO     status_code: 200, response_time(ms): 91.76 ms, response_length: 60 bytes
19 
20 .
21 
22 ----------------------------------------------------------------------
23 Ran 1 test in 0.148s
24 
25 OK
26 INFO     Start to render Html report ...
27 INFO     Generated Html report: D:\Python_Script\Interface_Test\reports\1576583785.html
运行日志信息

3.独立参数 & 引用 CSV 文件

对于 CSV 数据文件,需要遵循如下几项约定的规则:

1.CSV 文件中的第一行必须为参数名称,从第二行开始为参数值,每个(组)值占一行;

2.若同一个 CSV 文件中具有多个参数,则参数名称和数值的间隔符需实用英文逗号;

 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): 66.95 ms, response_length: 130 bytes
 8 
 9 .
10 
11 ----------------------------------------------------------------------
12 Ran 1 test in 0.071s
13 
14 OK
15 INFO     Start to render Html report ...
16 INFO     Generated Html report: D:\Python_Script\Interface_Test\reports\1576584334.html
17 
18 D:\Python_Script\Interface_Test>hrun testsuites/data_get_phone.yaml
19 INFO     HttpRunner version: 2.4.2
20 INFO     Loading environment variables from D:\Python_Script\Interface_Test\.env
21 INFO     Start to run testcase: 获取手机号码归属地接口
22 获取去手机号码归属地
23 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
24 INFO     status_code: 200, response_time(ms): 55.88 ms, response_length: 130 bytes
25 
26 .
27 
28 ----------------------------------------------------------------------
29 Ran 1 test in 0.069s
30 
31 OK
32 INFO     Start to render Html report ...
33 INFO     Generated Html report: D:\Python_Script\Interface_Test\reports\1576584482.html
34 
35 D:\Python_Script\Interface_Test>hrun testsuites/data_get_phone.yaml
36 INFO     HttpRunner version: 2.4.2
37 INFO     Loading environment variables from D:\Python_Script\Interface_Test\.env
38 INFO     Start to run testcase: 获取手机号码归属地接口
39 获取去手机号码归属地
40 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
41 INFO     status_code: 200, response_time(ms): 64.87 ms, response_length: 130 bytes
42 
43 .
44 
45 ----------------------------------------------------------------------
46 Ran 1 test in 0.069s
47 
48 OK
49 INFO     Start to render Html report ...
50 INFO     Generated Html report: D:\Python_Script\Interface_Test\reports\1576585040.html
运行日志信息

4.关联参数 & 引用 CSV 文件

 

  1 D:\Python_Script\Interface_Test>hrun testsuites/jinrong.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://192.168.200.144:8080/futureloan/mvc/api/member/register
  7 INFO     status_code: 200, response_time(ms): 133.64 ms, response_length: 60 bytes
  8 
  9 ERROR   
 10 validate: content.status equals 1(str)  ==> fail
 11 1(int) equals 1(str)
 12 ERROR    ******************************** DETAILED REQUEST & RESPONSE ********************************
 13 ====== request details ======
 14 url: http://192.168.200.144:8080/futureloan/mvc/api/member/register
 15 method: GET
 16 headers: {}
 17 params: {'mobilephone': '15664587568', 'pwd': 'xaiohua', 'regname': 'xiaohua'}
 18 verify: True
 19 
 20 ====== response details ======
 21 status_code: 200
 22 headers: {'Server': 'Apache-Coyote/1.1', 'Content-Type': 'application/json;charset=UTF-8', 'Transfer-Encoding': 'chunked
 23 ', 'Date': 'Tue, 17 Dec 2019 12:53:56 GMT'}
 24 body: '{"status":1,"code":"10001","data":null,"msg":"注册成功"}'
 25 
 26 F
 27 
 28 ======================================================================
 29 FAIL: test_0000_000 (httprunner.api.TestSequense)
 30 金融项目注册接口
 31 ----------------------------------------------------------------------
 32 Traceback (most recent call last):
 33   File "d:\programinstall\python_program\lib\site-packages\httprunner\api.py", line 61, in test
 34     test_runner.run_test(test_dict)
 35 httprunner.exceptions.ValidationFailure:
 36 validate: content.status equals 1(str)  ==> fail
 37 1(int) equals 1(str)
 38 
 39 During handling of the above exception, another exception occurred:
 40 
 41 Traceback (most recent call last):
 42   File "d:\programinstall\python_program\lib\site-packages\httprunner\api.py", line 63, in test
 43     self.fail(str(ex))
 44 AssertionError:
 45 validate: content.status equals 1(str)  ==> fail
 46 1(int) equals 1(str)
 47 
 48 ----------------------------------------------------------------------
 49 Ran 1 test in 0.172s
 50 
 51 FAILED (failures=1)
 52 INFO     Start to run testcase: 这是第一个测试用例
 53 金融项目注册接口
 54 INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
 55 INFO     status_code: 200, response_time(ms): 35.92 ms, response_length: 60 bytes
 56 
 57 ERROR   
 58 validate: content.status equals 1(str)  ==> fail
 59 1(int) equals 1(str)
 60 ERROR    ******************************** DETAILED REQUEST & RESPONSE ********************************
 61 ====== request details ======
 62 url: http://192.168.200.144:8080/futureloan/mvc/api/member/register
 63 method: GET
 64 headers: {}
 65 params: {'mobilephone': '18956455875', 'pwd': 'xiaocao', 'regname': 'xiaocao'}
 66 verify: True
 67 
 68 ====== response details ======
 69 status_code: 200
 70 headers: {'Server': 'Apache-Coyote/1.1', 'Content-Type': 'application/json;charset=UTF-8', 'Transfer-Encoding': 'chunked
 71 ', 'Date': 'Tue, 17 Dec 2019 12:53:56 GMT'}
 72 body: '{"status":1,"code":"10001","data":null,"msg":"注册成功"}'
 73 
 74 F
 75 
 76 ======================================================================
 77 FAIL: test_0000_000 (httprunner.api.TestSequense)
 78 金融项目注册接口
 79 ----------------------------------------------------------------------
 80 Traceback (most recent call last):
 81   File "d:\programinstall\python_program\lib\site-packages\httprunner\api.py", line 61, in test
 82     test_runner.run_test(test_dict)
 83 httprunner.exceptions.ValidationFailure:
 84 validate: content.status equals 1(str)  ==> fail
 85 1(int) equals 1(str)
 86 
 87 During handling of the above exception, another exception occurred:
 88 
 89 Traceback (most recent call last):
 90   File "d:\programinstall\python_program\lib\site-packages\httprunner\api.py", line 63, in test
 91     self.fail(str(ex))
 92 AssertionError:
 93 validate: content.status equals 1(str)  ==> fail
 94 1(int) equals 1(str)
 95 
 96 ----------------------------------------------------------------------
 97 Ran 1 test in 0.065s
 98 
 99 FAILED (failures=1)
100 INFO     Start to render Html report ...
101 INFO     Generated Html report: D:\Python_Script\Interface_Test\reports\1576587235.html
运行日志信息

5.独立参数 & 自定义函数

  1 INFO     HttpRunner version: 2.4.2
  2 INFO     Loading environment variables from D:\Python_Script\Interface_Test\.env
  3 INFO     Start to run testcase: 获取手机号码归属地接口
  4 获取去手机号码归属地
  5 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
  6 INFO     status_code: 200, response_time(ms): 112.7 ms, response_length: 108 bytes
  7 
  8 .
  9 
 10 ----------------------------------------------------------------------
 11 Ran 1 test in 0.116s
 12 
 13 OK
 14 INFO     Start to run testcase: 获取手机号码归属地接口
 15 获取去手机号码归属地
 16 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 17 INFO     status_code: 200, response_time(ms): 71.94 ms, response_length: 108 bytes
 18 
 19 .
 20 
 21 ----------------------------------------------------------------------
 22 Ran 1 test in 0.077s
 23 
 24 OK
 25 INFO     Start to run testcase: 获取手机号码归属地接口
 26 获取去手机号码归属地
 27 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 28 INFO     status_code: 200, response_time(ms): 78.79 ms, response_length: 108 bytes
 29 
 30 .
 31 
 32 ----------------------------------------------------------------------
 33 Ran 1 test in 0.083s
 34 
 35 OK
 36 INFO     Start to run testcase: 获取手机号码归属地接口
 37 获取去手机号码归属地
 38 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 39 INFO     status_code: 200, response_time(ms): 77.79 ms, response_length: 108 bytes
 40 
 41 .
 42 
 43 ----------------------------------------------------------------------
 44 Ran 1 test in 0.082s
 45 
 46 OK
 47 INFO     Start to run testcase: 获取手机号码归属地接口
 48 获取去手机号码归属地
 49 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 50 INFO     status_code: 200, response_time(ms): 88.77 ms, response_length: 108 bytes
 51 
 52 .
 53 
 54 ----------------------------------------------------------------------
 55 Ran 1 test in 0.093s
 56 
 57 OK
 58 INFO     Start to run testcase: 获取手机号码归属地接口
 59 获取去手机号码归属地
 60 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 61 INFO     status_code: 200, response_time(ms): 69.81 ms, response_length: 108 bytes
 62 
 63 .
 64 
 65 ----------------------------------------------------------------------
 66 Ran 1 test in 0.074s
 67 
 68 OK
 69 INFO     Start to run testcase: 获取手机号码归属地接口
 70 获取去手机号码归属地
 71 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 72 INFO     status_code: 200, response_time(ms): 84.78 ms, response_length: 108 bytes
 73 
 74 .
 75 
 76 ----------------------------------------------------------------------
 77 Ran 1 test in 0.090s
 78 
 79 OK
 80 INFO     Start to run testcase: 获取手机号码归属地接口
 81 获取去手机号码归属地
 82 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 83 INFO     status_code: 200, response_time(ms): 88.76 ms, response_length: 108 bytes
 84 
 85 .
 86 
 87 ----------------------------------------------------------------------
 88 Ran 1 test in 0.093s
 89 
 90 OK
 91 INFO     Start to run testcase: 获取手机号码归属地接口
 92 获取去手机号码归属地
 93 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
 94 INFO     status_code: 200, response_time(ms): 103.93 ms, response_length: 108 bytes
 95 
 96 .
 97 
 98 ----------------------------------------------------------------------
 99 Ran 1 test in 0.109s
100 
101 OK
102 INFO     Start to run testcase: 获取手机号码归属地接口
103 获取去手机号码归属地
104 INFO     GET http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?
105 INFO     status_code: 200, response_time(ms): 73.89 ms, response_length: 108 bytes
106 
107 .
108 
109 ----------------------------------------------------------------------
110 Ran 1 test in 0.078s
111 
112 OK
113 INFO     Start to render Html report ...
114 INFO     Generated Html report: D:\Python_Script\Interface_Test\reports\1576635075.html
运行日志信息

6.关联参数 & 自定义函数

 

INFO     HttpRunner version: 2.4.2
INFO     Loading environment variables from D:\Python_Script\Interface_Test\.env
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 15.96 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.023s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 12.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.031s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 11.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.030s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 11.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.019s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 10.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.019s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 12.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.020s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 10.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.017s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 10.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.018s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 9.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.018s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 30.92 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.038s

OK
INFO     Start to run testcase: 这是第一个测试用例
金融项目注册接口
INFO     GET http://192.168.200.144:8080/futureloan/mvc/api/member/register
INFO     status_code: 200, response_time(ms): 11.97 ms, response_length: 60 bytes

.

----------------------------------------------------------------------
Ran 1 test in 0.021s

OK
INFO     Start to render Html report ...
INFO     Generated Html report: D:\Python_Script\Interface_Test\reports\1576635738.html
运行日志信息

独立参数和关联参数 在自定义函数中是一样的,需要注意的是,在自定义函数中,生成的参数列表必须为 list of dict 的数据结构,该设计主要是为了与 CSV 文件的处理机制保持一致。

7.如果想要实现多个参数的随机组合可以在parameters中指定多个参数,系统会按照笛卡尔积的概念执行行。

例如:

组合结果为:

 

8.注意:系统内置函数实现参数化读取的是CSV文件,而CSV文件是一个纯文本文件,如果在断言时遇到断言int类型则断言失败

posted @ 2019-12-17 20:57  不休不止  阅读(1175)  评论(0编辑  收藏  举报