text
<div style="margin:24px 0;border:1px solid #E3F2FD;border-radius:8px;background:#FAFAFA;overflow:hidden;"><div style="display:flex;align-items:center;gap:12px;padding:12px 20px;background:linear-gradient(135deg,#E3F2FD,#BBDEFB);"><span style="width:36px;height:36px;background:#2196F3;color:#fff;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;font-weight:bold;font-size:16px;flex-shrink:0;">2</span><span style="font-size:17px;font-weight:700;color:#1565C0;">在测试框架中集成Allure适配器</span></div><div style="padding:16px 20px;"><p>以Python的pytest为例。</p>
<p>先安装依赖:</p>
<div style="margin:16px 0;border-radius:8px;overflow:hidden;border:1px solid #E0E0E0;"><div style="background:#263238;color:#B0BEC5;padding:6px 16px;font-size:12px;font-family:Consolas,monospace;"><span style="text-transform:uppercase;letter-spacing:1px;">bash</span></div><pre style="background:#1E272E;color:#E0E0E0;padding:16px 20px;margin:0;overflow-x:auto;font-size:14px;line-height:1.7;font-family:Consolas,"Courier New",monospace;"><code>pip install allure-pytest</code></pre></div>
<p>然后在测试代码中添加Allure注解:</p>
<div style="margin:16px 0;border-radius:8px;overflow:hidden;border:1px solid #E0E0E0;"><div style="background:#263238;color:#B0BEC5;padding:6px 16px;font-size:12px;font-family:Consolas,monospace;"><span style="text-transform:uppercase;letter-spacing:1px;">python</span></div><pre style="background:#1E272E;color:#E0E0E0;padding:16px 20px;margin:0;overflow-x:auto;font-size:14px;line-height:1.7;font-family:Consolas,"Courier New",monospace;"><code>import allure
import pytest
@allure.feature("用户登录")
class TestLogin:
@allure.story("登录成功")
@allure.title("使用正确用户名和密码登录")
@allure.severity(allure.severity_level.CRITICAL)
def test_login_success(self):
with allure.step("输入用户名"):
print("输入admin")
with allure.step("输入密码"):
print("输入123456")
with allure.step("点击登录按钮"):
print("登录成功")
assert True
@allure.story("登录失败")
@allure.title("使用错误密码登录")
@allure.severity(allure.severity_level.NORMAL)
def test_login_fail(self):
with allure.step("输入用户名"):
print("输入admin")
with allure.step("输入错误密码"):
print("输入wrong_password")
with allure.step("点击登录按钮"):
print("登录失败")
assert False</code></pre></div>
<p>运行测试并生成Allure结果文件:</p>
<div style="margin:16px 0;border-radius:8px;overflow:hidden;border:1px solid #E0E0E0;"><div style="background:#263238;color:#B0BEC5;padding:6px 16px;font-size:12px;font-family:Consolas,monospace;"><span style="text-transform:uppercase;letter-spacing:1px;">bash</span></div><pre style="background:#1E272E;color:#E0E0E0;padding:16px 20px;margin:0;overflow-x:auto;font-size:14px;line-height:1.7;font-family:Consolas,"Courier New",monospace;"><code>pytest --alluredir=./allure-results</code></pre></div>
<h3 id="第3步生成并查看报告" style="color:#1976D2;margin-top:24px;">第3步:生成并查看报告</h3>
<p>运行完测试后,执行:</p>
<div style="margin:16px 0;border-radius:8px;overflow:hidden;border:1px solid #E0E0E0;"><div style="background:#263238;color:#B0BEC5;padding:6px 16px;font-size:12px;font-family:Consolas,monospace;"><span style="text-transform:uppercase;letter-spacing:1px;">bash</span></div><pre style="background:#1E272E;color:#E0E0E0;padding:16px 20px;margin:0;overflow-x:auto;font-size:14px;line-height:1.7;font-family:Consolas,"Courier New",monospace;"><code>allure serve ./allure-results</code></pre></div>
<p>浏览器会自动打开,展示一份完整的测试报告。</p>
<p>> 3行代码,让你的测试报告从“石器时代”进入“信息时代”。</p></div></div>
## 03 Allure报告的5大核心功能
<div style="margin:24px 0;border:1px solid #E3F2FD;border-radius:8px;background:#FAFAFA;overflow:hidden;"><div style="display:flex;align-items:center;gap:12px;padding:12px 20px;background:linear-gradient(135deg,#E3F2FD,#BBDEFB);"><span style="width:36px;height:36px;background:#2196F3;color:#fff;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;font-weight:bold;font-size:16px;flex-shrink:0;">1. </span><span style="font-size:17px;font-weight:700;color:#1565C0;">仪表盘:全局一览</span></div><div style="padding:16px 20px;"><p>打开报告,第一眼就是仪表盘。</p>
<p>包含:</p>
<ul style="list-style:none;padding-left:0;margin:12px 0;">
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>测试用例总数</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>通过/失败/跳过数量</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>执行时间</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>通过率趋势图</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>失败原因分布图</li>
</ul>
<p>所有数据,3秒内读懂。</p></div></div>
<div style="margin:24px 0;border:1px solid #E3F2FD;border-radius:8px;background:#FAFAFA;overflow:hidden;"><div style="display:flex;align-items:center;gap:12px;padding:12px 20px;background:linear-gradient(135deg,#E3F2FD,#BBDEFB);"><span style="width:36px;height:36px;background:#2196F3;color:#fff;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;font-weight:bold;font-size:16px;flex-shrink:0;">2. </span><span style="font-size:17px;font-weight:700;color:#1565C0;">测试套件:用例组织</span></div><div style="padding:16px 20px;"><p>Allure支持用 <code style="background:#F3E5F5;color:#6A1B9A;padding:1px 6px;border-radius:3px;font-size:0.9em;font-family:Consolas,monospace;">@allure.feature</code> 和 <code style="background:#F3E5F5;color:#6A1B9A;padding:1px 6px;border-radius:3px;font-size:0.9em;font-family:Consolas,monospace;">@allure.story</code> 对用例进行分类。</p>
<p>比如:</p>
<ul style="list-style:none;padding-left:0;margin:12px 0;">
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>功能模块:登录、注册、搜索</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>用户故事:正常流程、异常场景</li>
</ul>
<p>点击任何一个套件,直接看到该模块的所有用例状态。</p></div></div>
<div style="margin:24px 0;border:1px solid #E3F2FD;border-radius:8px;background:#FAFAFA;overflow:hidden;"><div style="display:flex;align-items:center;gap:12px;padding:12px 20px;background:linear-gradient(135deg,#E3F2FD,#BBDEFB);"><span style="width:36px;height:36px;background:#2196F3;color:#fff;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;font-weight:bold;font-size:16px;flex-shrink:0;">3. </span><span style="font-size:17px;font-weight:700;color:#1565C0;">用例详情:步骤级可视化</span></div><div style="padding:16px 20px;"><p>点击一个失败用例,你能看到:</p>
<ul style="list-style:none;padding-left:0;margin:12px 0;">
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>每一步操作(step)</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>每一步的输入/输出</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>失败时的错误堆栈</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>附带的截图(如果有)</li>
</ul>
<p>> 失败用例,不再需要“猜”原因。</p></div></div>
<div style="margin:24px 0;border:1px solid #E3F2FD;border-radius:8px;background:#FAFAFA;overflow:hidden;"><div style="display:flex;align-items:center;gap:12px;padding:12px 20px;background:linear-gradient(135deg,#E3F2FD,#BBDEFB);"><span style="width:36px;height:36px;background:#2196F3;color:#fff;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;font-weight:bold;font-size:16px;flex-shrink:0;">4. </span><span style="font-size:17px;font-weight:700;color:#1565C0;">附件:截图、日志、参数</span></div><div style="padding:16px 20px;"><p>Allure支持为每个用例添加附件:</p>
<ul style="list-style:none;padding-left:0;margin:12px 0;">
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>截图:<code style="background:#F3E5F5;color:#6A1B9A;padding:1px 6px;border-radius:3px;font-size:0.9em;font-family:Consolas,monospace;">allure.attach.file("screenshot.png")</code></li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>日志:<code style="background:#F3E5F5;color:#6A1B9A;padding:1px 6px;border-radius:3px;font-size:0.9em;font-family:Consolas,monospace;">allure.attach("日志内容", "log.txt")</code></li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>参数:<code style="background:#F3E5F5;color:#6A1B9A;padding:1px 6px;border-radius:3px;font-size:0.9em;font-family:Consolas,monospace;">allure.attach("参数: username=admin", "params.txt")</code></li>
</ul>
<p>代码示例:</p>
<div style="margin:16px 0;border-radius:8px;overflow:hidden;border:1px solid #E0E0E0;"><div style="background:#263238;color:#B0BEC5;padding:6px 16px;font-size:12px;font-family:Consolas,monospace;"><span style="text-transform:uppercase;letter-spacing:1px;">python</span></div><pre style="background:#1E272E;color:#E0E0E0;padding:16px 20px;margin:0;overflow-x:auto;font-size:14px;line-height:1.7;font-family:Consolas,"Courier New",monospace;"><code>@allure.step("截图当前页面")
def take_screenshot():
allure.attach(driver.get_screenshot_as_png(), name="页面截图",
attachment_type=allure.attachment_type.PNG)</code></pre></div></div></div>
<div style="margin:24px 0;border:1px solid #E3F2FD;border-radius:8px;background:#FAFAFA;overflow:hidden;"><div style="display:flex;align-items:center;gap:12px;padding:12px 20px;background:linear-gradient(135deg,#E3F2FD,#BBDEFB);"><span style="width:36px;height:36px;background:#2196F3;color:#fff;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;font-weight:bold;font-size:16px;flex-shrink:0;">5. </span><span style="font-size:17px;font-weight:700;color:#1565C0;">趋势分析:历史数据</span></div><div style="padding:16px 20px;"><p>Allure支持多个测试运行结果的对比。</p>
<p>执行:</p>
<div style="margin:16px 0;border-radius:8px;overflow:hidden;border:1px solid #E0E0E0;"><div style="background:#263238;color:#B0BEC5;padding:6px 16px;font-size:12px;font-family:Consolas,monospace;"><span style="text-transform:uppercase;letter-spacing:1px;">bash</span></div><pre style="background:#1E272E;color:#E0E0E0;padding:16px 20px;margin:0;overflow-x:auto;font-size:14px;line-height:1.7;font-family:Consolas,"Courier New",monospace;"><code>allure generate ./allure-results --clean -o ./allure-report</code></pre></div>
<p>生成的 <code style="background:#F3E5F5;color:#6A1B9A;padding:1px 6px;border-radius:3px;font-size:0.9em;font-family:Consolas,monospace;">./allure-report</code> 文件夹内,包含历史趋势图。</p>
<p>你可以看到:</p>
<ul style="list-style:none;padding-left:0;margin:12px 0;">
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>通过率是上升还是下降</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>失败用例是增加还是减少</li>
<li style="position:relative;padding:4px 0 4px 24px;line-height:1.8;"><span style="color:#2196F3;font-weight:bold;position:absolute;left:4px;">►</span>执行时间是变长还是变短</li>
</ul>
<p>这些数据,直接指导测试策略的调整。</p></div></div>
## 04 高级技巧:让报告更专业
### 技巧1:动态标题
有时候,用例标题需要包含动态参数。
Allure支持: