本博文主要介绍jenkins中pipeline如何和allure做对接生成测试报告;
至于如何安装jenkins,pytest以及allure,allure command,由于篇幅限制,安装配置就不做解释了,网上很多这种详细博客,只要思路和顺序做对了就成功了;
上图是我集成后,自动提交代码执行一系列的步骤,最后生成测试用例;
1,想好你的代码是在window中还是在linux中,如果是windows中转移到linux中,需要注意路径问题 (路径问题)可参考: ImportError: attempted relative import with no known parent package
2,Jenkins的安装和配置,能在浏览器中打开你安装的jenkins,在你的jenkins当中插件管理中心,安装Allure Jenkins Plugin 我的版本是2.29.0;pipeline方式是不需要在jenkins中的全局工具配置那里配置allure的路径的;
3,Jenkins的Pipeline书写规则,可参考官方文档:Jenkins Pipeline官网文档
4,Pytest测试用例的代码开发完毕
5,在你的python环境中安装allure,以及在你的电脑环境中安装allure-command,配置环境变量,注意版本,有时候生成的报告内容为空,就是版本不匹配导致的
我的环境:
windows:
python版本:3.7.10
pytest 版本:6.2.5
电脑系统中的 allure-command 版本:2.15.0
python环境中 allure-pytest 版本:2.9.44 (pip安装这个 下面的会自动安装,如果没有再手动安装)
python环境中的allure-python-commons 版本:2.9.44
CentOs7.4:
Python版本:3.7.4
pytest版本:6.2.5
服务器系统中的allure-command 版本:2.15.0
Python环境中的allure-pytest版本:2.9.45
Python环境中的allure-python-commons版本:2.9.45
6,假设你前面五个步骤都ok了,在linux中你的测试用例代码也可以跑通(python3 pytestmain.py),allure运行也是正常的,而且运行测试用例的脚本可以生成报告了;
那么就可以开始将pytest生成的测试用例数据(以及被allure生成的报告)集成到jenkins中【linux环境】,首先你要知道pipeline是需要编写脚本的,同时你也要知道groovy的一些语法;使用过pipeline语法的都熟悉这个界面吧?
每个stage在jenkins中显示的时候就会显示为对应的方框,以及对应的执行脚本日志,在你的*.groovy 脚本文件中,编写如下代码:
// Test cases
stage('execute test cases'){
steps{
script{
sh("echo -----即将开始,执行测试用例,生成测试报告-----")
sh(" cd /home/进入你的测试用例所在目录 ; python3 test_main.py beta")
allure([
includeProperties: false,
jdk: '',
properties: [],
reportBuildPolicy: 'ALWAYS',
report: '/var/lib/jenkins/workspace/BetaBranchPipeline/allure-report',
results: [[path: '/var/lib/jenkins/workspace/BetaBranchPipeline/allure-results']]
])
}
}
}
注意的你report的路径以及results的路径,需要在你的pytest中也要对应的上,不然jenkins会找不到allure报告的数据,显示空白;如下为在我的pytest中指定的生成数据目录,以及指定allure的目录;
pytest.main(['-s', '--alluredir', '/var/lib/jenkins/workspace/BetaBranchPipeline/allure-results'])
os.system('allure generate /var/lib/jenkins/workspace/BetaBranchPipeline/allure-results -o /var/lib/jenkins/workspace/BetaBranchPipeline/allure-report --clean')
在这里我没有保存历史记录,每次都会清空上次的数据,但是在jenkins当中不必考虑这个问题,因为每次构建都会在一个单独的构建历史记录里面,这个历史记录可以在你的jenkins当中配置;
在我这里设置的是总共保持最近八次的构建记录
到这里就基本结束了,每次运行结束自动运行测试用例,自动生成测试报告,会看到每次构建的历史记录那里多了个彩色的圆圈,点击那里会跳转到你当次测试用例的详细信息;