环境搭建

环境准备

环境准备:python3 环境(安装Python,安装pycharm)

httprunner安装:

$ pip3 install httprunner
$ pip3 install git+https://github.com/httprunner/httprunner.git@master

httprunner 更新至最新版本

$ pip3 install -U httprunner
$ pip3 install -U git+https://github.com/httprunner/httprunner.git@master

查看httprunner 版本

$ httprunner -V # hrun -V

查看httprunner 帮助文档

$ httprunner -h

常用httprunner 命令
$ httprunner -h
usage: httprunner [-h] [-V] {run,startproject,har2case,make} ...One-stop solution for HTTP(S) testing.positional arguments:{run,startproject,har2case,make}sub-command helprun                 Make HttpRunner testcases and run with pytest.startproject        Create a new project with template structure.har2case            Convert HAR(HTTP Archive) to YAML/JSON testcases forHttpRunner.make                Convert YAML/JSON testcases to pytest cases.optional arguments:-h, --help            show this help message and exit-V, --version         show version

脚本概念

testcase:

config:name: xxxvariables:              # config variablesvarA: "configA"varB: "configB"varC: "configC"parameters:             # parameter variablesvarA: ["paramA1"]varB: ["paramB1"]teststeps:
-name: step 1variables:              # step variablesvarA: "step1A"request:url: /$varA/$varB/$varC # varA="step1A", varB="paramB1", varC="configC"method: GETextract:                # extracted variablesvarA: body.data.A   # suppose varA="extractVarA"varB: body.data.B   # suppose varB="extractVarB"
-name: step 2varialbes:varA: "step2A"request:url: /$varA/$varB/$varC # varA="step2A", varB="extractVarB", varC="configC"method: GET

变量优先级:
step variables > extracted variables, e.g. step 2, varA=“step2A”
parameter variables > config variables, e.g. step 1, varB=“paramB1”
extracted variables > parameter variables > config variables, e.g. step 2, varB=“extractVarB”
config variables are in the lowest priority, e.g. step 1/2, varC=“configC”

testsuite:

config:name: xxxvariables:                  # testsuite config variablesvarA: "configA"varB: "configB"varC: "configC"testcases:
-name: case 1variables:                  # testcase variablesvarA: "case1A"testcase: /path/to/testcase1export: ["varA", "varB"]    # export variables
-name: case 2varialbes:                  # testcase variablesvarA: "case2A"testcase: /path/to/testcase2

变量优先级:
testcase variables > export variables > testsuite config variables > referenced testcase config variables

脚手架(新建新项目,录制脚本,执行脚本)

脚手架:新建项目

创建新项目:使用下面命令构造目录结构
$ httprunner startproject ProjectName
$httprunner startproject Demo
查看项目结构:
$ tree directory
$ tree Demo

执行脚本

$ hrun demo
执行过程

录制脚本:charle抓包生成har文件

cahrles抓包生成测试用例
1、charles抓包生成har文件
2、har2case 将har文件转换成pytest(默认) 、yaml(-2y)或者json(-2j) 文件

$ har2case -h
usage: har2case har2case [-h] [-2y] [-2j] [--filter FILTER][--exclude EXCLUDE][har_source_file]positional arguments:har_source_file       Specify HAR source fileoptional arguments:-h, --help            show this help message and exit-2y, --to-yml, --to-yamlConvert to YAML format, if not specified, convert topytest format by default.-2j, --to-json        Convert to JSON format, if not specified, convert topytest format by default.--filter FILTER       Specify filter keyword, only url include filter stringwill be converted.--exclude EXCLUDE     Specify exclude keyword, url that includes excludestring will be ignored, multiple keywords can bejoined with '|'

写测试用例

写测试用例

测试支持场景:

testcase 示例:

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCaseclass TestCaseRequestWithFunctions(HttpRunner):config = (Config("request methods testcase with functions").variables(**{"foo1": "config_bar1","foo2": "config_bar2","expect_foo1": "config_bar1","expect_foo2": "config_bar2",}).base_url("https://postman-echo.com").verify(False).export(*["foo3"]))teststeps = [Step(RunRequest("get with params").with_variables(**{"foo1": "bar11", "foo2": "bar21", "sum_v": "${sum_two(1, 2)}"}).get("/get").with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"}).with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"}).extract().with_jmespath("body.args.foo2", "foo3").validate().assert_equal("status_code", 200).assert_equal("body.args.foo1", "bar11").assert_equal("body.args.sum_v", "3").assert_equal("body.args.foo2", "bar21")),Step(RunRequest("post form data").with_variables(**{"foo2": "bar23"}).post("/post").with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}","Content-Type": "application/x-www-form-urlencoded",}).with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3").validate().assert_equal("status_code", 200).assert_equal("body.form.foo1", "$expect_foo1").assert_equal("body.form.foo2", "bar23").assert_equal("body.form.foo3", "bar21")),]if __name__ == "__main__":TestCaseRequestWithFunctions().test_start()

