python httprunner3.X 框架 学习记录

因为本人主要学的python 使用jmeter做接口测试时 再对复杂的用例进行复杂的逻辑判断时过于吃力
因此调研合适的接口测试框架后 做一个httprunner的学习记录

HttpRunner是一个简单优雅但功能强大的 HTTP(S) 测试框架。

安装方式

pip install httprunner

安装校验

在 HttpRunner 安装成功后,系统中会新增如下 5 个命令:
httprunner: 核心命令

  • ate: 曾经用过的命令(当时框架名称为 ApiTestEngine),功能与 httprunner 完全相同 hrun:
  • httprunner 的缩写,功能与 httprunner 完全相同
  • locusts: 基于 Locust 实现性能测试
  • har2case: 辅助工具,可将标准通用的 HAR 格式(HTTP Archive)转换为YAML/JSON格式的测试用例

脚手架

创建新项目
您需要指定的唯一参数是项目名称。

httprunner startproject demo

运行脚手架项目
脚手架项目有几个有效的测试用例,因此您无需任何编辑即可运行测试。

hrun demo

生成测试用例(pytest)

由于 HttpRunner 3.0.7,har2case默认情况下会将 HAR 文件转换为 pytest,并且非常建议以 pytest 格式而不是以前的YAML/JSON格式编写和维护测试用例。
使用har2case会生成pytest用例

har2case har/postman-echo-post-form.har

编写测试用例

HttpRunner3.X官网 编写用例时用到的参数
为了保证用例的独立性,在一个用例中也是可以对其他用例进行调用的,RunTestCase就是在当前用例中对其他测试用例进行调用也是Step的子参数之一,作用是在用例步骤中引用其他的用例。
看例子:登录后获取token等参数,传入下一个用例,因为登录用例很常用,所以将登录用例的用例单独写成一个用例文件

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


class TestCaseLogin(HttpRunner):

    config = Config("登陆接口")\
            .verify(False)\
            .export(*["token"])\ #登录用例成功后将必要参数提取并在全局的Config中导出export(*["token"]),方便下一个用例做关联。
            .base_url('${ENV(base_url)}')\ #获取env配置文件base_url
            .variables(
            **{
                "mobile": "admin",
                "password": "21232f297a57a5a743894a0e4a801fc3",
            }
            )
    teststeps = [
        Step(
            RunRequest("/erp/user/login/passwordLogin")
            .post("/erp/user/login/passwordLogin")
            .with_headers(
                **{
                    "Content-Type": "application/json;charset=UTF-8",
                }
            )

            .with_json(
                {"mobile": "$mobile", "password": "$password"} #使用全局参数
            )
            .extract()
            .with_jmespath("body.data.token","token")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal('headers."Content-Type"', "application/json")
            .assert_equal("body.code", 200)
            .assert_equal("body.msg", "操作成功")
        )
    ]


if __name__ == "__main__":
    TestCaseLogin().test_start()
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

from demo.testcases.login_test import TestCaseLogin


class TestCaseSystem(HttpRunner):
    config = Config("testcase description") \
        .verify(False) \
        .base_url('${ENV(base_url)}') \
        .variables(
        **{
            "mobile": "admin",
            "password": "21232f297a57a5a743894a0e4a801fc3",
        }
    )

    teststeps = [
        Step(
            RunTestCase("登陆后获取token") #步骤名称会显示在测试日志里面
            .call(TestCaseLogin) #call()即是调用的意思,传参指定你要引用的testcase类名称
            .export(*["token"]) #指定要导出的变量,以供后续Step引用。这边的导出和被调用用例的导出只定义一处即可,如果两边都定义了,依然符合合并覆盖原则
        ),
        Step(
            RunRequest("/erp/user/setting/getUserMenu")
                .get("/erp/user/setting/getUserMenu")
                .with_headers(
                **{

                    "Accept": "application/json, text/plain, */*",
                    "Authorization": "$token",
                }
            )
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal('headers."Content-Type"', "application/json")
                .assert_equal("body.code", 200)
                .assert_equal("body.msg", "操作成功")
        )
    ]



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

如何获取env文件配置的信息

def get_env(name):
    from environs import Env
    env = Env()
    env.read_env(".env")
    return env.str(name)

生成测试用例报告

HTML 测试报告

HTTPrunner 安装之后自带 pytest-html插件,当你想生成 HTML测试报告时,可以添加命令参数–html

 hrun demo --html=test.html
 #--html=test.html`中的test.html是测试报告的存放路径,没有带文件夹的时候会存放在命令运行的当前文件夹,此处是项目根目录

allure report

pytest 支持大名鼎鼎的 allure 测试报告,HTTPrunner 集成了pytest,也天然支持allure。

不过 HTTPrunner 默认并未安装 allure,你需要另外安装。

安装有两种方式:

安装allure的 pytest 依赖库allure-pytest;
安装 HTTPrunner的allure 依赖库 httprunner[allure]。

安装 httprunnerallure

pip3 install "httprunner[allure]"

一旦allure-pytest 准备好,以下参数就可以与 hrun/pytest命令一起使用:

–alluredir=DIR: 生成 allure 报告到指定目录
-clean-alluredir: 如果指定目录已存在则清理该文件夹
–allure-no-capture:不要将 pytest 捕获的日志记录(logging)、标准输出(stdout)、标准错误(stderr)附加到报告中
要使 Allure 侦听器能够在测试执行期间收集结果,只需添加–alluredir选项,并提供指向存储结果的文件夹路径。如:

hrun demo --alluredir=/tmp/my_allure_results

/tmp/my_allure_results 只会存储收集的测试结果并非完成的报告,还需要通过命令生成。

要在测试完成后查看实际报告,您需要使用Allure命令行实用程序从结果中生成报告。

allure serve /tmp/my_allure_results
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值