当前位置: 首页 > backend >正文

allure测试报告的应用

1.电脑必须安装JDK1.8,下载地址:

https://www.oracle.com/java/technologies/downloads/#java8-windows;我这里下载的是

jdk-8u341的版本

2.安装JDK1.8,并配置环境变量

3.下载Allure的包,官网下载地址为:

https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/

我这里下载的是2.18.1的版本

4.将Allure的包放到电脑上的非中文名称的目录,解压,并配置环境变量

 

5.检查allure是否配置成功,打开dos窗口,输入:allure --version

显示的版本号为2.18.1表示你配置好了

6.由于我们需要使用pytest集成allure,所以需要下载安装allure-pytest;安装命令:pip install

allure-pytest;检查安装是否成功的命令:pip show allure-pytest

7.注意Pycharm中的需要配置的执行框架为Unittest,否则不会生成测试报告

 2.使用allure生产测试报告

import os,pytest
def step_01():
print('点击登录链接')
print('输入用户名密码,点击登录')
def step_02():
print('输入商品名称')
print('点击搜索按钮')
class TestShopping:
def test_01(self):
step_01()
step_02()
print('测试用例:test_01')
def test_02(self):
print('测试用例:test_02')
if __name__ == '__main__':
# __file__:指定运行的测试文件为当前文件
# --alluredir=report/tmp:指定测试结果数据的存储路径为:当前的report目录下的tmp目录
# --clean-alluredir:以免旧数据被加载了,清空测试结果目录
pytest.main([__file__,'--alluredir=report/tmp','--clean-alluredir'])
#利用os.system执行命令行命令:allure generate
# allure generate 测试结果目录 -o 测试报告存放目录 --clean
# --clean:清空测试报告目录,让新的报告工程可以继续使用以前的报告目录
os.system('allure generate report/tmp -o report/html --clean')
#直接生成测试报告,快速生成,与上面的二选一
#os.system('allure serve report/html')

测试报告结果如图:

 

3、Allure的特性

1.@allure.epic():epic描述,敏捷测试里面的概念,定义为史诗,一般用于描述测试的一级模块名称,修

饰测试文件里面的的类,下面未feature

2.@allure.feature():模块描述,用于定义测试用例的所属模块二级模块,通常修饰测试用例的类,下面

未story

3.@allure.story():定义故事,用于描述测试的业务场景,修饰测试用例方法,下面是title

4.@allure.title():定义测试用例名称,修饰测试用例方法,下面是step

5.@allure.testcase(url,'测试用例管理工具地址'):添加连接,显示测试用例管理工具的地址,便于出

问题核对

6.@allure.issue(url,'bug提单管理工具地址'):添加连接,显示bug提单工具地址,便于出问题提单

7.@allure.severity():定义测试用例的等级,修饰测试用例方法,一共5个等级:

blocker:阻塞,对应门槛用例,重要业务主流程用例

critical:严重,对应次要业务主流程用例,重要业务的次要场景用例

normal:一般,对应次要业务的次要场景用例,单业务场景正常场景用例

minor:轻微,对应单业务次要场景用例

trivial:提示,对应其它级别较低的用例

8.@allure.step():定义测试步骤,可单独修饰测试操作步骤的函数,也可以直接在测试用例使用:with

allure.step(): 这样去描述测试步骤

9.@allure.description():定义用例的描述信息,修饰测试用例的方法,一般不建议使用,关于测试用

例的描述,可以在测试用例的方法里面使用文档字符串的说明方式进行说明

10.allure.attach.file():添加附件到测试报告

初级最佳实践如下:

import pytest,allure,os
@allure.epic('EcShop项目测试WebUI自动化测试')
@allure.feature('登录模块')
class TestLogin:
@allure.story('登录成功')
@allure.title('正确用户名,正确密码,登录成功')
@allure.issue('https://www.baidu.com','点击进入提单入口')
@allure.testcase('https://www.jd.com','点击进入用例管理工具入口')
@allure.severity('blocker')
def test_login(self):
'''
使用文档字符串,可以起到@allure.description()在作用
前置处理:打开浏览器
输入用户名
输入密码
断言
后置处理:退出关闭浏览器
'''
with allure.step('输入用户名'):
print('username')
with allure.step('输入密码'):
print('password')
with allure.step('点击登录按钮'):
print('点击login')
with allure.step('断言'):
print('断言是否登录成功')
if __name__ == '__main__':
pytest.main(['--alluredir=report/tmp','--clean-alluredir'])
os.system('allure generate ./report/tmp -o ./report/html --clean')

 4、特性高级应用

1.在测试用例方法中可以使用:allure.dynamic.XXXX 动态生成相关的内容,结合

@pytest.mark.parametrize()实现动态的参数化显示, 具体实践如下:

from pathlib import Path
import pytest,allure,os
from public.get_data import get_yaml_data
@allure.epic('一级模块:登录/注册/登出')
@allure.feature('二级模块:登录')
class TestLogin:
case_data_path = Path(__file__).parent.parent / 'data/login.yaml'
@pytest.mark.parametrize('kwargs', get_yaml_data(case_data_path))
@allure.issue('https://www.baidu.com','点击进入提单入口')
@allure.testcase('https://www.jd.com','点击进入用例管理工具入口')
def test_login(self,brower,kwargs):
'''
1.打开浏览器,访问登录界面
2.输入用户名
3.输入密码
4.点击登录
5.断言
'''
allure.dynamic.story(kwargs['story'])
allure.dynamic.title(kwargs['title'])
allure.dynamic.severity(kwargs['severity'])
driver = brower
with allure.step('进入登录页面:http://47.113.104.130:9966/user.php'):
driver.get(r'http://47.113.104.130:9966/user.php')
with allure.step('输入用户名:{}'.format(kwargs['username'])):
driver.find_element('name','username').send_keys(kwargs['username'])
with allure.step('输入密码:{}'.format(kwargs['password'])):
driver.find_element('name','password').send_keys(kwargs['password'])
with allure.step('点击登录按钮'):
driver.find_element('name','submit').click()
with allure.step('获取断言的实际值'):
actual_value = driver.find_element(*kwargs['assert_el']).text
with allure.step('断言'):
assert kwargs['expect_value'] in actual_value
if __name__ == '__main__':
pytest.main([__file__,'--alluredir=../report/tmp','--clean-alluredir'])
os.system('allure generate ../report/tmp -o ../report/html --clean')

