httprunner中引用的是yaml/json写测试用例,但是有些时候一个一个地写比较繁琐,为了减少繁琐事务,应用.har文件转成yaml/json文件运行测试用例。

项目工程目录:

httprunner生成报告(.har文件处理运行)(1)

har中的文件可以直接从Charles或者Fiddler中抓取后导出放到har文件夹中:

httprunner生成报告(.har文件处理运行)(2)

httprunner生成报告(.har文件处理运行)(3)

har文件夹中的.har文件我们要转成yaml/json文件,具体命令我们可以使用帮助命令查看下 har2case -h:

F:\TESTING\apiWebStudent\demo>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 '|'

F:\TESTING\apiWebStudent\demo\har>har2case studentLogin.har -2y 2021-03-23 18:26:24.084 | INFO | httprunner.ext.har2case.core:gen_testcase:356 - Start to generate testcase from F:\TESTING\apiWebStudent\demo\har\studentLogin.har 2021-03-23 18:26:24.085 | INFO | httprunner.ext.har2case.core:_make_testcase:347 - Extract info from HAR file and prepare for testcase. 2021-03-23 18:26:24.168 | INFO | httprunner.ext.har2case.utils:dump_yaml:108 - dump testcase to YAML format. 2021-03-23 18:26:24.301 | INFO | httprunner.ext.har2case.utils:dump_yaml:115 - Generate YAML testcase successfully: F:\TESTING\apiWebStudent\demo\har\studentLogin.yml 2021-03-23 18:26:24.303 | INFO | httprunner.ext.har2case.core:gen_testcase:377 - generated testcase: F:\TESTING\apiWebStudent\demo\har\studentLogin.yml Sentry is attempting to send 0 pending error messages Waiting up to 2 seconds Press Ctrl-Break to quit

F:\TESTING\apiWebStudent\demo\har>har2case studentLogin.har -2j 2021-03-23 18:27:09.102 | INFO | httprunner.ext.har2case.core:gen_testcase:356 - Start to generate testcase from F:\TESTING\apiWebStudent\demo\har\studentLogin.har 2021-03-23 18:27:09.104 | INFO | httprunner.ext.har2case.core:_make_testcase:347 - Extract info from HAR file and prepare for testcase. 2021-03-23 18:27:09.192 | INFO | httprunner.ext.har2case.utils:dump_json:121 - dump testcase to JSON format. 2021-03-23 18:27:09.198 | INFO | httprunner.ext.har2case.utils:dump_json:130 - Generate JSON testcase successfully: F:\TESTING\apiWebStudent\demo\har\studentLogin.json 2021-03-23 18:27:09.199 | INFO | httprunner.ext.har2case.core:gen_testcase:377 - generated testcase: F:\TESTING\apiWebStudent\demo\har\studentLogin.json Sentry is attempting to send 0 pending error messages Waiting up to 2 seconds Press Ctrl-Break to quit

httprunner生成报告(.har文件处理运行)(4)

把har文件夹中转成的json/yaml文件移动到testcases文件夹中,然后运行 hrun xxxx 运行:

