录制和生成测试用例

抓取http请求和响应

在我们编写测试用例之前,我们应该知道API的详细信息,我们可以使用类似与Fiddle,Charles这样的抓包工具来抓取http请求

下面,我将使用一个测试环境

我们可以选择需要的请求,导出成.har的格式

使用har2case生成测试用例

获取 HAR 文件后,可以使用内置命令将其转换为 httpRunner 测试用例

har2case帮助

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 formatif not specified, convert to
                        pytest format by default.
  -2j--to-json        Convert to JSON formatif 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 '|'

生成测试用例

在3.0中,由于httpRunner默认会将HAR文件转成pytest,建议使用pytest,而不是之前的yaml、json格式

har2case test.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!
1 file reformatted.
2020-06-15 15:08:01.469 | INFO     | httprunner.ext.har2case.core:gen_testcase:353 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py

生成的pytest文件是如下标准的pytest文件,它可以直接运行:

# NOTE: Generated By HttpRunner v3.1.3
# FROM: test.har
 
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseTest(HttpRunner):
    config = Config("testcase description").verify(False)
    teststeps = [
        Step(
            RunRequest("/YYDZPY/login")
            .post("http://192.168.3.70:8070/YYDZPY/login")
            .with_headers(
                **{
                    "Host""192.168.3.70:8070",
                    "Connection""keep-alive",
                    "Content-Length""53",
                    "Pragma""no-cache",
                    "Accept""application/json, text/plain, */*",
                    "Cache-Control""no-cache",
                    "X-Requested-With""XMLHttpRequest",
                    "User-Agent""Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 Edg/84.0.522.40",
                    "Content-Type""application/json",
                    "Origin""http://192.168.3.70:8070",
                    "Referer""http://192.168.3.70:8070/YYDZPY/index.jsp",
                    "Accept-Encoding""gzip, deflate",
                    "Accept-Language""zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                    "Cookie""JSESSIONID=0E40D65F4C701A93621D2BE023A309C1",
                }
            )
            .with_cookies(**{"JSESSIONID""0E40D65F4C701A93621D2BE023A309C1"})
            .with_json({"XTZH""U1lTVEVN""YHMM""B/RGSNco4fpCWqdYQ3qSNg=="})
            .validate()
            .assert_equal("status_code"200)
            .assert_equal('headers."Content-Type"'"application/json;charset=UTF-8")
            .assert_equal("body.msg""登录成功!")
            .assert_equal("body.errormsg""用户未设置所属部门,部分功能会受影响,请联系管理员设置")
        ),
    ]
 
 
if __name__ == "__main__":
    TestCaseTest().test_start()

它可以使用命令或本机命令运行

hrun pytest

测试用例的组织形式:

在httpRunner中,测试用例的组织形式一共有三种:分别是。json,yaml和pytest

如何生成这些json、yaml、pytest文件?

第一步:

抓包

第二步:

把请求包保存为har格式

第三步:

使用命令生成

可以使用 -2y 来将 har文件生成yaml格式的用例

可以使用 -2j 来将 har文件生成json格式的用例

例如:现有有一个test.har的文件

可以使用 har2case test.har -2y 命令 生成yaml格式的用例

har2case test.har -2y
$ har2case har/postman-echo-post-form.har -2j
2020-06-15 15:32:02.955 | INFO     | httprunner.ext.har2case.core:gen_testcase:332 - Start to generate testcase from har/postman-echo-post-form.har
2020-06-15 15:32:02.955 | INFO     | httprunner.ext.har2case.core:_make_testcase:323 - Extract info from HAR file and prepare for testcase.
2020-06-15 15:32:02.958 | INFO     | httprunner.ext.har2case.utils:dump_json:122 - dump testcase to JSON format.
2020-06-15 15:32:02.959 | INFO     | httprunner.ext.har2case.utils:dump_json:131 - Generate JSON testcase successfully: har/postman-echo-post-form.json
2020-06-15 15:32:02.959 | INFO     | httprunner.ext.har2case.core:gen_testcase:353 - generated testcase: har/postman-echo-post-form.json

可以使用 har2case test.har -2j 命令生成json格式的用例

har2case test.har -2j
$ hrun har/postman-echo-post-form.json
2020-06-15 15:37:15.621 | INFO     | httprunner.loader:load_dot_env_file:130 - Loading environment variables from /Users/debugtalk/Desktop/demo/.env
2020-06-15 15:37:15.622 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: USERNAME
2020-06-15 15:37:15.622 | DEBUG    | httprunner.utils:set_os_environ:32 - Set OS environment variable: PASSWORD
2020-06-15 15:37:15.623 | INFO     | httprunner.make:make_testcase:310 - start to make testcase: /Users/debugtalk/Desktop/demo/har/postman-echo-post-form.json
2020-06-15 15:37:15.625 | INFO     | httprunner.make:make_testcase:383 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
2020-06-15 15:37:15.625 | 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!
1 file reformatted, 1 file left unchanged.
2020-06-15 15:37:15.962 | INFO     | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.0.12
====================================================================== test session starts ======================================================================
platform darwin -- Python 3.7.5, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /Users/debugtalk/Desktop/demo
plugins: metadata-1.9.0, allure-pytest-2.8.16, html-2.1.1
collected 1 item                                                                                                                                               
 
har/postman_echo_post_form_test.py .                                                                                                                      [100%]
 
======================================================================= 1 passed in 2.03s =======================================================================

如果,你使用的3.0的版本,建议以新使用pytest格式

posted @ 2020-08-10 11:24  SuYunJing  阅读(558)  评论(0)    收藏  举报