1. jenkins 集成单元测试
1.1先来一张图
趋势图和最新测试结果 出现的前提必须有一次成功的测试通过才能出现!
1.2 点击红色。可以看到具体那个单元测试类报错,点到具体的测试类,会显示对应方法,和错误原因。
2.配置
pip流水线代码:
pipeline {
agent any
tools{
maven "maven3"
}
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git branch: 'test', url: 'http://url/zeus.git'
// Run Maven on a Unix agent.
sh "mvn -X test"
// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}
post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
success {
junit '**/target/surefire-reports/TEST-*.xml'
}
}
}
}
}
上述脚本必须是所有单元测试通过的场景才会发布单元测试报告(修改post,使其失败也能展示单元测试)
改进的流水线写法:
pipeline {
agent any
tools{
maven "maven3"
}
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git branch: 'test', url: 'http://url/zeus.git'
// Run Maven on a Unix agent.
sh "mvn -X test"
// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}
post {
// 总数发布测试情况
always {
echo "post always"
}
// success, record the test results and archive the jar file.
success {
junit '**/target/surefire-reports/TEST-*.xml'
}
// failed, record the test results and archive the jar file.
failure {
junit '**/target/surefire-reports/TEST-*.xml'
}
}
}
}
}
3. 遇到的问题
3.1 mvn命令不存在
解决第一步在宿主机上安装maven
由于我是docker部署的jenkins服务(会自带jdk),
首先在宿主机上安装maven
配置环境变量 vim /etc/profile
生效:source /ect/profile
查看: mvn -v
出现如下图所示标识安装成功
如果出现下图所示:permission denied
只要输入此命令即可:
chmod a+x /usr/local/maven/bin/mvn
第二步在jenkins配置环境变量
路径-》系统管理-》系统配置-》全局属性
maven_home 的地址就是宿主机上安装的地址。可通过mvn -v查看
3.2 无创建其他目录的权限
这个是因为我项目的日志指定了创建目录是/home/log,
而jenkins是在容器中部署的,只挂在了 /var/jenkins_home目录下。因此没有权限创建日志导致系统启动报错。
如图:
解决办法:logback-spring.xml 配置文件中关闭指定log_home属性即可
或者修改地址为 /var/home, 两者取其一即可。
<property name="LOG_HOME" value="/var/jenkins_home/log/application_log"/>
<!--<property name="LOG_HOME" value="/var/jenkins_home/log/application_log"/>-->
更多关于logback-spring,log_home存储位置问题点击此处查看
3.3 服务器链接mysql数据库报错
这是因为服务器验证错误多次后报错,
两种解决办法:
1.是到mysql服务器,进入mysql 输入命令mysqladmin flush-hosts即可
2.是设置max_connect_errors大小,可
show variables like '%max_connect_errors%';
set global max_connect_errors = 1000;
3.4 如果看不到测试报告首先的看下你的pom文件
在pom文件中添加maven-surefire-plugin依赖。
<build>
<finalName>zeus</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.3.RELEASE</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<executions>
<execution>
<phase>test</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
本地运行mvn test能看到测试报告生成
更多推荐