Files
smart-management-auto-test/zhyy/test_case/JENKINS_SETUP.md
qiaoxinjiu 6994b185a3 addproject
2026-01-22 19:10:37 +08:00

191 lines
5.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Jenkins + Allure 报告集成配置指南
## 前置要求
1. **Jenkins已安装并运行**
2. **安装Allure插件**
- 进入 Jenkins → Manage Jenkins → Manage Plugins
- 搜索并安装 "Allure Plugin"
3. **安装Allure命令行工具**
- 下载https://github.com/allure-framework/allure2/releases
- 解压并添加到系统PATH环境变量
- 或在Jenkins全局工具配置中配置Allure路径
## Jenkins配置步骤
### 方式一使用Jenkinsfile推荐
1. **在Jenkins中创建Pipeline任务**
- 新建任务 → 选择 "Pipeline"
- 任务名称:例如 "ZZYY_Test_Automation"
2. **配置Pipeline**
- Pipeline definition → Pipeline script from SCM
- SCM: Git或其他版本控制
- Script Path: `zhyy/test_case/Jenkinsfile`
- 保存
3. **运行任务**
- 点击 "Build with Parameters"
- 选择运行方式RUN_TYPE
- 填写相应参数
- 点击 "Build"
### 方式二:自由风格项目配置
1. **创建自由风格项目**
- 新建任务 → 选择 "Freestyle project"
- 任务名称:例如 "ZZYY_Test_Automation"
2. **配置源码管理**
- Source Code Management → Git
- Repository URL: 你的Git仓库地址
- Branch: 分支名称
3. **配置构建步骤**
- Build → Add build step → Execute shellLinux/Mac或 Execute Windows batch commandWindows
- 命令示例:
```bash
# Linux/Mac
cd ${WORKSPACE}
python zhyy/test_case/run_tests.py --all --no-report
```
```batch
# Windows
cd %WORKSPACE%
python zhyy\test_case\run_tests.py --all --no-report
```
4. **配置Allure报告**
- Post-build Actions → Add post-build action → Allure Report
- Results path: `zhyy/test_case/reports/allure-results`
- Report path: `zhyy/test_case/reports/allure-report`(可选)
- 保存
5. **配置参数化构建(可选)**
- This project is parameterized → Add Parameter
- 添加Choice Parameter
- Name: `RUN_TYPE`
- Choices: `all`, `feature`, `story`, `dir`, `file`, `keyword`, `marker`
- 添加String Parameter根据需要
- `FEATURE_NAME`, `STORY_NAME`, `DIR_PATH`, `FILE_PATH`, `KEYWORD`, `MARKER`
6. **修改构建命令以使用参数**
```bash
# Linux/Mac
cd ${WORKSPACE}
if [ "${RUN_TYPE}" = "all" ]; then
python zhyy/test_case/run_tests.py --all --no-report
elif [ "${RUN_TYPE}" = "feature" ]; then
python zhyy/test_case/run_tests.py --feature "${FEATURE_NAME}" --no-report
elif [ "${RUN_TYPE}" = "dir" ]; then
python zhyy/test_case/run_tests.py --dir "${DIR_PATH}" --no-report
# ... 其他条件
fi
```
## Allure插件配置
### 全局工具配置
1. **配置Allure命令行工具路径**
- Manage Jenkins → Global Tool Configuration
- Allure Commandline → Add Allure Commandline
- Name: `Allure`(或自定义名称)
- Installation directory: Allure安装路径`C:\allure\bin` 或 `/usr/local/allure/bin`
- 保存
### 项目配置
1. **在项目配置中添加Allure报告**
- Post-build Actions → Allure Report
- Results path: `zhyy/test_case/reports/allure-results`
- 勾选 "Keep allure results history"
## 环境变量配置
### Jenkins全局环境变量
1. **配置Python路径如需要**
- Manage Jenkins → Configure System → Global properties
- Environment variables → Add
- Name: `PYTHONPATH`
- Value: `${WORKSPACE}`
### 项目环境变量
在Pipeline或构建脚本中设置
```groovy
environment {
PYTHONPATH = "${WORKSPACE}"
ALLURE_RESULTS = "${WORKSPACE}/zhyy/test_case/reports/allure-results"
}
```
## 使用示例
### 运行所有测试
```bash
python zhyy/test_case/run_tests.py --all --no-report
```
### 按目录运行
```bash
python zhyy/test_case/run_tests.py --dir "接口/SZPurchase" --no-report
```
### 按Feature标签运行
```bash
python zhyy/test_case/run_tests.py --feature "深圳采购工作台采购订单页面" --no-report
```
## 报告查看
1. **在Jenkins中查看**
- 构建完成后,在项目页面左侧菜单会出现 "Allure Report" 链接
- 点击即可查看详细的测试报告
2. **报告内容**
- 测试用例执行情况
- 通过/失败统计
- 执行时间
- 测试步骤详情
- 截图和日志(如果配置了)
## 常见问题
### 1. Allure命令未找到
- 确保Allure已安装并添加到PATH
- 或在Jenkins全局工具配置中指定Allure路径
### 2. 模块导入错误
- 检查PYTHONPATH环境变量
- 确保项目根目录在Python路径中
### 3. 报告未生成
- 检查allure-results目录是否存在且包含数据
- 检查Jenkins Allure插件配置的路径是否正确
### 4. 权限问题
- 确保Jenkins有权限访问工作空间目录
- 确保有权限执行Python和Allure命令
## 邮件通知配置(可选)
在Post-build Actions中添加
- Email Notification
- 配置收件人、主题等
- 可以附加Allure报告链接
## 定时构建(可选)
在项目配置中:
- Build Triggers → Build periodically
- 例如:`H 2 * * *`每天凌晨2点执行
## 多节点执行(可选)
如果有多台Jenkins节点
- 在Pipeline中配置 `agent { label 'your-node-label' }`
- 或在自由风格项目中配置 "Restrict where this project can be run"