将har(http存档)转换为httprunner的yaml/json测试用例。
har2case的Python项目详细描述
har2case
将har(http存档)转换为httprunner的yaml/json测试用例。
安装
har2case
在PyPI
上可用,可以通过pip安装。
$ pip install har2case
用法
安装har2case
时,shell中应该有一个har2case命令(如果不使用
您应该确保python脚本目录在您的路径上。
查看har2case
版本:
$ har2case -V
0.2.1
要查看可用选项,请运行:
$ har2case -h usage: main.py [-h] [-V] [--log-level LOG_LEVEL] [-2y] [--filter FILTER] [--exclude EXCLUDE] [har_source_file] Convert HAR to YAML/JSON testcases for HttpRunner. positional arguments: har_source_file Specify HAR source file optional arguments: -h, --help show this help message and exit -V, --version show version --log-level LOG_LEVEL Specify logging level, default is INFO. -2y, --to-yml, --to-yaml Convert to YAML format, if not specified, convert to JSON 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 '|'
示例
在大多数情况下,只能指定har源文件路径。默认情况下,har2case
将生成json格式的测试用例文件。
$ har2case tests/data/demo.har INFO:root:Start to generate testcase. INFO:root:dump testcase to JSON format. INFO:root:Generate JSON testcase successfully: tests/data/demo.json
如果要生成yaml格式的testcase文件,可以添加-2y
或--to-yml
参数。
$ har2case tests/data/demo.har -2y INFO:root:Start to generate testcase. INFO:root:dump testcase to YAML format. INFO:root:Generate YAML testcase successfully: tests/data/demo.yaml
生成的testcase文件与har源文件位于同一文件夹中,并且具有相同的名称。
filter
转换时可以进行一些筛选,只有包含筛选字符串的url才会被转换。
$ har2case tests/data/demo.har --filter httprunner.org
排除
您也可以在转换时设置exclude关键字,包含exclude字符串的url将被忽略。
$ har2case tests/data/demo.har --exclude debugtalk.com
生成的测试用例
生成的yaml测试用例demo.yml
如下所示:
-config:name:testcase descriptionvariables:{}-test:name:/api/v1/Account/Loginrequest:headers:Content-Type:application/jsonUser-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36(KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36json:Pwd:'123'UserName:test001VerCode:''method:POSTurl:https://httprunner.org/api/v1/Account/Loginvalidate:-eq:-status_code-200-eq:-headers.Content-Type-application/json; charset=utf-8-eq:-content.IsSuccess-true-eq:-content.Code-200-eq:-content.Message-null
生成的json测试用例demo.json
显示如下:
[{"config":{"name":"testcase description","variables":{}}},{"test":{"name":"/api/v1/Account/Login","request":{"url":"https://httprunner.org/api/v1/Account/Login","method":"POST","headers":{"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36","Content-Type":"application/json"},"json":{"UserName":"test001","Pwd":"123","VerCode":""}},"validate":[{"eq":["status_code",200]},{"eq":["headers.Content-Type","application/json; charset=utf-8"]},{"eq":["content.IsSuccess",true]},{"eq":["content.Code",200]},{"eq":["content.Message",null]}]}}]