Tavern是一款轻量级的测试框架,集合Pytest的测试框架,可以把测试的描述信息(API的请求信息)以及测试断言都可以编写在Yaml的文件中,然后结合Pytest的测试框架直接解析Yaml就可以来批量的执行。在Tavern的测试框架中,它追求的是“Easier API testing”的设计理念,不过从目前实践的应用来看,它是符合这样的一种简单的模式的,Easy to Write, Easy to Read and Understand。下面我们来安装它,须先安装python环境。
pip3 install tavern==1.12.2
这个yaml文件的命名 需要遵守一些规范
test_name: 测试登陆
stages:
- name: 测试登陆
requests:
url: http://xx.xxx.xx.xxx:85/ses-platform/login
method: POST
data:
username: hanyue
password: 123456
response:
status_code: 200
json :
success: true
message: 用户登录成功
---
test_name: 获取token方法登录
stages:
- name: 获取token方法登录
request:
url: http://xx.xxx.x.xx/jidian/admin/techApi/auth
method: GET
data:
headers:
$ext:
function: test_get_token:generate_token
response:
status_code: 500
test_name: 校验用户名为空
stages:
- name: 校验用户名为空
request:
url: http://xx.xxx.x.xxx/jidian/admin/techApi/auth
method: POST
data:
password: xxxjd@123
response:
status_code: 500
---
test_name: 校验密码为空
stages:
- name: 校验密码为空
request:
url: http://xx.xxx.x.xxx/jidian/admin/techApi/auth
method: POST
data:
username: root
response:
status_code: 500
在yaml文件中使用关键字的介绍
test_name : 这个是给当前测试用例起一个名字
stages: 在这个关键字内的内容就是请求内容
name: 这个还是说明一下这个接口的作用
request: 请求
url: 请求的url
method: 请求的方法(大写)
data: 请求参数
response: 响应断言
python -m pytest -v -s test_login.tavern.yaml
执行结果:
安装一个插件: pip install pytest-html
安装好以后 DOS命令输入:
pytest -v test_login.tavern.yaml --html=login.html
? =号后面的参数是给测试报告的html文件起的名字
生成后 打开报告 看效果
也可以用另一个插件:allure-pytest
因为该测试框架它是基于Pytest测试框架改造的,所以测试报告可以结合Allure来进行整合报告
步骤:
1、安装allure-pytest
pip3 install allure-pytest
2、下载安装 allure-2.14.0,配置Allure的环境变量,将bin目录放在path内;
3、执行命令生成测试报告,【注:这里命令与上种生成测试报告命令不同】如:
python3 -m pytest -v test_login.tavern.yaml --alluredir=index
【备注】:会在当前的目录下生成index的文件夹,文件夹下显示的是JSON的文件内容
4、
allure generate index -o index/html --clean
【备注】:会在当前的目录下生成基于HTML的测试报告
5、
allure open -h 127.0.0.1 -p 8088 ./index/html
【备注】:基于HTML的测试报告会以服务的显示自动启动展示到默认的浏览器
打开 http://127.0.0.1:8088
,之前写的 case 报告就会呈现在你面前
pytest-
原文:https://www.cnblogs.com/51record/p/15160845.html