Allure Framework 是一个开源测试报告框架,专注于生成可视化、结构化、可追溯的测试报告,广泛应用于自动化测试体系(UI/API/性能等)。
核心特点:
1.核心架构
flowchart LR
A[测试代码 pytest/TestNG] --> B[Allure Adapter]
B --> C[生成原始结果 allure-results]
C --> D[Allure CLI]
D --> E[HTML 报告]
说明:
| 组件 |
作用 |
| Adapter |
将测试结果转为 Allure 格式 |
| allure-results |
原始 JSON 数据 |
| Allure CLI |
生成 HTML 报告 |
| HTML Report |
可视化展示 |
2. 安装 Allure
2.1 安装Allure Commandline
macOS(推荐)
依赖:Homebrew
Linux(通用)
1 2 3 4 5 6 7 8
| wget https://github.com/allure-framework/allure2/releases/latest/download/allure-2.x.x.tgz
tar -zxvf allure-2.x.x.tgz
export PATH=$PATH:/path/to/allure/bin
|
Windows
或:
2.2 验证安装
2.3 Python环境(pytest)
1
| pip install allure-pytest
|
组件说明:
| 包 |
作用 |
| allure-pytest |
pytest 适配器 |
| allure-commandline |
报告生成 |
3. Allure 报告生成流程
3.1 执行测试并生成结果
1
| pytest --alluredir=./allure-results
|
输出目录:
allure-results/
├── *.json
├── attachments
└── environment.properties
3.2 生成报告
1
| allure generate ./allure-results -o ./allure-report --clean
|
3.3 启动报告
1
| allure serve ./allure-results
|
自动生成 + 启动 Web 服务
4. 报告结构详解
4.1 核心视图
| 模块 |
说明 |
| Overview |
总览(通过率、失败率) |
| Behaviors |
按 Feature/Story 分类 |
| Suites |
测试套件 |
| Graphs |
趋势图 |
| Timeline |
执行时间轴 |
5. pytest + Allure 实战
5.1 标注测试元信息
1 2 3 4 5 6 7
| import allure
@allure.feature("用户模块") @allure.story("登录功能") @allure.severity(allure.severity_level.CRITICAL) def test_login(): assert True
|
5.2 Step 步骤
1 2 3
| @allure.step("输入用户名: {username}") def input_username(username): pass
|
5.3 附件(关键能力)
1 2 3
| import allure
allure.attach("响应内容", "response data", allure.attachment_type.TEXT)
|
截图:
1
| allure.attach.file("error.png", name="失败截图", attachment_type=allure.attachment_type.PNG)
|
6. 高级能力
6.1 CI/CD 集成
常见集成:
- Jenkins
- GitLab CI
- GitHub Actions
Pipeline 示例:
1 2 3 4 5 6 7 8 9 10 11
| stages: - test - report
test: script: - pytest --alluredir=allure-results
report: script: - allure generate allure-results -o allure-report
|
6.2 历史趋势(关键)
1
| cp -r old-report/history allure-results/
|
实现:
6.3 环境信息注入
1
| echo "env=prod" > allure-results/environment.properties
|
6.4 分类失败原因
1 2 3 4 5 6 7
| [ { "name": "接口错误", "matchedStatuses": ["failed"], "messageRegex": "500" } ]
|