Skip to content

Commit 83b6eb7

Browse files
committed
Added lib tests, included jvm tests for android
1 parent 9ffe9e3 commit 83b6eb7

File tree

5 files changed

+75
-21
lines changed

5 files changed

+75
-21
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ jobs:
3535
uses: yutailang0119/action-android-lint@v3.1.0
3636
with:
3737
report-path: app/build/reports/lint/report.xml
38-
- name: Jvm Unit Tests with Coverage Report
39-
run: ./gradlew jacocoTestReportDebug
38+
- name: app Jvm Unit Tests with Coverage Report
39+
run: ./gradlew jacocoTestReportUnitTests
40+
- name: lib Jvm Unit Tests with Coverage Report
41+
run: ./gradlew lib:test
4042
- name: Clean Packages
4143
run: bash ./scripts/clean-packages.sh
4244
- name: On-Device Instrumented Tests
@@ -60,7 +62,10 @@ jobs:
6062
with:
6163
token: ${{ secrets.CODECOV_TOKEN }}
6264
flags: unittests
63-
files: ./app/build/reports/jacoco/jacocoTestReportDebug/jacocoTestReportDebug.xml
65+
files: |
66+
./app/build/reports/coverage/androidTest/debug/connected/report.xml
67+
./app/build/reports/jacoco/jacocoTestReportDebugUnitTest/jacocoTestReportDebugUnitTest.xml
68+
./lib/build/reports/jacoco/test/jacocoTestReport.xml
6469
6570
- name: Upload Instrumented Tests Report to CodeCov
6671
# https://github.com/codecov/codecov-action

app/build.gradle

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ android {
3131

3232
// 3) Java 8 is required
3333
compileOptions {
34-
sourceCompatibility JavaVersion.VERSION_11
35-
targetCompatibility JavaVersion.VERSION_11
34+
sourceCompatibility JavaVersion.VERSION_17
35+
targetCompatibility JavaVersion.VERSION_17
3636
}
3737
packagingOptions {
3838
jniLibs {
@@ -54,6 +54,7 @@ android {
5454
shrinkResources false
5555
minifyEnabled false
5656
testCoverageEnabled true
57+
enableUnitTestCoverage true
5758
}
5859
}
5960

@@ -104,6 +105,9 @@ android {
104105
}
105106
}
106107
}
108+
buildFeatures {
109+
buildConfig true
110+
}
107111
}
108112

109113
dependencies {
@@ -151,10 +155,8 @@ jacoco {
151155
toolVersion = '0.8.11'
152156
}
153157

154-
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
155-
kotlinOptions {
156-
jvmTarget = "11"
157-
}
158+
kotlin {
159+
jvmToolchain(17)
158160
}
159161

160162
// https://github.com/ReactiveCircus/app-versioning
@@ -168,5 +170,29 @@ appVersioning {
168170
}
169171
}
170172

173+
// for some rason running the jacocoTestReport generates a report for the release unit tests that
174+
// works with jacoco, however it doesn't add it to the
175+
tasks.create(name: "jacocoTestReportUnitTests", type: JacocoReport, dependsOn: "jacocoTestReport") {
176+
group = "Reporting" // existing group containing tasks for generating linting reports etc.
177+
description = "Generate JVM unit test coverage reports using Jacoco."
178+
179+
reports {
180+
// human readable (written into './build/reports/jacoco/unitTestCoverageReport/html')
181+
html.required = true
182+
// CI-readable (written into './build/reports/jacoco/unitTestCoverageReport/unitTestCoverageReport.xml')
183+
xml.required = true
184+
}
185+
186+
// Execution data generated when running the tests against classes instrumented by the JaCoCo agent.
187+
// This is enabled with 'enableUnitTestCoverage' in the 'debug' build type.
188+
executionData.from = fileTree(dir: "${project.buildDir}/jacoco/", includes: ["/testReleaseUnitTest.exec"])
189+
190+
// Compiled Kotlin class files are written into build-variant-specific subdirectories of 'build/tmp/kotlin-classes'.
191+
classDirectories.from = "${project.buildDir}/tmp/kotlin-classes/release"
192+
193+
// To produce an accurate report, the bytecode is mapped back to the original source code.
194+
sourceDirectories.from = "${project.projectDir}/src/main/java"
195+
}
196+
171197
apply from: file('secrets.gradle')
172198
apply from: file('publish.gradle')

gradle.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ android.useAndroidX=true
1919
android.enableJetifier=true
2020
# Kotlin code style for this project: "official" or "obsolete":
2121
kotlin.code.style=official
22-
android.defaults.buildfeatures.buildconfig=true
2322
android.nonTransitiveRClass=false
2423
android.nonFinalResIds=false

lib/build.gradle

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
31
plugins {
42
id 'java-library'
53
id 'org.jetbrains.kotlin.jvm'
4+
id 'jacoco'
65
}
76

8-
compileJava {
9-
sourceCompatibility = '11'
10-
targetCompatibility = '11'
7+
java {
8+
sourceCompatibility = JavaVersion.VERSION_17
9+
targetCompatibility = JavaVersion.VERSION_17
1110
}
1211

13-
java {
14-
sourceCompatibility = JavaVersion.VERSION_11
15-
targetCompatibility = JavaVersion.VERSION_11
12+
kotlin {
13+
jvmToolchain(17)
14+
}
15+
16+
jacoco {
17+
toolVersion = "0.8.11"
1618
}
1719

18-
tasks.withType(KotlinCompile).configureEach {
19-
kotlinOptions {
20-
jvmTarget = "11"
20+
jacocoTestReport {
21+
reports {
22+
xml.required = true
23+
html.required = true
2124
}
2225
}
2326

27+
test {
28+
useJUnitPlatform()
29+
finalizedBy jacocoTestReport
30+
}
31+
2432
dependencies {
2533
implementation "org.slf4j:slf4j-api:${slf4j_version}"
34+
35+
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5:$kotlin_version"
36+
testImplementation "org.junit.jupiter:junit-jupiter-engine:$jupiter_version"
37+
testImplementation "org.slf4j:slf4j-simple:${slf4j_version}"
38+
testRuntimeOnly "org.junit.platform:junit-platform-launcher:1.10.1"
2639
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.lib
2+
3+
import org.junit.jupiter.api.Test
4+
5+
class MyClassTest {
6+
@Test
7+
fun testMyClass() {
8+
val myClass = MyClass()
9+
myClass.display()
10+
}
11+
}

0 commit comments

Comments
 (0)