学习httprunner记录03

Posted luke8919

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习httprunner记录03相关的知识,希望对你有一定的参考价值。

使用httprunner3.x版本

python版本是3.6

测试环境是zen dao

以下为脚本:

# NOTE: Generated By HttpRunner v3.1.0
# FROM: assign_me.har
import re
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase


class TestCaseAssignMe(HttpRunner):
    config = (
        Config("testcase description")
        .verify(False)
        .variables(**{})
        .base_url("http://192.168.75.175")
    )

    teststeps = [

        Step(
            RunRequest("login with username and password")
                .post("/zentao/user-login.html")
                .with_headers(
                **{
                    "Accept": "application/json, text/javascript, */*; q=0.01",
                    "X-Requested-With": "XMLHttpRequest",
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36",
                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                    "Referer": "http://192.168.75.175/zentao/user-login.html",
                    "Accept-Encoding": "gzip, deflate",
                    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,lb;q=0.7"

                }
            )
                .with_data(
                {
                    "account": "admin",
                    "password": "qazwsx123",
                    "referer": "/zentao/",
                    "keepLogin": "1",
                }
            ).validate()
                .assert_equal("status_code", 200)
                .assert_equal(‘headers."Content-Type"‘, "text/html; Language=UTF-8;charset=UTF-8")
                .assert_equal(‘body.result‘, ‘success‘)
        )
        ,

        Step(
            RunRequest("check bugs that are assigned to me")
            .get("/zentao/bug-browse-1-0-assigntome.html")
            .with_headers(
                **{
                    "Host": "192.168.75.175",
                    "Upgrade-Insecure-Requests": "1",
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 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;q=0.9",
                    "Referer": "http://192.168.75.175/zentao/bug-browse-1-0-unclosed.html",
                    "Accept-Encoding": "gzip, deflate",
                    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,lb;q=0.7",
                    "Pragma": "no-cache",
                    "Cache-Control": "no-cache",
                }
            ).validate()
            .assert_equal("status_code", 200)
            .assert_equal(
                ‘headers."Content-Type"‘, "text/html; Language=UTF-8;charset=UTF-8"
            )
            .assert_contains(‘body‘, "data-id=‘6‘".encode(‘utf-8‘))
        ),
    ]


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

1  采用charles录制,导出

 

2 使用har2case 将har文件转换为python脚本,如上;

 

3 修正脚本,主要是去除不需要的部分,修改断言部分,参数化url部分

与手写pytest的区别:

最主要的一点是:

httprunner的作者设定为接口调用返回的都是json格式的数据,所以httprunner的断言函数都是依据此假设封装的

这一点对我不适用

 

手写pytest脚本的话完全不受此限制,可以方便的结合re模块从html中提取信息做断言

但是httprunner录制脚本然后转换的做法,生成脚本的速度确实是非常快

 

 

 

 

 

 

 

 

 

 

 

 

  

以上是关于学习httprunner记录03的主要内容,如果未能解决你的问题,请参考以下文章

httprunner 4.x学习

03-Httprunner-JMESPath提取返回结果

httprunner 3.x学习2

httprunner 2.x学习16 - 调用HttpRunner类执行用例

httprunner4.x学习6

httprunner 3.X学习