【HttpRunner v3.x】笔记 ——3. 录制生成测试用例

在正式手动编写case之前,我们可以先来熟悉下httprunner的录制生成用例功能。
用postman的童鞋都知道,里面有个功能可以将接口转换成代码,可以直接copy过来使用,提升case编写效率。
那httprunner的录制生成用例功能又是怎么回事呢?

一、har2case

其实,这都要依托于另一个独立的项目-har2case。
原理就是当前主流的抓包工具和浏览器都支持将抓取得到的数据包导出为标准通用的 HAR 格式(HTTP Archive),然后 HttpRunner 将 HAR 格式的数据包转换为YAML/JSON格式的测试用例文件。
比如,我现在用window系统上的fiddler去抓取一个百度首页的请求。

选中这个请求,点击左上角的File——Export Sessions——(可以选择导出选中的也可以导出所有),这里我们选择导出选中的,导出HTTPArchive,文件名baidu_home.har,保存到了项目的har目录下。

二、转换为pytest文件

运行命令将har文件转换成测试用例:

har2case baidu_home.har


生成完毕,在har目录下可以看到生成出的python文件:

# NOTE: Generated By HttpRunner v3.1.1
# FROM: har\baidu_home.har

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseBaiduHome(HttpRunner):
    config = Config("testcase description").verify(False)

    teststeps = [
        Step(
            RunRequest("/")
            .get("https://www.baidu.com/")
            .with_headers(
                **{
                    "Host": "www.baidu.com",
                    "Connection": "keep-alive",
                    "Upgrade-Insecure-Requests": "1",
                    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36",
                    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
                    "Cookie": "PSTM=1582766561; BAIDUID=5F919C7A22A02E55FBC58E932E7495CD:FG=1; BD_UPN=12314353; BIDUPSID=B2A8970CF5106170D98A137A26C533F7; H_WISE_SIDS=143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085; BDUSS=ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; BD_CK_SAM=1; PSINO=5; BD_HOME=1; __yjsv5_shitong=1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9; yjs_js_security_passport=db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js; H_PS_645EC=8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby; H_PS_PSSID=32095_1445_31672_21106_32139_31660_32045_31321; BDSVRTM=0",
                }
            )
            .with_cookies(
                **{
                    "PSTM": "1582766561",
                    "BAIDUID": "5F919C7A22A02E55FBC58E932E7495CD:FG=1",
                    "BD_UPN": "12314353",
                    "BIDUPSID": "B2A8970CF5106170D98A137A26C533F7",
                    "H_WISE_SIDS": "143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085",
                    "BDUSS": "ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ",
                    "BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
                    "BDRCVFR[feWj1Vr5u3D]": "I67x6TjHwwYf0",
                    "delPer": "0",
                    "BD_CK_SAM": "1",
                    "PSINO": "5",
                    "BD_HOME": "1",
                    "__yjsv5_shitong": "1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9",
                    "yjs_js_security_passport": "db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js",
                    "H_PS_645EC": "8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby",
                    "H_PS_PSSID": "32095_1445_31672_21106_32139_31660_32045_31321",
                    "BDSVRTM": "0",
                }
            )
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal('headers."Content-Type"', "text/html;charset=utf-8")
        ),
    ]


if __name__ == "__main__":
    TestCaseBaiduHome().test_start()

因为httprunner封装了pytest,所有既可以用hrun去运行,也可以用pytest去运行。

hrun

pytest

三、转换为YAML/JSON

很简单,只要在命令后面多加对应的参数就行了。-2y/--to-ymlor-2j/--to-json
转为YAML:

har2case baidu_home.har -2y


可以查看到转换生成的yaml文件了。

config:
    name: testcase description
    variables: {}
    verify: false
