httprunner 4.x学习 - 1 .环境准备与快速开始

前言

HttpRunner v4.0 同时采用了 Golang/Python 两种编程语言,底层会有两套相对独立的执行引擎,兼具 Golang 的高性能和 pytest 的丰富生态。

Windows 环境准备

使用 pip 安装目前最新版4.3.0

pip install httprunner

安装完成后,就可以使用hrun 命令了

查看版本号

(venv) D:\demo\hrun4x>hrun -V
v4.3.0

通过pip 安装的httprunner 版本仅仅只能得到 hrun 命令。4.x版本新增了一个hrp 命令,需在github 上下载安装包https://github.com/httprunner/httprunner/releases

hrp 命令安装

在github 上下载安装包https://github.com/httprunner/httprunner/releases

目前最新版本v4.3.3, 找到适合自己电脑的安装包

下载完成后,解压到自己电脑任意目录, 如:D:\soft\hrp

将解压的目录,添加到系统环境变量即可

打开cmd ,输入 hrp 检测命令是否生效

C:\Users\dell>hrp

██╗  ██╗████████╗████████╗██████╗ ██████╗ ██╗   ██╗███╗   ██╗███╗   ██╗█ ██████╗██████╗
██║  ██║╚══██╔══╝╚══██╔══╝██╔══██╗██╔══██╗██║   ██║████╗  ██║████╗  ██║██╔════╝██╔══██╗
███████║   ██║      ██║   ██████╔╝██████╔╝██║   ██║██╔██╗ ██║██╔██╗ ██║█████╗  ██████╔╝
██╔══██║   ██║      ██║   ██╔═══╝ ██╔══██╗██║   ██║██║╚██╗██║██║╚██╗██║██╔══╝  ██╔══██╗
██║  ██║   ██║      ██║   ██║     ██║  ██║╚██████╔╝██║ ╚████║██║ ╚████║███████╗██║  ██║
╚═╝  ╚═╝   ╚═╝      ╚═╝   ╚═╝     ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═══╝╚═╝  ╚═══╝╚══════╝╚═╝  ╚═╝

HttpRunner is an open source API testing tool that supports HTTP(S)/HTTP2/WebSocket/RPC
network protocols, covering API testing, performance testing and digital experience
monitoring (DEM) test types. Enjoy! ✨ 🚀 ✨

License: Apache-2.0
Website: https://httprunner.com
Github: https://github.com/httprunner/httprunner
Copyright 2017 debugtalk

Usage:
  hrp [command]

Available Commands:
  adb          simple utils for android device management
  boom         run load test with boomer
  build        build plugin for testing
  completion   Generate the autocompletion script for the specified shell
  convert      convert multiple source format to HttpRunner JSON/YAML/gotest/pytest cases
  dns          DNS resolution for different source and record types
  help         Help about any command
  ios          simple utils for ios device management
  ping         run integrated ping command
  pytest       run API test with pytest
  run          run API test with go engine
  startproject create a scaffold project
  wiki         visit https://httprunner.com

Flags:
  -h, --help               help for hrp
      --log-json           set log to json format
  -l, --log-level string   set log level (default "INFO")
      --venv string        specify python3 venv path
  -v, --version            version for hrp

Use "hrp [command] --help" for more information about a command.

快速创建项目

执行 hrp startproject demo 命令,即可初始化指定名称的项目工程。

hrp startproject demo

执行完成后会得到如下命令结构

demo
├── .env
├── .gitignore
├── debugtalk.py
├── har
│   └── .keep
├── reports
│   └── .keep
└── testcases
    ├── demo_ref_testcase.yml
    ├── demo_requests.yml
    └── demo_with_funplugin.json

相关目录与文件说明

  • .env 是环境配置文件
  • .gitignore 传git仓库时忽略文件
  • debugtalk.py 辅助函数功能文件
  • har 录制的文件目录
  • eports 报告目录
  • testcases 测试用例目录

快速开始

写个简单的test_get.yml

# 上海悠悠 wx:283340479
# blog:https://www.cnblogs.com/yoyoketang/

config:
  name: get

teststeps:
-
  name: get
  request:
    method: GET
    url: http://httpbin.org/get
  validate:
    - eq: [status_code, 200]

如果你写的是 JSON/YAML(/pytes) 类型的测试用例文件,那么可以继续用 HttpRunner3.x 的 hrun命令执行

(venv) D:\demo\hrun4x>hrun xuexi/test_get.yml
2023-05-05 08:42:48.139 | INFO | make path: D:\demo\hrun4x\xuexi\test_get.yml
2023-05-05 08:42:48.155 | INFO | ensure compatibility with testcase format v2/v3
2023-05-05 08:42:48.155 | INFO | start to make testcase: D:\demo\hrun4x\xuexi\test_get.yml
2023-05-05 08:42:48.170 | INFO | generated testcase: D:\demo\hrun4x\xuexi\test_get_test.py
2023-05-05 08:42:48.170 | INFO | format pytest cases with black ...
reformatted D:\demo\hrun4x\xuexi\test_get_test.py

All done! ✨ 🍰 ✨
1 file reformatted.
2023-05-05 08:42:48.377 | INFO | start to run tests with pytest. HttpRunner version: v4.3.0

如你所想的一样,它延续了httprunner3.x 版本的功能,会在当前test_get.yml 所在目录生成一个 test_get_test.py文件,用pytest去执行用例

# NOTE: Generated By HttpRunner v4.3.0
# FROM: xuexi\test_get.yml
from httprunner import HttpRunner, Config, Step, RunRequest


class TestCaseTestGet(HttpRunner):

    config = Config("get")

    teststeps = [
        Step(
            RunRequest("get")
            .get("http://httpbin.org/get")
            .validate()
            .assert_equal("status_code", 200)
        ),
    ]


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

hrp 命令运行

httprunner4.x 版本新特性,可以通过 hrp run 命令即可执行指定的测试用例;如需生成 HTML 测试报告,可附带 --gen-html-report 参数。

hrp run xuexi --gen-html-report

执行完成后,你会发现熟悉的 httprunner2.x 版本的报告又回来了

总结

看到这里你会发现 httprunner4.x 合并了以前的httprunner3.x 和 2.x 的功能, 并引入了 hrp 命令

HttpRunner v4 = v2 + v3 + hrp + ...

HttpRunner 的测试用例脚本支持4种格式了:JSON、YAML、pytest、go test

posted @ 2023-05-05 08:56  上海-悠悠  阅读(1512)  评论(0编辑  收藏  举报