【自动化基础】allure描述用例详细讲解及实战
前言
allure可以输出非常精美的测试报告,也可以和pytest进行完美结合,不仅可以渲染页面,还可以控制用例的执行。下面就对allure的使用进行一个详细的介绍和总结。
需要准备的环境:
- python
- pytest
- allure-pytest
- allure工具
一、allure用例描述
使用方法 | 参数值 | 参数说明 |
---|---|---|
@allure.epic() | epic描述 | 敏捷里面的概念,对用例或用例集进行描述分类 |
@allure.feature() | 模块名称 | 与epic类似,只是比epic级别低 |
@allure.story() | 用户故事 | 与epic类似,只是比feature级别低 |
@allure.title(用例的标题) | 用例的标题 | 重命名html报告的用例名称 |
@allure.testcase() | 测试用例的链接地址 | 与link类似 |
@allure.issue() | 缺陷 | 与link类似 |
@allure.description() | 用例描述 | 进行测试用例的描述 |
@allure.step() | 操作步骤 | 进行测试用例的步骤 |
@allure.severity() | 用例等级 | blocker,critical,normal,minor,trivial |
@allure.link() | 链接 | 定义一个链接,在测试报告展现(推荐使用) |
@allure.attachment() | 附件 | 报告添加附件 |
二、allure实战demo
# -*- coding:utf-8 -*- """ @File: allure_demo.py @Author: 三叔测试笔记 @Time : 2022/3/18 8:34 @Description: allure使用介绍 """ import pytest import allure from base.log import Logger logger = Logger(logger_name='allure', level='error').get_logger() @pytest.fixture(scope="session") # 用例前置操作 def login_fixture(): # 比如登录获取token操作 return "token:xx" @allure.step("用例步骤1") def step_1(): logger.info("用例操作---------------步骤1") return True @allure.step("用例步骤2") def step_2(): logger.info("用例操作---------------步骤2") return False @allure.step("用例步骤3") def step_3(): logger.info("用例操作---------------步骤3") return True @allure.epic("可以对用例或用例集进行描述分类(若出现多个时,内容一致则自动归为一类)") @allure.feature("对用例集或用例进行描述分类---与epic类似,只是比epic级别低") @allure.story("对用例集或用例进行描述分类---与epic类似,只是比feature级别低") class TestAllureDemo: @allure.testcase("https://xxx/testcase/list", name='用例链接testcase') # 为了更好的链接到问题分类或者bug、测试用例地址中(url、name两个参数,可不填写name;可以用@allure.link) @allure.link("https://xxx/testcase/list", name='用例链接link') # 与testcase没有多大区别,从可读性角度还是建议选择@allure.link @allure.issue("https://xxx/testcase/list", name='用例链接issue') # 与testcase区别在于有小虫子图标 @allure.title("用例的标题") # 可参数化标题 @allure.story("用例分类:1") # 可参数化标题 @allure.severity("critical") # 用例等级(blocker critical normal minor trivial) def test_case_1(self, login_fixture): """ 1.用例描述 2.用例步骤 3.预期结果 """ logger.info(login_fixture) # 获取用例前置的信息,比如登录token assert step_1() assert step_2() @allure.story("用例分类:2") def test_case_2(self, login_fixture): logger.info("测试用例2") assert step_1() assert step_3() @allure.epic("冒烟自动化用例") class TestDemo2: @allure.story("用例分类:3") def test_case_3(self, login_fixture): logger.info("测试用例3") step_1() @allure.story("用例分类:4") def test_case_4(self, login_fixture): logger.info("测试用例4") step_3()
三、allure的命令行参数
pytest执行用例时可以加上allure的标记参数,可以控制执行哪些用例。
--allure-severities=SEVERITIES_SET Comma-separated list of severity names. Tests only with these severities will be run. Possible values are: blocker, critical, normal, minor, trivial. --allure-epics=EPICS_SET Comma-separated list of epic names. Run tests that have at least one of the specified feature labels. --allure-features=FEATURES_SET Comma-separated list of feature names. Run tests that have at least one of the specified feature labels. --allure-stories=STORIES_SET Comma-separated list of story names. Run tests that have at least one of the specified story labels. --allure-link-pattern=LINK_TYPE:LINK_PATTERN Url pattern for link type. Allows short links in test, like 'issue-1'. Text will be formatted to full url with python str.format().
实例如下:
# 选择运行你要执行epic的用例 pytest --alluredir ./report/allure --allure-epics=epic对大Story的一个描述性标签 # 选择运行你要执行features的用例 pytest --alluredir ./report/allure --allure-features=模块2 # 选择运行你要执行features的用例 pytest --alluredir ./report/allure --allure-stories="用户故事:1"
四、执行脚本,allure生成报表,并启动报告
运行方式一:
-
命令行模式下运行pytest,生产测试结果文件
pytest demo.py --alluredir ./report/allure -
allure程序启动已经生产的文件
allure serve ./report/allure
运行方式二:
- 编写启动方法,运行pytest
pytest.main([allure_demo.py, "--alluredir", "report/result"])
- 使用进程,开启allure服务
import subprocess subprocess.call('allure generate report/result/ -o report/html --clean', shell=True) subprocess.call('allure open -h 127.0.0.1 -p 9999 ./report/html', shell=True)
(两种方法都需要安装allure工具)
五、报告效果图及注解
============================= 提升自己 ==========================
进群交流、获取更多干货, 请关注微信公众号:

> > > 咨询交流、进群,请加微信,备注来意:sanshu1318 (←点击获取二维码)
> > > 学习路线+测试实用干货精选汇总:
https://www.cnblogs.com/upstudy/p/15859768.html
> > > 【自动化测试实战】python+requests+Pytest+Excel+Allure,测试都在学的热门技术:
https://www.cnblogs.com/upstudy/p/15921045.html
> > > 【热门测试技术,建议收藏备用】项目实战、简历、笔试题、面试题、职业规划:
https://www.cnblogs.com/upstudy/p/15901367.html
> > > 声明:如有侵权,请联系删除。
============================= 升职加薪 ==========================
更多干货,正在挤时间不断更新中,敬请关注+期待。
进群交流、获取更多干货, 请关注微信公众号:

> > > 咨询交流、进群,请加微信,备注来意:sanshu1318 (←点击获取二维码)
> > > 学习路线+测试实用干货精选汇总:
https://www.cnblogs.com/upstudy/p/15859768.html
> > > 【自动化测试实战】python+requests+Pytest+Excel+Allure,测试都在学的热门技术:
https://www.cnblogs.com/upstudy/p/15921045.html
> > > 【热门测试技术,建议收藏备用】项目实战、简历、笔试题、面试题、职业规划:
https://www.cnblogs.com/upstudy/p/15901367.html
> > > 声明:如有侵权,请联系删除。
============================= 升职加薪 ==========================
更多干货,正在挤时间不断更新中,敬请关注+期待。
标签:
自动化测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具