Pytest框架的GitHub项目Jenkins持续集成
Posted liangjt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pytest框架的GitHub项目Jenkins持续集成相关的知识,希望对你有一定的参考价值。
Jenkins安装Allure插件
1.进入jenkins系统管理 -> 管理插件
2.点击可选插件
3.搜索框输入Allure Jenkins Plugin
4.选中安装,安装后如下。
Jenkins安装Allure Commandline工具
1.进入jenkins系统管理 -> 全局工具安装(Global Tool Configuration)
2.找到Allure Commandline,点击Allure Commandline安装
3.输入一个别名,取消勾选Install automatically
4.输入已下载好的allure2.9.0所在路径,注意:不要进入到bin目录
5.点击保存
Jenkins新建一个项目
1.选择新建一个自由风格的软件项目 -> 点击确定
2.输入一些项目描述
3.选择GitHub 项目
4.输入项目 url
源码管理配置
5.勾选Git
6.Repository URL输入地址同第四步
7.点击Add添加github的用户名和密码
构建触发器
8.勾选Poll SCM # 根据定时任务,查看github版本是否更新,如果更新会自动构建项目
9.输入crontab命令,
举例:
*/1 * * * * # 每一分钟检查一次
构建(以Windows为例)
10.点击增加构建步骤,选择Execute windows batch command
11.Command输入以下内容:
set path=%path%;D:Anaconda3;D:Anaconda3Scripts;D:Anaconda3Libraryin;D:Anaconda3Libraryusrin;D:Anaconda3Librarymingw-w64in
pytest
exit 0
注意:
-这里set path设置的是系统环境变量,如果你已经在系统环境变量里把以上东西放进path里了,这里可以忽略。
-因为我只是放在了个人环境变量里,所以在jekins直接运行pytest,会报错‘pytest‘ 不是内部或外部命令...
-运行jekins可以临时把pytest放进环境变量里,在cmd运行where pytest找到pytest.exe所在目录,我这里是在Anaconda3Scripts,并且把其相关的也加入进来,就像去高级系统设置添加环境变量一样,否则会报错找不到模块。
-jenkins执行windows命令,若退出代码不为0 ,则jenkins会将构建标记为失败,所以需要加上exit 0表示执行完成退出。
构建后操作
12.点击增加构建后操作步骤,选择Allure Report
13.Path路径输入:生成的报告文件夹名称。(
?? 文件夹名称与pytest生成的报告文件夹名称一致)(我的是report文件夹:addopts = -s --alluredir=report)
14.设置完成点击保存。
触发项目构建
项目构建会运行测试代码,构建方式有两种:
1.手动触发构建
2.更新github代码,触发器在定时任务到达时,会出发项目构建
成功后控制台输出
Started by user admin Running as SYSTEM Building in workspace D:Program Files (x86)Jenkinsworkspaceallure_test using credential b3b8744d-d4b3-4053-aabf-53ec58085521 > D:Program Files (x86)Gitingit.exe rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > D:Program Files (x86)Gitingit.exe config remote.origin.url https://github.com/JEyeshield/Test_Allure.git # timeout=10 Fetching upstream changes from https://github.com/JEyeshield/Test_Allure.git > D:Program Files (x86)Gitingit.exe --version # timeout=10 using GIT_ASKPASS to set credentials > D:Program Files (x86)Gitingit.exe fetch --tags --force --progress -- https://github.com/JEyeshield/Test_Allure.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > D:Program Files (x86)Gitingit.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10 > D:Program Files (x86)Gitingit.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10 Checking out Revision 4959d39a771ba90b1d029fe25eb1d85c12c765b8 (refs/remotes/origin/master) > D:Program Files (x86)Gitingit.exe config core.sparsecheckout # timeout=10 > D:Program Files (x86)Gitingit.exe checkout -f 4959d39a771ba90b1d029fe25eb1d85c12c765b8 # timeout=10 Commit message: "v2" > D:Program Files (x86)Gitingit.exe rev-list --no-walk 4959d39a771ba90b1d029fe25eb1d85c12c765b8 # timeout=10 [allure_test] $ cmd /c call C:WINDOWSTEMPjenkins1408643635908210552.bat D:Program Files (x86)Jenkinsworkspaceallure_test>set path=C:ProgramDataOracleJavajavapath;C:WINDOWS;C:WINDOWSsystem32;C:WINDOWSSystem32;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:WINDOWSSystem32OpenSSH;"D:Program Files (x86)Javajdk1.8.0_144in;D:Program Files (x86)Javajdk1.8.0_144jrein;";"D:Program Files (x86)jmeterapache-jmeter-5.1.1in;";D:Program Files (x86)Gitcmd;C:WINDOWSsystem32configsystemprofileAppDataLocalMicrosoftWindowsApps;D:Anaconda3;D:Anaconda3Scripts;D:Anaconda3Libraryin;D:Anaconda3Libraryusrin;D:Anaconda3Librarymingw-w64in D:Program Files (x86)Jenkinsworkspaceallure_test>pytest ============================= test session starts ============================= platform win32 -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 rootdir: D:Program Files (x86)Jenkinsworkspaceallure_test, inifile: pytest.ini, testpaths: ./scripts plugins: allure-pytest-2.8.13, arraydiff-0.3, doctestplus-0.4.0, html-2.1.1, metadata-1.8.0, openfiles-0.4.0, remotedata-0.3.2 collected 4 items scripts est_001.py ...F ================================== FAILURES =================================== __________________________ Test_Abc.test_abc_002[2] ___________________________ self = <scripts.test_001.Test_Abc object at 0x00000136BE490388>, a = 2 @pytest.mark.parametrize(‘a‘, [0, 1, 2]) @allure.severity(allure.severity_level.CRITICAL) @allure.step(title=‘第二个测试.‘) def test_abc_002(self, a): allure.attach(‘描述‘, ‘我是测试步骤002的描述~~~‘) > assert a != 2 E assert 2 != 2 scripts est_001.py:15: AssertionError ========================= 1 failed, 3 passed in 0.15s ========================= D:Program Files (x86)Jenkinsworkspaceallure_test>exit 0 [allure_test] $ "D:Program Files (x86)allure-2.9.0inallure.bat" generate "D:Program Files (x86)Jenkinsworkspaceallure_test eport" -c -o "D:Program Files (x86)Jenkinsworkspaceallure_testallure-report" Report successfully generated to D:Program Files (x86)Jenkinsworkspaceallure_testallure-report Allure report was successfully generated. Creating artifact for the build. Artifact was added to the build. Build step ‘Allure Report‘ changed build result to UNSTABLE Finished: UNSTABLE
查看Allure Report报告内容
以上是关于Pytest框架的GitHub项目Jenkins持续集成的主要内容,如果未能解决你的问题,请参考以下文章
Jenkins+python+allure-pytest之API自动化测试框架
pytest系列- pytest+allure+jenkins - 持续集成平台生成allure报告
python+requests+pytest+yaml/json+Allure+jenkins+docker接口自动化框架保姆级教学