5.2 KiB
5.2 KiB
Jenkins + Allure 报告集成配置指南
前置要求
- Jenkins已安装并运行
- 安装Allure插件
- 进入 Jenkins → Manage Jenkins → Manage Plugins
- 搜索并安装 "Allure Plugin"
- 安装Allure命令行工具
- 下载:https://github.com/allure-framework/allure2/releases
- 解压并添加到系统PATH环境变量
- 或在Jenkins全局工具配置中配置Allure路径
Jenkins配置步骤
方式一:使用Jenkinsfile(推荐)
-
在Jenkins中创建Pipeline任务
- 新建任务 → 选择 "Pipeline"
- 任务名称:例如 "ZZYY_Test_Automation"
-
配置Pipeline
- Pipeline definition → Pipeline script from SCM
- SCM: Git(或其他版本控制)
- Script Path:
zhyy/test_case/Jenkinsfile - 保存
-
运行任务
- 点击 "Build with Parameters"
- 选择运行方式(RUN_TYPE)
- 填写相应参数
- 点击 "Build"
方式二:自由风格项目配置
-
创建自由风格项目
- 新建任务 → 选择 "Freestyle project"
- 任务名称:例如 "ZZYY_Test_Automation"
-
配置源码管理
- Source Code Management → Git
- Repository URL: 你的Git仓库地址
- Branch: 分支名称
-
配置构建步骤
- Build → Add build step → Execute shell(Linux/Mac)或 Execute Windows batch command(Windows)
- 命令示例:
# Linux/Mac cd ${WORKSPACE} python zhyy/test_case/run_tests.py --all --no-report# Windows cd %WORKSPACE% python zhyy\test_case\run_tests.py --all --no-report -
配置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(可选) - 保存
-
配置参数化构建(可选)
- This project is parameterized → Add Parameter
- 添加Choice Parameter:
- Name:
RUN_TYPE - Choices:
all,feature,story,dir,file,keyword,marker
- Name:
- 添加String Parameter(根据需要):
FEATURE_NAME,STORY_NAME,DIR_PATH,FILE_PATH,KEYWORD,MARKER
-
修改构建命令以使用参数
# 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插件配置
全局工具配置
- 配置Allure命令行工具路径
- Manage Jenkins → Global Tool Configuration
- Allure Commandline → Add Allure Commandline
- Name:
Allure(或自定义名称) - Installation directory: Allure安装路径(如:
C:\allure\bin或/usr/local/allure/bin) - 保存
项目配置
- 在项目配置中添加Allure报告
- Post-build Actions → Allure Report
- Results path:
zhyy/test_case/reports/allure-results - 勾选 "Keep allure results history"
环境变量配置
Jenkins全局环境变量
- 配置Python路径(如需要)
- Manage Jenkins → Configure System → Global properties
- Environment variables → Add
- Name:
PYTHONPATH - Value:
${WORKSPACE}
项目环境变量
在Pipeline或构建脚本中设置:
environment {
PYTHONPATH = "${WORKSPACE}"
ALLURE_RESULTS = "${WORKSPACE}/zhyy/test_case/reports/allure-results"
}
使用示例
运行所有测试
python zhyy/test_case/run_tests.py --all --no-report
按目录运行
python zhyy/test_case/run_tests.py --dir "接口/SZPurchase" --no-report
按Feature标签运行
python zhyy/test_case/run_tests.py --feature "深圳采购工作台采购订单页面" --no-report
报告查看
-
在Jenkins中查看
- 构建完成后,在项目页面左侧菜单会出现 "Allure Report" 链接
- 点击即可查看详细的测试报告
-
报告内容
- 测试用例执行情况
- 通过/失败统计
- 执行时间
- 测试步骤详情
- 截图和日志(如果配置了)
常见问题
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"