Gitlab-CI自动化集成测试

yml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# stages:
# - build
# - test
# - deploy
# job1:
# stage: build
# script:
# - 'pwd && /usr/local/maven/apache-maven-3.2.5/bin/mvn clean package'
# tags:
# - hgr

job2:
stage: test
script:
# - 'D:/apache-maven-3.3.9/bin/mvn test'
# - 'D:/apache-maven-3.3.9/bin/mvn jacoco:report'
# - 'echo \"$(cat target/site/jacoco/testcoverage.txt)% covered\"'
- 'D:/apache-maven-3.3.9/bin/mvn clean test && type E:\GitLab-Runner\builds\jut3BULm\0\caojunjie\test\target\site\jacoco\index.html'
tags:
- Test_ci

# job2:
# stage: deploy
# script:
# - 'pwd && /usr/local/maven/apache-maven-3.2.5/bin/mvn deploy'
# tags:
# - hgr

需要配置的 pom文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.jacoco</groupId>
<artifactId>testJacoco</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>JaCoCo Examples</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<!-- Sonar -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<!-- The destination file for the code coverage report has to be set to the same value
in the parent pom and in each module pom. Then JaCoCo will add up information in
the same report, so that, it will give the cross-module code coverage. -->
<sonar.jacoco.itReportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.itReportPath>
<sonar.language>java</sonar.language>
</properties>

<dependencies>
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.3.201107060350</version>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>com.*</includes>
</configuration>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

需要在gitlab ci中配置:

测试覆盖率解析为:

1
Total.*?([0-9]{1,3})%

复制Pipeline status/Coverage report中的html连接到readme.xml中。

直接就会项目首页中显示出来覆盖率和pieline的状态和覆盖率

热评文章