F:\TESTING\apiWebStudent\demo>hrun testcases 2021-03-23 18:43:24.012 | INFO | httprunner.make:__make:512 - make path: F:\TESTING\apiWebStudent\demo\testcases 2021-03-23 18:43:24.019 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-03-23 18:43:24.025 | INFO | httprunner.loader:load_dot_env_file:127 - Loading environment variables from F:\TESTING\apiWebStudent\demo\.env 2021-03-23 18:43:24.026 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: USERNAME 2021-03-23 18:43:24.028 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: PASSWORD 2021-03-23 18:43:24.030 | INFO | httprunner.make:make_testcase:349 - start to make testcase: F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_login.yml 2021-03-23 18:43:24.041 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-03-23 18:43:24.044 | INFO | httprunner.make:make_testcase:349 - start to make testcase: F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_request.yml 2021-03-23 18:43:24.046 | INFO | httprunner.make:make_testcase:442 - generated testcase: F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_request_test.py 2021-03-23 18:43:24.047 | INFO | httprunner.make:make_testcase:442 - generated testcase: F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_login_test.py 2021-03-23 18:43:24.053 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-03-23 18:43:24.055 | INFO | httprunner.make:make_testcase:349 - start to make testcase: F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_ref.yml 2021-03-23 18:43:24.069 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-03-23 18:43:24.071 | INFO | httprunner.make:make_testcase:349 - start to make testcase: F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_request.yml 2021-03-23 18:43:24.072 | INFO | httprunner.make:make_testcase:442 - generated testcase: F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_ref_test.py 2021-03-23 18:43:24.084 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2 2021-03-23 18:43:24.086 | INFO | httprunner.make:make_testcase:349 - start to make testcase: F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_request.yml 2021-03-23 18:43:24.088 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ... reformatted F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_login_test.py reformatted F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_ref_test.py reformatted F:\TESTING\apiWebStudent\demo\testcases\demo_testcase_request_test.py All done! ✨ � ✨ 3 files reformatted. 2021-03-23 18:43:24.733 | INFO | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.1.4 ================================================================================= test session starts ================================================================================= platform win32 -- Python 3.7.7, pytest-5.4.2, py-1.9.0, pluggy-0.13.1 rootdir: F:\TESTING\apiWebStudent\demo plugins: allure-pytest-2.8.24, cov-2.8.1, emoji-0.2.0, forked-1.3.0, html-2.1.1, metadata-1.11.0, rerunfailures-9.1.1, xdist-2.1.0 collected 3 items testcases\demo_testcase_ref_test.py . [ 33%] testcases\demo_testcase_request_test.py . [ 66%] testcases\demo_testcase_login_test.py . [100%] ================================================================================== 3 passed in 6.30s ================================================================================== 2021-03-23 18:43:25.1412021-03-23 18:43:25.1452021-03-23 18:43:25.1462021-03-23 18:43:27.1592021-03-23 18:43:27.1602021-03-23 18:43:27.1652021-03-23 18:43:27.1672021-03-23 18:43:27.168 2021-03-23 18:43:27.1702021-03-23 18:43:27.1712021-03-23 18:43:27.1722021-03-23 18:43:27.1732021-03-23 18:43:27.1732021-03-23 18:43:27.1742021-03-23 18:43:27.1752021-03-23 18:43:27.391 2021-03-23 18:43:27.3922021-03-23 18:43:27.3922021-03-23 18:43:27.3932021-03-23 18:43:27.3942021-03-23 18:43:27.3952021-03-23 18:43:27.3962021-03-23 18:43:27.3962021-03-23 18:43:27.397 2021-03-23 18:43:27.6232021-03-23 18:43:27.6242021-03-23 18:43:27.6252021-03-23 18:43:27.6262021-03-23 18:43:27.6272021-03-23 18:43:27.6272021-03-23 18:43:27.6282021-03-23 18:43:27.629 2021-03-23 18:43:27.6292021-03-23 18:43:27.6302021-03-23 18:43:27.6312021-03-23 18:43:27.6312021-03-23 18:43:27.6322021-03-23 18:43:27.8592021-03-23 18:43:27.8602021-03-23 18:43:27.861 2021-03-23 18:43:27.8632021-03-23 18:43:27.8642021-03-23 18:43:27.8662021-03-23 18:43:27.8672021-03-23 18:43:27.8682021-03-23 18:43:27.8702021-03-23 18:43:27.8712021-03-23 18:43:27.885 2021-03-23 18:43:27.8872021-03-23 18:43:28.8342021-03-23 18:43:28.8352021-03-23 18:43:28.8372021-03-23 18:43:28.8402021-03-23 18:43:28.8412021-03-23 18:43:28.8432021-03-23 18:43:28.844 2021-03-23 18:43:28.8452021-03-23 18:43:28.8472021-03-23 18:43:28.8482021-03-23 18:43:28.8492021-03-23 18:43:28.8502021-03-23 18:43:29.0822021-03-23 18:43:29.0832021-03-23 18:43:29.084 2021-03-23 18:43:29.0852021-03-23 18:43:29.0862021-03-23 18:43:29.0862021-03-23 18:43:29.0872021-03-23 18:43:29.0872021-03-23 18:43:29.0882021-03-23 18:43:29.5952021-03-23 18:43:29.596 2021-03-23 18:43:29.5972021-03-23 18:43:29.5982021-03-23 18:43:29.5992021-03-23 18:43:29.5992021-03-23 18:43:29.6002021-03-23 18:43:29.6012021-03-23 18:43:29.6022021-03-23 18:43:29.603 2021-03-23 18:43:29.6052021-03-23 18:43:29.6182021-03-23 18:43:29.6202021-03-23 18:43:29.6232021-03-23 18:43:30.5272021-03-23 18:43:30.5282021-03-23 18:43:30.5292021-03-23 18:43:30.530 2021-03-23 18:43:30.5312021-03-23 18:43:30.5322021-03-23 18:43:30.5322021-03-23 18:43:30.5332021-03-23 18:43:30.5342021-03-23 18:43:30.5352021-03-23 18:43:30.5352021-03-23 18:43:30.536 2021-03-23 18:43:30.7612021-03-23 18:43:30.7632021-03-23 18:43:30.7652021-03-23 18:43:30.7672021-03-23 18:43:30.7682021-03-23 18:43:30.7702021-03-23 18:43:30.7712021-03-23 18:43:30.772 2021-03-23 18:43:30.7732021-03-23 18:43:31.0012021-03-23 18:43:31.0022021-03-23 18:43:31.0042021-03-23 18:43:31.0062021-03-23 18:43:31.0072021-03-23 18:43:31.0092021-03-23 18:43:31.010 2021-03-23 18:43:31.0112021-03-23 18:43:31.0122021-03-23 18:43:31.0132021-03-23 18:43:31.0142021-03-23 18:43:31.0162021-03-23 18:43:31.0172021-03-23 18:43:31.2432021-03-23 18:43:31.244 2021-03-23 18:43:31.2452021-03-23 18:43:31.2462021-03-23 18:43:31.2472021-03-23 18:43:31.2472021-03-23 18:43:31.2482021-03-23 18:43:31.2492021-03-23 18:43:31.2492021-03-23 18:43:31.250

[捂脸][流泪][笑哭]注意,敲黑板,划重点:har转的json/yaml文件,直接运行是不会生成.py文件的,你还得去修改,修改成符合识别的ymal/json文件,不然你就会一直疑惑,咦,怎么不成功呢......

如果你不知道怎么改,testcases文件夹中有官网yaml文件的模板,可以进行参考:

httprunner生成报告(.har文件处理运行)(5)

以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,评论区留言会及时修正发布,谢谢!


未完,待续…

一直都在努力,希望您也是!

,