testcase结构

1、每个基于pytest的testcase继承了httprunner类
2、每个testcase都有config和teststeps 两个部分

config

3、config: 内容下有:name(必填),base_url, verify, variables, export.

name:测试用例名称,仅该变量为config必填参数,用于在日志和报告中输出
base_url:共同的URL部分,base_url可输入为uat,stg头部的域名,后续接口只需要使用相对路径
verify:验证服务器的TLS证书,保证数据是安全加密进行传输,默认为true; >>variables:request的请求变量
export:response结果提取的变量,作为下一接口的请求参数

teststeps

teststeps :每个testcase应该有一个或多个有序的测试步骤(List[Step]),每个步骤对应于一个API请求或另一个testcase引用调用。
每个step下可能包含:variables/extract/validate/hooks

RunRequest(name):指定每个请求API的名字

variables:设置请求参数

.with_variables:接口请求的变量,独立使用,如需使用在复杂场景内,将变量定义在config内,step内变量的优先级高于config内
.method(url):接口请求URL,如定义base_url,只需输入相对路径部分
以下为请求参数的指定

.with_params
Specify query string for the request url. This is corresponding to the params argument of requests.request.

.with_headers
Specify HTTP headers for the request. This is corresponding to the headers argument of requests.request.

.with_cookies
Specify HTTP request cookies. This is corresponding to the cookies argument of requests.request.

.with_data
Specify HTTP request body. This is corresponding to the data argument of requests.request.

.with_json
Specify HTTP request body in json. This is corresponding to the json argument of requests.request.

extract 提取变量

使用jmespath进行提取变量:with_jmespath(jmes_path: Text, var_name: Text)
jmespath语法文档

validate 断言

使用jmespath提取JSON响应主体,并使用预期值进行断言
语法:assert_XXX(jmes_path: Text, expected_value: Any, message: Text = “”)

RunTestCase

RunTestCase在步骤中用于引用另一个testcase调用,call 传入调用的方法,export导出外部引用的变量供后续使用
Step(
RunTestCase(“request with functions”)
.with_variables(
**{“foo1”: “testcase_ref_bar1”, “expect_foo1”: “testcase_ref_bar1”}
)
.call(RequestWithFunctions)
.export(*[“foo3”])
),

执行脚本

1、执行命令

hrun # httprunner run 2个命令等价皆可使用

2、 执行脚本方式

单个脚本执行

$ hrun path/to/testcase1

多个脚本执行

$ hrun path/to/testcase1 path/to/testcase2

整个文件夹执行

$ hrun path/to/testcase_folder/

3、 执行文件格式

执行文件格式是YAML/JSON格式

1、用例先转换成pytest,再进行执行 hrun = make + pytest

例如: hrun /path/to/example.yml => hun /path/to/example_test.py

2、如果testcase文件夹名或文件名包含点、连字符或空格等符号,则这些符号将替换为下划线,以避免在python类导入(testcase引用)中出现语法错误。另外,文件夹/文件名以digit开头将添加前缀T,因为python模块和类名不能以digit开头

例如:path 1/a.b-2/3.yml => path_1/a_b_2/T3_test.py

执行文件格式是pytest格式

如果您的测试用例是以pytest格式编写的,或者您希望运行从YAML/JSON测试用例转换的pytest文件,那么hrun和pytest命令都可以。您需要记住的是,hrun只包装pytest,因此pytest的所有参数都可以与hrun一起使用。

hrun # pytest 两个执行命令是等价关系
hrun -h 查看所有执行命令

4、日志信息

默认在日志信息中是不输出request和response 参数的,如需输出加入参数“-s”,参见如下示例

$ hrun -s examples/postman_echo/request_methods/request_with_functions.yml
每个测试用例都将生成一个执行日志文件,存放位置在 /logs/TestCaseID.run.log

参考文档:

httprunner使用手册: https://docs.httprunner.org/user/concepts/
httprunner开发手写笔记:https://debugtalk.com/tags/HttpRunner/

