-
Notifications
You must be signed in to change notification settings - Fork 1
/
CodeCoverage.kt
209 lines (188 loc) · 8.92 KB
/
CodeCoverage.kt
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.BaseVariant
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import org.gradle.testing.jacoco.plugins.JacocoPluginExtension
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension
import org.gradle.testing.jacoco.tasks.JacocoCoverageVerification
import org.gradle.testing.jacoco.tasks.JacocoReport
object CodeCoverage {
const val JACOCO_VERSION = "0.8.4"
val generatedClasses = setOf(
"**/R.class",
"**/R$*.class",
"**/BuildConfig.*",
"**/Manifest*.*",
"com/android/**/*.class",
"**/*_ViewBind*" // ButterKnife auto generated classes
)
val androidClasses = setOf(
"**/*Activity.class",
"**/*Activity$*.class",
"**/*Fragment.class",
"**/*Fragment$*.class",
"**/*Service.class",
"**/*Service$*.class",
"**/*Application.class",
"**/*Application$*.class",
"**/*Dialog.class",
"**/*Dialog$*.class",
"**/*Adapter.class",
"**/*Adapter$*.class",
"**/*ViewHolder.class",
"**/*ViewHolder$*.class",
"**/*Animation.class",
"**/*Animation$*.class"
)
/***
* Creates jacoco tasks and configures various extensions to allow for test coverage
* The generated tasks are in the format of
* `{coverageReport|coverageMinimum}{$buildVariant}{Unit|Ui|Combined}Test`
* The `coverageReport` tasks simply generates the coverage report, whereas `coverageMinimum` also checks to make
* sure the coverage percentages is above the minimum threshold and fails the build if it isn't
* `Unit` test only runs the JVM unitTests, `Ui` test only runs the Android Instrumentation tests (currently through
* Spoon), and `Combined` test runs both.
* ex `./gradlew coverageReportDebugUnitTest` generates unit test coverage report for the debug build
*
* @param project gradle project, usually `this` when called from the module build.gradle file
* @param minimumCombinedCoverage failure threshold when running coverageMinimum${variant}CombinedTest
* @param minimumUnitCoverage failure threshold when running coverageMinimum${variant}UniTest
* @param minimumUiCoverage failure threshold when running coverageMinimum${variant}UiTest
* @param commonExcludedClasses classes that are excluded from all test coverages. Typically for generated classes
* @param unitTestExcludedClasses classes that are excluded only from unit tests. Typically for Android classes
* @param uiTestExcludedClasses classes that are excluded only from UI tests
*/
fun configure(
project: Project,
minimumCombinedCoverage: Double = 0.7,
minimumUnitCoverage: Double = 0.5,
minimumUiCoverage: Double = 0.6,
commonExcludedClasses: Set<String> = generatedClasses,
unitTestExcludedClasses: Set<String> = androidClasses,
uiTestExcludedClasses: Set<String> = emptySet()
) {
with(project) {
plugins.apply("jacoco")
configure<JacocoPluginExtension> {
toolVersion = JACOCO_VERSION
}
tasks.withType(Test::class) {
// needed so that Robolectric tests are included in the results
extensions.configure<JacocoTaskExtension> {
isIncludeNoLocationClasses = true
}
}
configure<BaseExtension> {
buildTypes {
getByName("debug") {
// testCoverageEnabled messes up debugging/etc, so we want it disabled most of times.
// unit test coverage reports seems to work fine even with the flag disabled, but the
// instrumentation tests need this flag otherwise the coverage data may be incomplete
isTestCoverageEnabled = isRunningUiTestCoverage
}
}
}
}
project.afterEvaluate {
allVariants(project)?.forEach {
configureVariant(
it,
project,
minimumCombinedCoverage,
minimumUnitCoverage,
minimumUiCoverage,
commonExcludedClasses,
unitTestExcludedClasses,
uiTestExcludedClasses)
}
}
}
private fun configureVariant(variant: BaseVariant,
project: Project,
minimumCombinedCoverage: Double,
minimumUnitCoverage: Double,
minimumUiCoverage: Double,
commonExcludedClasses: Set<String>,
unitTestExcludedClasses: Set<String>,
uiTestExcludedClasses: Set<String>
) {
val variantNameCapitalized = variant.name.capitalize()
val variantNameDecapitalized = variant.name.decapitalize()
val buildDir = project.buildDir
fun createJacocoTasks(testType: String,
testTasks: Array<String>,
minimumCoverage: Double,
excludedClasses: Set<String>,
ecData: ConfigurableFileCollection) {
val reportTaskName = "coverageReport${variantNameCapitalized}${testType}Test"
val coverageSourceDirs = project.files("src/main/kotlin")
val coverageClassDirs = project.fileTree("$buildDir/tmp/kotlin-classes/$variantNameDecapitalized") {
setExcludes(excludedClasses)
}
project.tasks.register<JacocoReport>(reportTaskName) {
dependsOn(testTasks)
group = "verification"
sourceDirectories.setFrom(coverageSourceDirs)
classDirectories.setFrom(coverageClassDirs)
executionData.setFrom(ecData)
val reportLocationPrefix = "$buildDir/reports/jacoco/$reportTaskName"
reports {
xml.isEnabled = true
xml.destination = project.file("$reportLocationPrefix/jacoco.xml")
}
doLast {
println("You can view the coverage report at:")
println("$reportLocationPrefix/html/index.html")
}
}
project.tasks.register<JacocoCoverageVerification>("coverageMinimum$variantNameCapitalized${testType}Test") {
dependsOn(reportTaskName)
group = "verification"
sourceDirectories.setFrom(coverageSourceDirs)
classDirectories.setFrom(coverageClassDirs)
executionData.setFrom(ecData)
violationRules {
rule {
limit {
minimum = minimumCoverage.toBigDecimal()
}
}
}
}
}
val unitTestExecutionData = "$buildDir/jacoco/test${variantNameCapitalized}UnitTest.exec"
createJacocoTasks(
testType = "Unit",
testTasks = arrayOf("test${variantNameCapitalized}UnitTest"),
minimumCoverage = minimumUnitCoverage,
excludedClasses = commonExcludedClasses + unitTestExcludedClasses,
ecData = project.files(unitTestExecutionData)
)
val uiTestExecutionData = "$buildDir/spoon-output/$variantNameDecapitalized/coverage/merged-coverage.ec"
createJacocoTasks(
testType = "Ui",
testTasks = arrayOf("spoon${variantNameCapitalized}AndroidTest"),
minimumCoverage = minimumUiCoverage,
excludedClasses = commonExcludedClasses + uiTestExcludedClasses,
ecData = project.files(uiTestExecutionData)
)
createJacocoTasks(
testType = "Combined",
testTasks = arrayOf("test${variantNameCapitalized}UnitTest", "spoon${variantNameCapitalized}AndroidTest"),
minimumCoverage = minimumCombinedCoverage,
excludedClasses = commonExcludedClasses,
ecData = project.files(unitTestExecutionData, uiTestExecutionData)
)
}
}
val Project.isRunningUiTestCoverage: Boolean
get() {
return gradle.startParameter.taskNames.any {
(it.startsWith("coverageReport") || it.startsWith("coverageMinimum")) &&
(it.endsWith("UiTest") || it.endsWith("CombinedTest"))
}
}