现实是:测试报告不够“好看/好读” = 没人看。
Allure 的价值不止“颜值”,更在于把一次测试执行拍成电影:
有分镜(Steps)、有证据(Attachments)、有剧情线(Timeline)、还能复盘趋势(History)。
下面这套方案专治两类痛点:
- 开发:想快速定位失败原因(步骤、日志、截图、请求响应)
- 测试/管理:想一眼看懂质量(趋势、分类、谁在失败、失败是不是“老毛病”)
1)整体方案:一条 CI 流水线把报告“产出来 + 发出去”
Jenkins 侧用 Allure 插件后,可以让 Jenkins每次构建自动生成并展示报告入口。([allurereport.org][1])
2)5 分钟跑出第一份 Allure 报告(本地验证闭环)
2.1 安装
Allure 的 pytest 适配器安装非常直接:pip install allure-pytest,运行 pytest 时带--alluredir输出结果目录。([allurereport.org][2])
pipinstallpytest allure-pytest pytest -q --alluredir=allure-results allure serve allure-results
allure serve会起一个临时 Web 服务并自动打开浏览器;
你要落盘分享,用allure generate生成静态 HTML。([allurereport.org][3])
3)让报告“像电影”:Allure 的三大加戏法宝
3.1 分镜:Steps(强烈建议把“业务动作”变成 step)
Allure 支持装饰器 steps 与上下文 steps,两种都基于allure.step()。([allurereport.org][2])
importallureimportpytest@allure.epic("支付")@allure.feature("下单")@allure.story("支付成功链路")@allure.title("支付成功:余额充足时应返回 success")deftest_pay_success():withallure.step("准备订单"):order_id="ORDER-10086"withallure.step("调用支付接口"):resp={"status":"success","order_id":order_id}withallure.step("断言支付结果"):assertresp["status"]=="success"
epic/feature/story这类层级标签能让报告“可导航”,不是一坨用例列表。([allurereport.org][2])
3.2 证据链:Attachments(失败不是一句 assert,得有证据)
Allure 支持把任何文件/字节流 attach 到报告里,比如截图、日志、请求响应。([allurereport.org][2])
importallureimportjsondefattach_json(name:str,obj:dict):allure.attach(json.dumps(obj,ensure_ascii=False,indent=2),name=name,attachment_type=allure.attachment_type.JSON)3.3 自动在失败时“补证据”(开发最爱)
把失败时的日志/响应/截图自动塞进 Allure,体验会直接从“能看”变成“好用”。
核心思路:pytest hook 捕获失败 → attach 关键上下文。
# conftest.pyimportpytestimportallure@pytest.hookimpl(hookwrapper=True)defpytest_runtest_makereport(item,call):outcome=yieldrep=outcome.get_result()ifrep.when=="call"andrep.failed:# 你可以在这里把:最后一次请求/响应、关键日志、截图路径等 attach 进去allure.attach("这里放失败时的关键上下文,例如:最后一次接口响应、堆栈摘要、关键参数等",name="failure_context",attachment_type=allure.attachment_type.TEXT)4)Jenkins 集成:生成报告 + 保证每次构建都能看
4.1 Jenkins 端准备(一次配置,长期收益)
Allure 官方的 Jenkins 集成建议做两件事:
1)安装 Jenkins 的 Allure 插件
2)在 Jenkins “全局工具”里配置 Allure Commandline(插件可自动下载)。([allurereport.org][1])
这一步非常关键:很多人 Jenkinsfile 写对了,但工具没装对,最后就是“空报告/不出报告”。
5)核心 Jenkinsfile(Declarative Pipeline):跑测试 → 生成 Allure → 自动发邮件
Allure 插件在 Pipeline 里提供allurestep,参数里最关键的是results: [[path: '...']]。([Jenkins][4])
pipeline{agent any stages{stage('Test'){steps{sh''' python -m pip install -U pip pip install -r requirements.txt pytest -q --alluredir=allure-results '''}post{always{// 生成并挂载 Allure Report 到构建页面allure includeProperties:false,jdk:'',results:[[path:'allure-results']]}}}}post{always{// 邮件只发“关键内容”:结论 + 入口链接emailext(to:'qa@your.com, dev@your.com',subject:"【${currentBuild.currentResult}】${env.JOB_NAME} #${env.BUILD_NUMBER} 自动化报告",mimeType:'text/html',body:""" <p><b>结果:</b>${currentBuild.currentResult}</p> <p><b>构建:</b><a href="${env.BUILD_URL}">${env.BUILD_URL}</a></p> <p><b>Allure:</b>打开上面构建链接,在左侧/页面中进入 <b>Allure Report</b>。</p> """)}}}emailext是 Email Extension 插件提供的 Pipeline step,可直接在 Jenkinsfile 里发送邮件(支持 HTML、附件、收件人规则等)。([Jenkins][5])
6)把报告做“更像线上产品”:趋势 / 分类 / 环境信息(加分项)
Allure 的报告里,趋势图/历史需要history文件;环境信息可以放environment;CI 信息可以放executor。这些都属于“Launch Data”的范畴。([allurereport.org][3])
你不需要一口吃成胖子,最实用的顺序是:
- 环境信息:Python 版本、接口环境、分支名、commit id
- 失败分类:把 “断言失败/超时/环境问题” 分门别类(管理层最爱)
- 历史趋势:每天跑回归,趋势图立刻有价值
7)高频翻车点(提前避坑 = 少掉 80% 破事)
- 空报告:pytest 没有输出到
--alluredir或路径不一致(Jenkins 读不到 results)([allurereport.org][2]) - 工具没装:Jenkins 只装了插件,没配置 Allure Commandline 全局工具([allurereport.org][1])
- 报告没人看:只发“测试失败”四个字没意义——发“结论 + 入口链接 + Top 失败摘要”才有人点
- Steps 太细:每行都 step 会变成“流水账”;按业务动作分镜即可
8)最后一句:你要的不是“漂亮报告”,是“团队愿意看”的反馈回路
Allure 的高颜值确实是点击保证,但真正留住开发的,是:
- 失败用例一眼能复现(步骤清晰)
- 失败原因一眼能定位(证据齐全)
- 质量趋势一眼能判断(历史可追溯)([allurereport.org][3])