2.allure测试报告默认没有环境进行,我们可以通过配置文 件添加相关信息到allure报告,具体操作:

a.在config目录中添加配置文件:evnironment.properties

b.文件格式为k=v格式,内容可配置为:

systemVersion=Win11

pythonVersion=3.8.10

allureVersion=2.18.1

baseUrl=http://47.113.104.130:9966

projectName=VSS

author=rebort

emial=rebort.chen@howentech.com

c.在运行测试用例后,打开报告前,将配置文件copy到测试结果的收集目录:result/tmp

 

3、错误报告截图处理

具体的处理方案如下

在测试用例中使用异常处理方式,将测试步骤包起来,使用try...expect...raise处理
import pytest,allure,os
from config.config import base_path
from public.get_data import get_yaml_data
from public.get_png_path import get_png_name
@allure.epic('注册/登录/登出')
@allure.feature('登录')
class TestLogin:
case_data_path = base_path / 'data/login.yaml'
@pytest.mark.parametrize('kwargs', get_yaml_data(case_data_path))
@allure.issue('https://www.baidu.com','点击进入提单入口')
@allure.testcase('https://www.jd.com','点击进入用例管理工具入口')
def test_login(self,brower,kwargs):
'''
1.打开浏览器,访问登录界面
2.输入用户名
3.输入密码
4.点击登录
5.断言
'''
allure.dynamic.story(kwargs['story'])
allure.dynamic.title(kwargs['title'])
allure.dynamic.severity(kwargs['severity'])
driver = brower
try:
with allure.step('进入登录页
面:http://47.113.104.130:9966/user.php'):
driver.get(r'http://47.113.104.130:9966/user.php')
with allure.step('输入用户名:{}'.format(kwargs['username'])):
driver.find_element('name','username').send_keys(kwargs['username'])
with allure.step('输入密码:{}'.format(kwargs['password'])):
driver.find_element('name','password').send_keys(kwargs['password'])
with allure.step('点击登录按钮'):
driver.find_element('name','submit').click()
with allure.step('获取断言的实际值'):
actual_value = driver.find_element(*kwargs['assert_el']).text
with allure.step('断言'):
assert kwargs['expect_value'] in actual_value
except Exception as msg: #收集异常
png_name = get_png_name() #调用public中的公共方法准备截图的图片名称
driver.save_screenshot(png_name)#截图
allure.attach.file(png_name,attachment_type=allure.attachment_type.PNG)#添加截图
附件到测试报告
raise msg #抛出异常,不然断言就错了
if __name__ == '__main__':
pytest.main(['--alluredir=../result/tmp','--clean-alluredir'])
os.system('allure generate ../result/tmp -o ../report --clean')

 

http://www.xdnf.cn/news/3507.html

相关文章:

  • 「Mac畅玩AIGC与多模态11」开发篇07 - 使用自定义名言插件开发智能体应用
  • L3-040 人生就像一场旅行
  • stm32基础001(串口)
  • Astral Ascent 星界战士(星座上升) [DLC 解锁] [Steam] [Windows SteamOS macOS]
  • Linux架构篇、第1章_02源码编译安装Apache HTTP Server 最新稳定版本是 2.4.62
  • Windows11 管理员用户下无权限操作的解决方法
  • 基于BERT类的MRPC语义相似度检测(从0到-1系列)
  • 一键解放双手,操作丝滑起飞!
  • 21.1Linux中的LCD驱动实验(知识)_csdn
  • Flowable7.x学习笔记(十六)分页查询我的待办
  • [SystemVerilog] Arrays
  • JGQ516Ⅱ数据采集湿法袋式除尘器实验装置
  • DRV8301 三相电机驱动芯片的硬件参数与应用设计
  • 【AI论文】ReasonIR:为推理任务训练检索器
  • HarmonyOS应用开发中实现本地化存储的几种方式
  • 接口幂等性保证:技术方案与实践指南
  • Three.js + React 实战系列-3D 个人主页:构建 About 组件 (响应式 + 互动 + 动效)✨
  • 【Shell 脚本编程】详细指南:第四章 - 循环结构(for、while、until) 深度解析
  • Java 基础--数组(Array):存储数据的“排排坐”
  • 青蛙Todo:高效管理日程,提升工作学习效率
  • L39.【LeetCode题解】面试题 01.07. 旋转矩阵(四种方法)
  • 鸿蒙开发:如何解决软键盘弹出后的间距
  • [免费]SpringBoot+Vue非物质文化网站系统【论文+源码+SQL脚本】
  • 2025五一杯数学建模竞赛B题 矿山数据处理 保姆级教程讲解|模型讲解
  • Spring AI开发跃迁指南(第二章:急速上手3——Advisor核心原理、源码讲解及使用实例)
  • 如何使用网站备份到u盘,网站数据备份到U盘的方法
  • Python 函数装饰器和闭包(装饰器基础知识)
  • 二叉搜索树中的搜索(递归解决)
  • 【Shell 脚本编程】详细指南:第一章 - 基础入门与最佳实践
  • 软件工程国考