1. 程式人生 > 實用技巧 >httprunner v3.x 官方教程中文翻譯03--Record&Generate testcase(記錄&生成測試用例)

httprunner v3.x 官方教程中文翻譯03--Record&Generate testcase(記錄&生成測試用例)

記錄&生成測試用例

捕獲HTTP請求和響應

在編寫測試用例之前,我們應該瞭解API的細節。使用像Charles proxy這樣的web除錯代理工具來捕獲HTTP流量是一個不錯的選擇。

例如,下面的圖片展示了post表單資料到postman-echo.com

)

將會話匯出到HAR檔案

然後我們可以選擇捕獲的請求和響應,並將會話匯出到HTTP歸檔(.har)檔案。

用har2case生成testcase

獲得HAR檔案後,可以使用內建命令har2case將其轉換為HttpRunner 測試用例。

help

$ har2case -h
usage: har2case har2case [-h] [-2y] [-2j] [--filter FILTER]
                         [--exclude EXCLUDE]
                         [har_source_file]

positional arguments:
  har_source_file       Specify HAR source file

optional arguments:
  -h, --help            show this help message and exit
  -2y, --to-yml, --to-yaml
                        Convert to YAML format, if not specified, convert to
                        pytest format by default.
  -2j, --to-json        Convert to JSON format, if not specified, convert to
                        pytest format by default.
  --filter FILTER       Specify filter keyword, only url include filter string
                        will be converted.
  --exclude EXCLUDE     Specify exclude keyword, url that includes exclude
                        string will be ignored, multiple keywords can be
                        joined with '|'

生成testcase (pytest)

由於HttpRunner 3.0.7, har2case在預設情況下會將HAR檔案轉換為pytest,因此非常建議用pytest格式編寫和維護測試用例,而不是以前的YAML/JSON格式。

$ har2case har/postman-echo-post-form.har
2020-06-15 15:08:01.187 | INFO     | httprunner.ext.har2case.core:gen_testcase:332 - Start to generate testcase from har/postman-echo-post-form.har
2020-06-15 15:08:01.187 | INFO     | httprunner.ext.har2case.core:_make_testcase:323 - Extract info from HAR file and prepare for testcase.
2020-06-15 15:08:01.191 | INFO     | httprunner.loader:load_dot_env_file:130 - Loading environment variables from /Users/debugtalk/Desktop/demo/.env
2020-06-15 15:08:01.191 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: USERNAME
2020-06-15 15:08:01.191 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: PASSWORD
2020-06-15 15:08:01.193 | INFO     | httprunner.make:make_testcase:310 - start to make testcase: /Users/debugtalk/Desktop/demo/har/postman-echo-post-form.har
2020-06-15 15:08:01.193 | INFO     | httprunner.make:make_testcase:383 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
2020-06-15 15:08:01.194 | INFO     | httprunner.make:format_pytest_with_black:147 - format pytest cases with black ...
reformatted /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
All done! ✨