teststeps:
-   name: /
    request:
        cookies:
            BAIDUID: 5F919C7A22A02E55FBC58E932E7495CD:FG=1
            BDORZ: B490B5EBF6F3CD402E515D22BCDA1598
            BDRCVFR[feWj1Vr5u3D]: I67x6TjHwwYf0
            BDSVRTM: '0'
            BDUSS: ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ
            BD_CK_SAM: '1'
            BD_HOME: '1'
            BD_UPN: '12314353'
            BIDUPSID: B2A8970CF5106170D98A137A26C533F7
            H_PS_645EC: 8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby
            H_PS_PSSID: '32095_1445_31672_21106_32139_31660_32045_31321'
            H_WISE_SIDS: '143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085'
            PSINO: '5'
            PSTM: '1582766561'
            __yjsv5_shitong: 1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9
            delPer: '0'
            yjs_js_security_passport: db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js
        headers:
            Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
            Accept-Encoding: gzip, deflate, br
            Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
            Connection: keep-alive
            Cookie: PSTM=1582766561; BAIDUID=5F919C7A22A02E55FBC58E932E7495CD:FG=1;
                BD_UPN=12314353; BIDUPSID=B2A8970CF5106170D98A137A26C533F7; H_WISE_SIDS=143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085;
                BDUSS=ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ;
                BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0;
                delPer=0; BD_CK_SAM=1; PSINO=5; BD_HOME=1; __yjsv5_shitong=1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9;
                yjs_js_security_passport=db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js;
                H_PS_645EC=8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby;
                H_PS_PSSID=32095_1445_31672_21106_32139_31660_32045_31321; BDSVRTM=0
            Host: www.baidu.com
            Upgrade-Insecure-Requests: '1'
            User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,
                like Gecko) Chrome/75.0.3770.80 Safari/537.36
        method: GET
        url: https://www.baidu.com/
    validate:
    -   eq:
        - status_code
        - 200
    -   eq:
        - headers.Content-Type
        - text/html;charset=utf-8

转换为JSON:

har2case baidu_home.har -2j


可以看的对应的json文件:

{
    "config": {
        "name": "testcase description",
        "variables": {},
        "verify": false
    },
    "teststeps": [
        {
            "name": "/",
            "request": {
                "url": "https://www.baidu.com/",
                "method": "GET",
                "cookies": {
                    "PSTM": "1582766561",
                    "BAIDUID": "5F919C7A22A02E55FBC58E932E7495CD:FG=1",
                    "BD_UPN": "12314353",
                    "BIDUPSID": "B2A8970CF5106170D98A137A26C533F7",
                    "H_WISE_SIDS": "143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085",
                    "BDUSS": "ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ",
                    "BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
                    "BDRCVFR[feWj1Vr5u3D]": "I67x6TjHwwYf0",
                    "delPer": "0",
                    "BD_CK_SAM": "1",
                    "PSINO": "5",
                    "BD_HOME": "1",
                    "__yjsv5_shitong": "1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9",
                    "yjs_js_security_passport": "db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js",
                    "H_PS_645EC": "8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby",
                    "H_PS_PSSID": "32095_1445_31672_21106_32139_31660_32045_31321",
                    "BDSVRTM": "0"
                },
                "headers": {
                    "Host": "www.baidu.com",
                    "Connection": "keep-alive",
                    "Upgrade-Insecure-Requests": "1",
                    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36",
                    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
                    "Cookie": "PSTM=1582766561; BAIDUID=5F919C7A22A02E55FBC58E932E7495CD:FG=1; BD_UPN=12314353; BIDUPSID=B2A8970CF5106170D98A137A26C533F7; H_WISE_SIDS=143933_142621_143879_144883_139041_141744_145870_144419_144135_145271_136863_131247_144682_137745_138883_140259_141941_127969_144790_140593_143491_144376_131423_114552_142206_145910_144501_125695_107313_139909_145654_143477_144966_140367_145423_144535_145305_145399_143857_139914_110085; BDUSS=ldaMkhYZjEtLTJQbFhQUTJtU3pwTGhnRVJkeE9YUmNzU2tRRExoZDE3N2hvfmxlSVFBQUFBJCQAAAAAAAAAAAEAAABJufAYUTc2Nzc2MDMyNQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOEW0l7hFtJeQ; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; BD_CK_SAM=1; PSINO=5; BD_HOME=1; __yjsv5_shitong=1.0_7_47374759f3e4680e7c7afb4a907cb9d2e37d_300_1593325350568_112.80.30.194_b17302a9; yjs_js_security_passport=db59f16989e33eb07a57bd9928ad960ce36495c4_1593325351_js; H_PS_645EC=8043uGVfepN6KMlbDns%2FO0qiazHvEYcE62vhF1luCcNa%2FgwzKQEQq2epcrqdNRvj4iby; H_PS_PSSID=32095_1445_31672_21106_32139_31660_32045_31321; BDSVRTM=0"
                }
            },
            "validate": [
                {
                    "eq": [
                        "status_code",
                        200
                    ]
                },
                {
                    "eq": [
                        "headers.Content-Type",
                        "text/html;charset=utf-8"
                    ]
                }
            ]
        }
    ]
}

以上转换出的pytest、yaml、json这3种格式的文件效果都是一样的,用hrun都可以运行,但是用pytest执行的话只可以运行.py的文件了。

posted @ 2020-06-28 17:21  把苹果咬哭的测试笔记  阅读(1955)  评论(0编辑  收藏  举报