httprunner(4)录制生成测试用例
前言
写用例之前,我们应该熟悉API的详细信息。建议使用抓包工具Charles或AnyProxy进行抓包。
har2case
我们先来了解一下另一个项目har2case
他的工作原理就是将当前主流的抓包工具和浏览器都支持将抓取得到的数据包导出为标准通用的 HAR 格式(HTTP Archive),然后 HttpRunner 将 HAR 格式的数据包转换为YAML/JSON格式的测试用例文件。
这里使用charles抓取百度首页进行演示
选中百度请求,右键点击Export Session,导出到/hrun_demo/har下,命名为baidu,格式选择HTTP Archive(.har),点击save


此时,har文件夹下会多出baidu.har文件
har2case生成用例
可以使用命令har2case将Har文件转成测试用例,先使用命令har2case -h查看帮助文档
(httprunner_env) ➜ har har2case -h
usage: har2case har2case [-h] [-2y] [-2j] [--filter FILTER]
[--exclude EXCLUDE]
[har_source_file]
positional arguments:
har_source_file Specify HAR source file
optional arguments:
-h, --help show this help message and exit
-2y, --to-yml, --to-yaml
Convert to YAML format, if not specified, convert to
pytest format by default.
-2j, --to-json Convert to JSON format, if not specified, convert to
pytest format by default.
--filter FILTER Specify filter keyword, only url include filter string
will be converted.
--exclude EXCLUDE Specify exclude keyword, url that includes exclude
string will be ignored, multiple keywords can be
joined with '|'
- -2y:转换为
yaml格式,如果没有指定,默认转换为pytest格式 - -2j:转换为
json格式,如果没有指定,默认转换为pytest格式 - --filter:指定筛选关键字,仅url包含筛选字符串将被转换。
- --exclude:指定exclude关键字,包括exclude的url字符串将被忽略,可以选择多个关键字以“|”联接
生成pytest格式的用例
HttpRunner 3.0.7版本开始,har2case将HAR文件默认转换成pytest,强烈建议以pytest格式而不是以前的YAML / JSON格式编写和维护测试用例。
这里也是博主从pytest框架转换为httprunner框架的原因之一
运行命令将har文件转换成测试用例:
(httprunner_env) ➜ har har2case baidu.har
2021-02-05 11:27:32.945 | INFO | httprunner.ext.har2case.core:gen_testcase:356 - Start to generate testcase from /Users/songyingkai/hrun/hrun_demo/har/baidu.har
2021-02-05 11:27:32.945 | INFO | httprunner.ext.har2case.core:_make_testcase:347 - Extract info from HAR file and prepare for testcase.
2021-02-05 11:27:32.948 | INFO | httprunner.compat:ensure_testcase_v3:219 - ensure compatibility with testcase format v2
2021-02-05 11:27:32.948 | INFO | httprunner.loader:load_dot_env_file:127 - Loading environment variables from /Users/songyingkai/hrun/hrun_demo/.env
2021-02-05 11:27:32.949 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: USERNAME
2021-02-05 11:27:32.949 | DEBUG | httprunner.utils:set_os_environ:33 - Set OS environment variable: PASSWORD
2021-02-05 11:27:32.950 | INFO | httprunner.make:make_testcase:349 - start to make testcase: /Users/songyingkai/hrun/hrun_demo/har/baidu.har
2021-02-05 11:27:32.951 | INFO | httprunner.make:make_testcase:442 - generated testcase: /Users/songyingkai/hrun/hrun_demo/har/baidu_test.py
2021-02-05 11:27:32.952 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...
reformatted /Users/songyingkai/hrun/hrun_demo/har/baidu_test.py
All done!
1 file reformatted.
2021-02-05 11:27:33.181 | INFO | httprunner.ext.har2case.core:gen_testcase:377 - generated testcase: /Users/songyingkai/hrun/hrun_demo/har/baidu_test.py
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit
生成的pytest文件是标准的python文件,如下所示:
# NOTE: Generated By HttpRunner v3.1.4
# FROM: har/baidu.har
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseBaidu(HttpRunner):
config = Config("testcase description").verify(False)
teststeps = [
Step(
RunRequest("/")
.get("https://www.baidu.com/")
.with_headers(
**{
"Host": "www.baidu.com",
"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"sec-ch-ua": '"Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Sec-Fetch-Site": "none",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Cookie": "PSTM=1610459846; BAIDUID=A40AD6AD806FBBED1033903732FFA453:FG=1; BD_UPN=123253; BIDUPSID=898B917A8EF92E036B0F06DC792638A1; __yjs_duid=1_c391922164c20246fd49e4ad821d46c41610613507042; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=33423_33513_33403_33273_33594_33585_26350_33568; delPer=0; BD_CK_SAM=1; PSINO=5; BAIDUID_BFESS=A40AD6AD806FBBED1033903732FFA453:FG=1; COOKIE_SESSION=86630_1_9_6_41_5_0_0_8_2_1_0_0_0_71_0_1612340974_1611912344_1612427533%7C9%23262013_235_1611912339%7C9; BD_HOME=1; ZD_ENTRY=baidu; BA_HECTOR=20ag2180aga18k21mm1g1pegu0r",
}
)
.with_cookies(
**{
"PSTM": "1610459846",
"BAIDUID": "A40AD6AD806FBBED1033903732FFA453:FG=1",
"BD_UPN": "123253",
"BIDUPSID": "898B917A8EF92E036B0F06DC792638A1",
"__yjs_duid": "1_c391922164c20246fd49e4ad821d46c41610613507042",
"BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
"H_PS_PSSID": "33423_33513_33403_33273_33594_33585_26350_33568",
"delPer": "0",
"BD_CK_SAM": "1",
"PSINO": "5",
"BAIDUID_BFESS": "A40AD6AD806FBBED1033903732FFA453:FG=1",
"COOKIE_SESSION": "86630_1_9_6_41_5_0_0_8_2_1_0_0_0_71_0_1612340974_1611912344_1612427533%7C9%23262013_235_1611912339%7C9",
"BD_HOME": "1",
"ZD_ENTRY": "baidu",
"BA_HECTOR": "20ag2180aga18k21mm1g1pegu0r",
}
)
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "text/html;charset=utf-8")
),
]
if __name__ == "__main__":
TestCaseBaidu().test_start()
可以使用命令hrun或pytest运行,实际上,hrun是pytest的封装,效果基本相同。
(httprunner_env) ➜ har hrun baidu_test.py
2021-02-05 14:03:39.279 | INFO | httprunner.make:__make:512 - make path: /Users/songyingkai/hrun/hrun_demo/har/baidu_test.py
2021-02-05 14:03:39.279 | INFO | httprunner.make:format_pytest_with_black:170 - format pytest cases with black ...
No Path provided. Nothing to do
httprunner(4)录制生成测试用例的更多相关文章
- 【HttpRunner v3.x】笔记 ——3. 录制生成测试用例
在正式手动编写case之前,我们可以先来熟悉下httprunner的录制生成用例功能. 用postman的童鞋都知道,里面有个功能可以将接口转换成代码,可以直接copy过来使用,提升case编写效率. ...
- 2. HttpRunnner录制生成用例
录制生成用例 为了简化测试用例的编写工作,HttpRunner 实现了测试用例生成的功能,对应的转换工具为一个独立的项目:har2case. 简单来说,就是当前主流的抓包工具和浏览器都支持将抓取得到的 ...
- 【Golang】 可以自动生成测试用例的库--gotests
简介 gotests是一个Golang命令行工具,它可以使编写Go的测试代码变得容易.它能基于目标源文件的函数和方法生成数据驱动测试用例,并且在此过程会自动导入任何依赖. 下面是gotests在使用S ...
- httprunner学习2-har2case录制生成脚本
前言 复制毁一生,录制穷三代,如果你只是因为不想写脚本,而去录制脚本,那我建议你还是别学录制了. 录制脚本,只是一个过渡,从0到1的一个过渡,如果让你直接写脚本,你会无从下手,可以将录制的脚本快速转化 ...
- 安装并使用PICT,生成测试用例
一.PICT简介 PICT工具是在微软公司内部使用的一款承兑组合的命令行生成工具,现在已经对外提供,可以在 http://download.microsoft.com/download/f/5/5/f ...
- 1.httprunner3入门
一.httprunner3 httprunner是一款面向HTTP(S)协议的通用开源测试框架 支持三种格式的用例:YAML/JSON/Pytest,httprunner3以前不支持pytest用例 ...
- 【HttpRunner v3.x】笔记 ——4. 测试用例-结构解析
一.官方首推pytest格式 上篇文章我们知道了,httprunner可以支持三种格式的用例,分别是pytest.yaml和json.yaml和json是以前的版本所使用的用例格式,但是在3.x版本上 ...
- 基于HttpRunner,解析swagger数据,快速生成接口测试框架
使用HttpRunner默认生成的项目是这样的 命令:httprunner --startproject 项目名称 so,根据这个项目的目录结构,使用python解析swagger接口参数,可以快速 ...
- 测试用例生成工具ALLPAIRS(转)
ALLPAIRS是一个测试用例设计工具,用于Windows,但移植到了多种平台,以适应该脚本文件的一些小改动.它自动对所有实验技术进行设计,通过这个工具的方法可以在海量的数据组合中选择少量的数据生成测 ...
随机推荐
- Spark的dataframe转rdd通用工具类
需求解决问题 当每次读取hive表或者其他数据源,获取数据,相对其进行rdd操作,遇到任何类都需要df.rdd(row>row.getstring(0))去获取,就很麻烦,所以可以实现个通用的转 ...
- 如何快速搭建hadoop集群
安装好虚拟机,重命名为master 配置网卡 命令:vi /etc/sysconfig/network-scripts/ifcfg-en(按tab键) 这里要配置ip,网关,域名解析 例如我的 IPA ...
- 并发编程--锁--Lock和Synchronized
说说对于 synchronized 关键字的了解? synchronized关键字解决的是多个线程之间访问资源的同步性: synchronized 关键字可以保证被它修饰的方法或者代码块在任意时刻只能 ...
- CentOS | python3.7安装指南
前言: centos系统本身默认安装有python2.x,版本x根据不同版本系统有所不同 可通过 python --V 或 python --version 查看系统自带的python版本 有一些系统 ...
- SpringBoot 导入插件报错 Cannot resolve plugin org.springframework.boot:spring-boot-maven-plugin:2.4.1
使用 maven 导入插件的时候报错: Cannot resolve plugin org.springframework.boot:spring-boot-maven-plugin:2.4.1 我的 ...
- 如何利用Intellij Idea搭建python编译运行环境 (转)
首先进入Intellij Idea的官方网站:点击打开链接 点击download,选择旗舰版进行下载.网上的破解教程很多,也可以注册一个学生账号拿到一年的免费试用权. 安装过程不再细说,第一次打开选择 ...
- Centos搭建Git服务端
首先需要安装git,可以使用yum源在线安装 yum install -y git 创建一个git用户,用来运行管理git服务 adduser git 初始化git仓库(这里我们选择/home/git ...
- [Cerc2005]Knights of the Round Table
题目描述 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少有3个骑士参加,且相互憎恨的骑士不能坐在圆桌的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是大于1的奇数,以防 ...
- 接收的参数为日期类型、controller控制层进行数据保存、进行重定向跳转
目录 1.接收的参数为日期类型 2.controller控制层进行数据保存 3.controller层如何进行重定向跳转(因为默认是请求转发) 4.静态资源的映射 1.接收的参数为日期类型 WEB-I ...
- 使用Logback日志
使用Logback日志 spring boot内部使用Logback作为日志实现的框架. Logback和log4j非常相似,如果你对log4j很熟悉,那对logback很快就会得心应手. logba ...