当前位置:   article > 正文

自动生成测试用例_接口测试用例自动生成工具_接口测试用例生成

接口测试用例生成

前言

写用例之前,我们应该熟悉API的详细信息。建议使用抓包工具Charles或AnyProxy进行抓包。

har2case

我们先来了解一下另一个项目har2case 他的工作原理就是将当前主流的抓包工具和浏览器都支持将抓取得到的数据包导出为标准通用的 HAR 格式(HTTP Archive),然后 HttpRunner 将 HAR 格式的数据包转换为YAML/JSON格式的测试用例文件。 这里使用charles抓取百度首页进行演示

选中百度请求,右键点击Export Session,导出到/hrun_demo/har下,命名为baidu,格式选择HTTP Archive(.har),点击save

此时,har文件夹下会多出baidu.har文件

har2case生成用例

可以使用命令har2case将Har文件转成测试用例,先使用命令har2case -h查看帮助文档

 
  1. (httprunner_env) ➜ har har2case -h

  2. usage: har2case har2case [-h] [-2y] [-2j] [--filter FILTER]

  3. [--exclude EXCLUDE]

  4. [har_source_file]

  5. positional arguments:

  6. har_source_file Specify HAR source file

  7. optional arguments:

  8. -h, --help show this help message and exit

  9. -2y, --to-yml, --to-yaml

  10. Convert to YAML format, if not specified, convert to

  11. pytest format by default.

  12. -2j, --to-json Convert to JSON format, if not specified, convert to

  13. pytest format by default.

  14. --filter FILTER Specify filter keyword, only url include filter string

  15. will be converted.

  16. --exclude EXCLUDE Specify exclude keyword, url that includes exclude

  17. string will be ignored, multiple keywords can be

  18. joined with '|'

  • -2y:转换为yaml格式,如果没有指定,默认转换为pytest格式
  • -2j:转换为json格式,如果没有指定,默认转换为pytest格式
  • –filter:指定筛选关键字,仅url包含筛选字符串将被转换。
  • –exclude:指定exclude关键字,包括exclude的url字符串将被忽略,可以选择多个关键字以“|”联接

生成pytest格式的用例

HttpRunner 3.0.7版本开始,har2case将HAR文件默认转换成pytest,强烈建议pytest格式而不是以前的YAML / JSON格式编写和维护测试用例。 这里也是博主从pytest框架转换为httprunner框架的原因之一 运行命令将har文件转换成测试用例:

 
  1. (httprunner_env) ➜ har har2case baidu.har

  2. 2021-02-05 11:27:32.945 | INFO | httprunner.ext.har2case.core:gen_testcase:356 - Start to generate testcase from /Users/jkc/hrun/hrun_demo/har/baidu.har

  3. 2021-02-05 11:27:32.945 | INFO | httprunner.ext.har2case.core:_make_testcase:347 - Extract info from HAR file and prepare for testcase.

  4. 2021-02-05 11:27:32.948 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2

  5. 2021-02-05 11:27:32.948 | INFO | httprunner.loader:load_dot_env_file:127 - Loading environment variables from /Users/jkc/hrun/hrun_demo/.env

  6. 2021-02-05 11:27:32.949 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: USERNAME

  7. 2021-02-05 11:27:32.949 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: PASSWORD

  8. 2021-02-05 11:27:32.950 | INFO | httprunner.make:make_testcase:349 - start to make testcase: /Users/jkc/hrun/hrun_demo/har/baidu.har

  9. 2021-02-05 11:27:32.951 | INFO | httprunner.make:make_testcase:442 - generated testcase: /Users/jkc/hrun/hrun_demo/har/baidu_test.py

  10. 2021-02-05 11:27:32.952 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...

  11. reformatted /Users/songyingkai/hrun/hrun_demo/har/baidu_test.py

  12. All done! ✨

    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/658974
推荐阅读
相关标签