HelloWood

Java 将单元测试覆盖率上传到 Codecov

2018-01-01

将单元测试覆盖率上传到 Codecov

通过使用 Jacoco 生成单元测试覆盖率报告,并将该报告上传到 Codecov

配置 Jacoco

  • 配置 build.gradle 文件(以 SpringBoot 应用为例)
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
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("gradle.plugin.com.gorylenko.gradle-git-properties:gradle-git-properties:1.4.17")
}
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

// 使用 Jacoco 插件
apply plugin: 'jacoco'


group = 'cn.com.hellowood'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

war {
baseName = 'Security'
version = ''
}

dependencies {
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-devtools")
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.webjars:jquery:3.2.1')
compile('org.webjars:bootstrap:4.0.0-beta.2')
compile('org.webjars:font-awesome:4.7.0')
compile('org.webjars:bootstrap-glyphicons:bdd2cbfba0')
runtime('mysql:mysql-connector-java')
runtime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}

// 添加 task 用于生成 Jacoco 测试结果
task codeCoverageReport(type: JacocoReport) {

// 需指定生成的类文件位置和源文件位置
classDirectories = files('build/classes')
sourceDirectories = files('src/main/java')

executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

subprojects.each {
sourceSets it.sourceSets.main
}

// 生成的报告类型包括 xml/html/csv
reports {
xml.enabled true
xml.destination "${buildDir}/reports/jacoco/report.xml"
html.enabled false
csv.enabled false
}
}

codeCoverageReport.dependsOn {
subprojects*.test
}

check.dependsOn codeCoverageReport

生成测试报告

1
2
gradle check
gradle codeCoverageReport

此时在 build/reports/jacoco 下生成 Jacoco 的测试报告

上传测试结果

  • 通过命令直接上传(TOKEN 在 Codecov 项目中可以找到)
1
bash <(curl -s https://codecov.io/bash) -t YOUR_PROJECT_TOKEN
  • 通过 Travis CI 上传(在 Travis 配置文件中添加以下内容)
1
2
3
4
5
6
script:
- ./gradlew check
- ./gradlew codeCoverageReport
after_success:
- codecov
- bash <(curl -s https://codecov.io/bash) -t YOUR_PROJECT_TOKEN
  • 通过 Circle CI 上传(在 Circle 配置文件中添加以下内容)
1
2
3
4
5
6
jobs:
build:
steps:
- run: gradle check
- run: gradle codeCoverageReport
- run: bash <(curl -s https://codecov.io/bash) -t 92b3ad6b-92f7-49bb-94e7-ea233b860e47

需要注意的是只有 xml 格式的测试报告才会被上传

分析结果