在之前的五篇文章中,我们介绍了从Git、Maven、Junit、SonarQube、Sh等工具及技术介绍了从持续集成到静态代码扫描再到简单的部署。基本可以满足了我们在基础开发过程的流水线。
之前的文章链接见下:
Devops关键工具及技术(一)—Jenkins 容器化
Devops关键工具及技术(二)—Jenkins2.0 Pipeline
Devops关键工具及技术(三)—基于Pipeline的Git+Maven+Junit持续集成
Devops关键工具及技术(四)—基于Pipeline的SonarQube静态代码扫描
Devops关键工具及技术(五)—基于Pipeline的Bash脚本部署
接下来,我们将介绍在流水线中加入测试相关的功能,如Web自动化测试、性能测试这些测试流水线。Web自动化测试加入到流水线中,将会用三篇文章进行介绍。在Web自动化测试的框架中将会选用RobotFramework+Selenium2Library。三篇文章分别是Windows下Web自动化测试工具安装、Web自动化测试工具的容器化、Web自动化测试的流水线集成。
本篇我们将介绍Web自动化测试的流水线集成。
我们将会参考Devops关键工具及技术(一)—Jenkins 容器化中的增加Slave的方式来将自动化测试节点加入到Jenkins中,目的为了将自动化测试成为流水线的一部分。
在此之前我们需要将自动化测试的工具进行容器化,具体可以参考Web自动化测试工具的容器化。
在做配置之前我们也还是需要对该加入进来的节点做一下配置,具体的操作已经在Devops关键工具及技术(一)—Jenkins 容器化中提到过,这里不做过多讲解。可参考下面的截图
基于之前文章中的流水线,我们加入Web自动化测试的Stage。在此之前我们的Pipeline里面加入了Checkout Code、Mvn Build、Sonar、Bash Deploy等Stage,这次我们在后面加上Robot Web自动化测试的Stage。如下:
内容也可以在Github中找到
https://github.com/zbbkeepgoing/pipeline-sample
pipeline {
agent none
stages {
stage('Preparation') {
agent { node { label 'master' } }
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.com/zbbkeepgoing/springboot-demo.git']]])
}
}
stage('Build') {
agent { node { label 'master' } }
steps {
dir(env.WORKSPACE){
sh "mvn clean install"
junit allowEmptyResults: true, keepLongStdio: true, testResults: 'target/surefire-reports/*.xml'
sh "mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar"
}
}
}
stage('Sonarqube') {
agent { node { label 'master' } }
steps {
dir(env.WORKSPACE){
sh "mvn sonar:sonar -Dsonar.host.url=http://192.168.88.130:9000 -Dsonar.login=2382ac098363521b98731e286e52e1ad22adef2b" //指定sonar的ip和token
}
}
}
stage('Deploy') {
agent { node { label 'master' } }
steps {
dir(env.WORKSPACE){
sh 'sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no target/sample.jar [email protected]:/opt/ansible' //把部署的jar传到目标机器上
sh 'sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no deploy.sh [email protected]:/opt/ansible' //把脚本传到目标机器上
sh 'sshpass -p 1qaz@WSX ssh -o StrictHostKeychecking=no [email protected] "bash /opt/ansible/deploy.sh"' //在目标机器上执行对应的脚本
sh 'sleep 8s' //睡眠8s
}
}
}
stage('Robot Framework') {
agent { node { label 'robot' } } //指定label为robot节点,即自动化测试节点
steps {
dir(env.WORKSPACE){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.com/zbbkeepgoing/springboot-demo.git']]]) //拉去最新工程,因为自动化测试脚本在工程中
sh "pybot -d /opt/workspace/CI+Sonar+Sh+Robot robot/demo.robot" //生成报告路径需要注意
step([$class: 'RobotPublisher', //将自动化测试的报告推送给jenkins服务
disableArchiveOutput: false,
logFileName: 'log.html',
otherFiles: '',
outputFileName: 'output.xml',
outputPath: '.',
passThreshold: 40,
reportFileName: 'report.html',
unstableThreshold: 0]);
}
}
}
}
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
......
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] checkout
> git rev-parse --is-inside-work-tree # timeout=10
.......
Commit message: "Update deploy.sh"
> git rev-list --no-walk 1aa03a20b88565b775a23a759fde2701cabe8592 # timeout=10
......
[Pipeline] { (Build)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh+Robot] Running shell script
+ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
......
-------------------------------------------------------
T E S T S
-------------------------------------------------------
......
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
......
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.944 s
[INFO] Finished at: 2018-10-19T15:39:20+00:00
[INFO] Final Memory: 29M/69M
[INFO] ------------------------------------------------------------------------
[Pipeline] junit
Recording test results
[Pipeline] sh
[CI+Sonar+Sh+Robot] Running shell script
+ mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar
......
[Pipeline] { (Sonarqube)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh+Robot] Running shell script
+ mvn sonar:sonar -Dsonar.host.url=http://192.168.88.130:9000 -Dsonar.login=65607ba9d0f54590cf55fe8e60134fb5e87c557d
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- sonar-maven-plugin:3.5.0.1254:sonar (default-cli) @ sample ---
[INFO] User cache: /var/jenkins_home/.sonar/cache
[INFO] SonarQube version: 7.1.0
......
[INFO] Analysis report generated in 420ms, dir size=51 KB
[INFO] Analysis reports compressed in 61ms, zip size=16 KB
[INFO] Analysis report uploaded in 104ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://192.168.88.130:9000/dashboard/index/com.dxc.ddccloud:sample
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://192.168.88.130:9000/api/ce/task?id=AWaM-5RsqIizZTHi4NZ_
[INFO] Task total time: 15.023 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.297 s
[INFO] Finished at: 2018-10-19T15:39:50+00:00
[INFO] Final Memory: 32M/154M
[INFO] ------------------------------------------------------------------------
......
[Pipeline] { (Deploy)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] dir
Running in /var/jenkins_home/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] sh
[CI+Sonar+Sh+Robot] Running shell script
+ sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no target/sample.jar [email protected]:/opt/ansible
[Pipeline] sh
[CI+Sonar+Sh+Robot] Running shell script
+ sshpass -p 1qaz@WSX scp -o StrictHostKeychecking=no deploy.sh [email protected]:/opt/ansible
[Pipeline] sh
[CI+Sonar+Sh+Robot] Running shell script
+ sshpass -p 1qaz@WSX ssh -o StrictHostKeychecking=no [email protected] bash /opt/ansible/deploy.sh
[Pipeline] sh
[CI+Sonar+Sh+Robot] Running shell script
+ sleep 8s
......
[Pipeline] { (Robot Framework)
[Pipeline] node
Still waiting to schedule task
All nodes of label ‘robot’ are offline
Running on robot-00000stm8ezap on 192.168.88.130robot in /opt/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] dir
Running in /opt/workspace/CI+Sonar+Sh+Robot
[Pipeline] {
[Pipeline] checkout
> git rev-parse --is-inside-work-tree # timeout=10
......
Commit message: "Update deploy.sh"
[Pipeline] sh
[CI+Sonar+Sh+Robot] Running shell script
+ pybot -d /opt/workspace/CI+Sonar+Sh+Robot robot/demo.robot
==============================================================================
Demo
==============================================================================
Demo | PASS |
------------------------------------------------------------------------------
Baidu | PASS |
------------------------------------------------------------------------------
Demo | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output: /opt/workspace/CI+Sonar+Sh+Robot/output.xml
Log: /opt/workspace/CI+Sonar+Sh+Robot/log.html
Report: /opt/workspace/CI+Sonar+Sh+Robot/report.html
[Pipeline] step
Robot results publisher started...
-Parsing output xml:
Done!
-Copying log files to build dir:
Done!
-Assigning results to build:
Done!
-Checking thresholds:
Done!
Done publishing Robot results.
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS
有三个地方我们可以查看以及获取报告:
1、Jenkins构建结果中:
2、上图中点击Original result files。可以下载原报告进行查看
3、在宿主机的目录下
以上就是我们Web自动化测试集成Pipeline的所有内容。后续我们会把Jmeter性能测试加入到Pipeline中。