-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoverage.gradle
More file actions
125 lines (108 loc) · 4.65 KB
/
coverage.gradle
File metadata and controls
125 lines (108 loc) · 4.65 KB
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
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.7.201606060606"
}
// change the buildType and productFlavor fields here if the project uses different build variant for testing
def buildType = "debug"
def productFlavor = ""
def buildVariant = "${productFlavor.capitalize()}${buildType.capitalize()}"
def buildVariantDirectory = "${productFlavor}/${buildType}"
def coverageSourceDirs = files(['src/main/java'])
def coverageClassDirs = fileTree(
dir: "${buildDir}/intermediates/classes/${buildVariantDirectory}",
excludes: ['**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'com/android/**/*.class',
'**/*_ViewBind*' // ButterKnife auto generated classes
]
)
def unitTestCoverageData = "${buildDir}/jacoco/test${buildVariant}UnitTest.exec"
def instrumentationTestCoverageData = "${buildDir}/spoon-output/${buildVariantDirectory}/coverage/merged-coverage.ec"
// 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
def startTaskNames = gradle.startParameter.taskNames
def isRunningInstrumentationCoverage = startTaskNames.contains('instrumentationTestCoverage') || startTaskNames.contains('testCoverage')
// Run both unit and instrumentation tests and merge the result of both tests into a single report
task testCoverage(type: JacocoReport, dependsOn: ["test${buildVariant}UnitTest", "spoon${buildVariant}AndroidTest"]) {
sourceDirectories = coverageSourceDirs
classDirectories = coverageClassDirs
executionData = files([unitTestCoverageData, instrumentationTestCoverageData])
doLast {
println "You can view the coverage report at:"
println "${buildDir}/reports/jacoco/testCoverage/html/index.html"
}
reports {
xml.enabled true
xml.destination "${buildDir}/reports/jacoco/testCoverage/jacoco.xml"
}
}
// Run and generate the report for the unit tests. The test report only includes the coverage data for
// the presenter and utils classes since we generally don't unit test any Android related classes
task unitTestCoverage(type: JacocoReport, dependsOn: "test${buildVariant}UnitTest") {
sourceDirectories = coverageSourceDirs
classDirectories = fileTree(
dir: "${buildDir}/intermediates/classes/${buildVariantDirectory}",
excludes: [*coverageClassDirs.excludes.asList(),
'**/*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'
]
)
executionData = files("${buildDir}/jacoco/test${buildVariant}UnitTest.exec")
doLast {
println "You can view the coverage report at:"
println "${buildDir}/reports/jacoco/unitTestCoverage/html/index.html"
}
reports {
xml.enabled true
xml.destination "${buildDir}/reports/jacoco/unitTestCoverage/jacoco.xml"
}
}
// Run and generate the report for the instrumentation test
task instrumentationTestCoverage(type: JacocoReport, dependsOn: "spoon${buildVariant}AndroidTest") {
sourceDirectories = coverageSourceDirs
classDirectories = coverageClassDirs
executionData = files(instrumentationTestCoverageData)
doLast {
println "You can view the coverage report at:"
println "${buildDir}/reports/jacoco/instrumentationTestCoverage/html/index.html"
}
reports {
xml.enabled true
xml.destination "${buildDir}/reports/jacoco/instrumentationTestCoverage/jacoco.xml"
}
}
android {
buildTypes {
debug {
testCoverageEnabled(isRunningInstrumentationCoverage)
}
}
testOptions {
unitTests.all {
jacoco {
// needed so that Robolectric tests are included in the results
includeNoLocationClasses = true
}
}
}
}
spoon {
codeCoverage = isRunningInstrumentationCoverage
}