從 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類型則斷言失敗