httprunner学习手册相关推荐

  1. 《ABAQUS 6.14超级学习手册》——1.2 ABAQUS分析模块

    本节书摘来自异步社区<ABAQUS 6.14超级学习手册>一书中的第1章,第1.2节,作者: 齐威 更多章节内容可以访问云栖社区"异步社区"公众号查看. 1.2 ABA ...

  2. 《ANSYS 14.0超级学习手册》一第2章 高级应用的基石——APDL

    本节书摘来自异步社区<ANSYS 14.0超级学习手册>一书中的第2章,作者 张建伟 , 白海波 , 李昕, 更多章节内容可以访问云栖社区"异步社区"公众号查看 第2章 ...

  3. 《Rhino3D 4.0产品造型设计学习手册》——导读

    本节书摘来自异步社区<Rhino3D 4.0产品造型设计学习手册>一书中的目录,作者[韩]崔成权,更多章节内容可以访问云栖社区"异步社区"公众号查看 目 录 Part. ...

  4. 转-Redis学习手册(目录)

    为什么自己当初要选择Redis作为数据存储解决方案中的一员呢?现在能想到的原因主要有三.其一,Redis不仅性能高效,而且完全免费.其二,是基于C/C++开发的服务器,这里应该有一定的感情因素吧.最后 ...

  5. 《ANSYS 14.0超级学习手册》一第1章 绪 论

    本节书摘来自异步社区<ANSYS 14.0超级学习手册>一书中的第1章,第1.1节,作者 张建伟 , 白海波 , 李昕, 更多章节内容可以访问云栖社区"异步社区"公众号 ...

  6. 网络安全体系 应用学习手册 下载

    网络安全体系应用学习手册系我从赛迪网上看到的一篇好文章,由于篇幅过长,特制作成CHM文件.此为本人第一次试做CHM文件,希望大家能够喜欢. 全手册共包括两大章,分别介绍了"网络应用的信息安全 ...

  7. php mysql 学习,php+mysql完全学习手册源码

    [实例简介] <php+mysql完全学习手册>(黄桂金.于永军)源码 [实例截图] [核心代码] b82c182d-8fbd-4888-854f-5dc80db47e47 └── php ...

  8. 一站式SpringBoot for NoSQL Study Tutorial 开发教程学习手册

    SpringBoot2.0 + NoSQL使用教程,项目名称:"SpringBoot2NoSQL" 项目地址: https://gitee.com/475660/SpringBoo ...

  9. 安卓开发重磅炸弹!程序员福利!《高级Kotlin强化实战学习手册(附Demo)》开放下载!

    前言 自Google宣布将 Kotlin 作为 Android 开发的首选语言 (Kotlin-first),现已有60% 的专业 Android 开发者已经采用了该编程语言.在 Google Pla ...

最新文章

  1. 比特币脚本及交易分析 - 智能合约雏形
  2. 车载以太网之权威指南_awk权威指南之 终结篇!
  3. 2020 五大技术趋势:无人驾驶发展、机器视觉崛起、区块链实用化、人类增强技术、超自动化...
  4. Java Pinyin4j(汉字转拼音)
  5. iOS 访问权限设置
  6. 【BLE MIDI】开发 BLE MIDI 硬件电子乐器设备需要遵循的相关规范 ( 资料收集 )
  7. Java正则判断中文字符串句型_NLP中文句子类型判别和分类实现
  8. 大数据的下一站是什么?服务/分析一体化(HSAP)
  9. 【STM32】GPIO模拟I2C程序示例
  10. SAP JAM tag Cloud
  11. python中的pyinstaller库_Python(00):PyInstaller库,打包成exe基本介绍
  12. SpringSecurity入门到入土教程_1
  13. 五种 必须了解的CSS选择器
  14. 一个简单标注库的插件化开发实践
  15. 陈向京:个人养老金投资的配置和策略
  16. R语言使用rcauchy函数生成符合柯西分布的随机数、使用plot函数可视化符合柯西分布的随机数(Cauchy distribution)
  17. ROS ,how to subscriber hark_msgs----hark-ros
  18. 详情页用虚拟机还是云服务器,详情页用虚拟机还是云服务器
  19. OOP-面向对象程序设计
  20. Opencv学习(3)——基础矩阵F、本质矩阵E、单应矩阵H 函数解析

热门文章

  1. Java中Int、Integer、new Integer()之间的区别
  2. python使用spark sql查询impala_使用SparkSQL阅读Impala表
  3. 第3章第2节:使用HStack在水平方向排列视图 [SwiftUI快速入门到实战]
  4. Java开发工具idea的安装与破解
  5. uncanny valley(恐怖谷)--学习笔记
  6. php 数组索引重新排序,PHP索引数组排序方法整理
  7. Win98病毒制作原理-完整版
  8. 如何正确干净的卸载nodejs?
  9. 学习笔记之DataGrid
  10. Android 为什么不能在子线程中直接更新UI