Skip to content

Commit 8f9da44

Browse files
committed
- Integration of jacoco for test coverage and exclude the files that should not be a part of test cases.
1 parent ec964a4 commit 8f9da44

3 files changed

Lines changed: 98 additions & 1 deletion

File tree

.github/workflows/weather-app-ci-cd.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ jobs:
5454
with:
5555
name: unit-test-reports
5656
path: app/build/reports/tests/
57+
58+
# -------------------------------
59+
# Upload Jacoco Test Reports
60+
# -------------------------------
61+
- name: Upload Jacoco Test Reports
62+
if: always()
63+
uses: actions/upload-artifact@v4
64+
with:
65+
name: jacoco-test-reports
66+
path: app/build/reports/jacoco/jacocoTestReport/

app/build.gradle.kts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ plugins {
66
alias(libs.plugins.kotlin.compose)
77
alias(libs.plugins.dagger.hilt)
88
alias(libs.plugins.kotlin.kapt)
9+
alias(libs.plugins.jacoco)
910
}
11+
1012
val localProperties = Properties()
1113
val localPropertiesFile = rootProject.file("local.properties")
1214

1315
if (localPropertiesFile.exists()) {
1416
localProperties.load(localPropertiesFile.inputStream())
1517
}
1618

19+
jacoco {
20+
toolVersion = "0.8.11"
21+
}
22+
1723
android {
1824
namespace = "com.asad.weatherapp"
1925
compileSdk = 36
@@ -41,6 +47,9 @@ android {
4147
}
4248

4349
buildTypes {
50+
debug {
51+
enableUnitTestCoverage = true
52+
}
4453
release {
4554
isMinifyEnabled = false
4655
proguardFiles(
@@ -102,4 +111,81 @@ dependencies {
102111
testImplementation(libs.kotlinx.coroutines.test)
103112
testImplementation(libs.mockk)
104113
testImplementation(libs.turbine)
105-
}
114+
}
115+
116+
tasks.register<JacocoReport>("jacocoTestReport") {
117+
dependsOn("testDebugUnitTest")
118+
119+
reports {
120+
xml.required.set(true)
121+
html.required.set(true)
122+
}
123+
124+
val fileFilter = listOf(
125+
// Android generated
126+
"**/R.class",
127+
"**/R$*.class",
128+
"**/BuildConfig.*",
129+
"**/Manifest*.*",
130+
131+
// Tests
132+
"**/*Test*.*",
133+
134+
// Dependency Injection / Hilt
135+
"**/di/**",
136+
"**/*Hilt*.*",
137+
"**/*_Factory.*",
138+
"**/*_MembersInjector.*",
139+
140+
// UI Packages to exclude
141+
"**/ui/theme/**",
142+
"**/ui/components/**",
143+
"**/ui/model/**",
144+
"**/ui/forecast/previews/**",
145+
"**/ui/forecast/WeatherForecastScreenKt.*",
146+
"**/*Lambda*.*",
147+
148+
// Data Packages to exclude
149+
"**/data/repository/**",
150+
"**/data/remote/**",
151+
"**/data/handler/**",
152+
153+
// Activities / Fragments (optional, if needed)
154+
"**/*Activity*.*",
155+
"**/*Fragment*.*",
156+
"**/*$*.*",
157+
158+
// Exclude main app classes
159+
"**/WeatherApp.class"
160+
)
161+
162+
val javaClasses = fileTree(
163+
mapOf(
164+
"dir" to "$buildDir/intermediates/javac/debug/classes",
165+
"excludes" to fileFilter
166+
)
167+
)
168+
169+
val kotlinClasses = fileTree(
170+
mapOf(
171+
"dir" to "$buildDir/tmp/kotlin-classes/debug",
172+
"excludes" to fileFilter
173+
)
174+
)
175+
176+
classDirectories.setFrom(files(javaClasses, kotlinClasses))
177+
178+
sourceDirectories.setFrom(
179+
files(
180+
"src/main/java",
181+
"src/main/kotlin"
182+
)
183+
)
184+
185+
executionData.setFrom(
186+
fileTree(buildDir).include(
187+
"outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec"
188+
)
189+
)
190+
}
191+

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
5050
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
5151
dagger-hilt = { id = "com.google.dagger.hilt.android", version.ref = "hiltVersion" }
5252
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt" }
53+
jacoco = { id = "jacoco" }
5354

0 commit comments

Comments
 (0)