Skip to content

Commit 562aed6

Browse files
authored
Add unit test (#30)
* add stuff * add workflows * add workflows * add `ViewIntent_Initial returns failure` * jacoco * wip * fix wip failed * Fix tests * Fix tests * wip * format * format * format * CLEAN * CLEAN * enable all * enable all
1 parent ef243cf commit 562aed6

File tree

23 files changed

+452
-98
lines changed

23 files changed

+452
-98
lines changed

.github/workflows/build.yml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ jobs:
1010
build:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v2
14-
15-
- name: Set up JDK
16-
uses: actions/setup-java@v2
17-
with:
18-
distribution: 'zulu'
19-
java-version: '11'
20-
21-
- name: Make gradlew executable
22-
run: chmod +x ./gradlew
23-
24-
- name: Spotless check
25-
run: ./gradlew spotlessCheck
26-
27-
- name: Build debug APK
28-
run: ./gradlew assembleDebug --warning-mode all --stacktrace
29-
30-
- name: Upload APK
31-
uses: actions/upload-artifact@v2
32-
with:
33-
name: app-debug
34-
path: app/build/outputs/apk/debug/app-debug.apk
13+
- uses: actions/checkout@v2
14+
15+
- name: Set up JDKd
16+
uses: actions/setup-java@v2
17+
with:
18+
distribution: 'zulu'
19+
java-version: '11'
20+
21+
- name: Make gradlew executable
22+
run: chmod +x ./gradlew
23+
24+
- name: Spotless check
25+
run: ./gradlew spotlessCheck
26+
27+
- name: Build debug APK
28+
run: ./gradlew assembleDebug --warning-mode all --stacktrace
29+
30+
- name: Upload APK
31+
uses: actions/upload-artifact@v2
32+
with:
33+
name: app-debug
34+
path: app/build/outputs/apk/debug/app-debug.apk

.github/workflows/unit-test.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Unit Tests CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: Set up JDK
16+
uses: actions/setup-java@v2
17+
with:
18+
distribution: 'zulu'
19+
java-version: '11'
20+
21+
- name: Make gradlew executable
22+
run: chmod +x ./gradlew
23+
24+
- name: Run Android Debug Unit Test
25+
run: ./gradlew jacocoTestReportDebug --warning-mode all --stacktrace
26+
27+
- name: Run Java/Kotlin Unit Test
28+
run: ./gradlew jacocoTestReport --warning-mode all --stacktrace
29+
30+
- name: Upload Test Report
31+
uses: codecov/codecov-action@v2.1.0

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
androidApplication
33
kotlinAndroid
4+
jacoco
45
}
56

67
android {
@@ -26,6 +27,10 @@ android {
2627
"proguard-rules.pro"
2728
)
2829
}
30+
31+
// getByName("debug") {
32+
// isTestCoverageEnabled = true
33+
// }
2934
}
3035

3136
compileOptions {
@@ -34,6 +39,11 @@ android {
3439
}
3540
kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }
3641
buildFeatures { viewBinding = true }
42+
43+
testOptions {
44+
unitTests.isIncludeAndroidResources = true
45+
unitTests.isReturnDefaultValues = true
46+
}
3747
}
3848

3949
dependencies {
@@ -61,4 +71,6 @@ dependencies {
6171
testImplementation(deps.test.junit)
6272
androidTestImplementation(deps.test.androidxJunit)
6373
androidTestImplementation(deps.test.androidXSspresso)
74+
75+
addUnitTest()
6476
}

app/src/test/java/com/hoc/flowmvi/ExampleUnitTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.hoc.flowmvi
22

3-
import org.junit.Assert.assertEquals
4-
import org.junit.Test
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
55

66
/**
77
* Example local unit test, which will execute on the development machine (host).

build.gradle.kts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ buildscript {
77
google()
88
mavenCentral()
99
gradlePluginPortal()
10+
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
1011
}
1112
dependencies {
1213
classpath("com.android.tools.build:gradle:7.0.2")
1314
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
1415
classpath("com.diffplug.spotless:spotless-plugin-gradle:5.16.0")
1516
classpath("dev.ahmedmourad.nocopy:nocopy-gradle-plugin:1.4.0")
17+
classpath("org.jacoco:org.jacoco.core:0.8.7")
18+
classpath("com.vanniktech:gradle-android-junit-jacoco-plugin:0.17.0-SNAPSHOT")
1619
}
1720
}
1821

1922
subprojects {
2023
apply(plugin = "com.diffplug.spotless")
24+
apply(plugin = "com.vanniktech.android.junit.jacoco")
2125

2226
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
2327
kotlin {
@@ -59,6 +63,26 @@ subprojects {
5963
endWithNewline()
6064
}
6165
}
66+
67+
configure<com.vanniktech.android.junit.jacoco.JunitJacocoExtension> {
68+
jacocoVersion = "0.8.7"
69+
includeNoLocationClasses = true
70+
includeInstrumentationCoverageInMergedReport = true
71+
csv.isEnabled = false
72+
xml.isEnabled = true
73+
html.isEnabled = true
74+
}
75+
76+
afterEvaluate {
77+
tasks.withType<Test> {
78+
extensions
79+
.getByType<JacocoTaskExtension>()
80+
.run {
81+
isIncludeNoLocationClasses = true
82+
excludes = listOf("jdk.internal.*")
83+
}
84+
}
85+
}
6286
}
6387

6488
allprojects {

buildSrc/src/main/kotlin/deps.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@file:Suppress("unused", "ClassName", "SpellCheckingInspection")
22

33
import org.gradle.api.artifacts.dsl.DependencyHandler
4+
import org.gradle.kotlin.dsl.DependencyHandlerScope
45
import org.gradle.kotlin.dsl.project
56
import org.gradle.plugin.use.PluginDependenciesSpec
67
import org.gradle.plugin.use.PluginDependencySpec
@@ -11,8 +12,8 @@ const val kotlinVersion = "1.5.21"
1112
object appConfig {
1213
const val applicationId = "com.hoc.flowmvi"
1314

14-
const val compileSdkVersion = 30
15-
const val buildToolsVersion = "30.0.3"
15+
const val compileSdkVersion = 31
16+
const val buildToolsVersion = "31.0.0"
1617

1718
const val minSdkVersion = 21
1819
const val targetSdkVersion = 30
@@ -51,6 +52,7 @@ object deps {
5152

5253
const val core = "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version"
5354
const val android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version"
55+
const val test = "org.jetbrains.kotlinx:kotlinx-coroutines-test:$version"
5456
}
5557

5658
object koin {
@@ -65,9 +67,12 @@ object deps {
6567
const val flowExt = "io.github.hoc081098:FlowExt:0.0.7-SNAPSHOT"
6668

6769
object test {
68-
const val junit = "junit:junit:4.13"
70+
const val junit = "junit:junit:4.13.2"
6971
const val androidxJunit = "androidx.test.ext:junit:1.1.2"
7072
const val androidXSspresso = "androidx.test.espresso:espresso-core:3.3.0"
73+
74+
const val mockk = "io.mockk:mockk:1.12.0"
75+
const val kotlinJUnit = "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
7176
}
7277
}
7378

@@ -85,3 +90,10 @@ inline val DependencyHandler.data get() = project(":data")
8590
inline val DependencyHandler.featureMain get() = project(":feature-main")
8691
inline val DependencyHandler.featureAdd get() = project(":feature-add")
8792
inline val DependencyHandler.featureSearch get() = project(":feature-search")
93+
94+
fun DependencyHandler.addUnitTest() {
95+
add("testImplementation", deps.test.junit)
96+
add("testImplementation", deps.test.mockk)
97+
add("testImplementation", deps.test.kotlinJUnit)
98+
add("testImplementation", deps.coroutines.test)
99+
}

core/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ android {
2929
targetCompatibility = JavaVersion.VERSION_1_8
3030
}
3131
kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }
32+
33+
testOptions {
34+
unitTests.isIncludeAndroidResources = true
35+
unitTests.isReturnDefaultValues = true
36+
}
3237
}
3338

3439
dependencies {
@@ -42,4 +47,6 @@ dependencies {
4247

4348
implementation(deps.lifecycle.commonJava8)
4449
implementation(deps.lifecycle.runtimeKtx)
50+
51+
addUnitTest()
4552
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
package com.hoc.flowmvi.core
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class ExampleUnitTest {
7+
@Test
8+
fun addition_isCorrect() {
9+
assertEquals(4, 2 + 2)
10+
}
11+
}

coverage.gradle.kts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply(plugin = "jacoco")
2+
3+
tasks {
4+
val debugCoverageReport by registering(JacocoReport::class)
5+
debugCoverageReport {
6+
dependsOn("testDebugUnitTest")
7+
8+
reports {
9+
xml.run {
10+
required.value(true)
11+
outputLocation.set(file("$buildDir/reports/jacoco/test/jacocoTestReport.xml"))
12+
}
13+
html.required.value(true)
14+
}
15+
16+
val kotlinClasses = fileTree("$buildDir/tmp/kotlin-classes/debug")
17+
val coverageSourceDirs = arrayOf(
18+
"src/main/java",
19+
"src/debug/java"
20+
)
21+
val executionDataDirs = fileTree("$buildDir") {
22+
setIncludes(
23+
listOf(
24+
"jacoco/testDebugUnitTest.exec",
25+
"outputs/code_coverage/debugAndroidTest/connected/*.ec",
26+
"outputs/code-coverage/connected/*coverage.ec"
27+
)
28+
)
29+
}
30+
31+
classDirectories.setFrom(files(kotlinClasses))
32+
sourceDirectories.setFrom(coverageSourceDirs)
33+
additionalSourceDirs.setFrom(files(coverageSourceDirs))
34+
executionData.setFrom(executionDataDirs)
35+
}
36+
}

data/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ android {
2929
targetCompatibility = JavaVersion.VERSION_1_8
3030
}
3131
kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }
32+
33+
testOptions {
34+
unitTests.isIncludeAndroidResources = true
35+
unitTests.isReturnDefaultValues = true
36+
}
3237
}
3338

3439
dependencies {
@@ -43,4 +48,6 @@ dependencies {
4348
implementation(deps.squareup.loggingInterceptor)
4449

4550
implementation(deps.koin.core)
51+
52+
addUnitTest()
4653
}

data/src/test/java/com/hoc/flowmvi/data/ExampleUnitTest.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package com.hoc.flowmvi.data
22

3-
import org.junit.Test
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
45

5-
/**
6-
* Example local unit test, which will execute on the development machine (host).
7-
*
8-
* See [testing documentation](http://d.android.com/tools/testing).
9-
*/
106
class ExampleUnitTest {
117
@Test
128
fun addition_isCorrect() {

domain/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ plugins {
55
dependencies {
66
implementation(deps.coroutines.core)
77
implementation(deps.koin.core)
8+
9+
addUnitTest()
810
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.hoc.flowmvi.domain
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class ExampleTest {
7+
@Test
8+
fun example() {
9+
assertEquals(1, 1)
10+
}
11+
}

feature-add/build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ android {
2929
targetCompatibility = JavaVersion.VERSION_1_8
3030
}
3131
kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }
32-
3332
buildFeatures { viewBinding = true }
33+
34+
testOptions {
35+
unitTests.isIncludeAndroidResources = true
36+
unitTests.isReturnDefaultValues = true
37+
}
3438
}
3539

3640
dependencies {
@@ -51,4 +55,6 @@ dependencies {
5155

5256
implementation(deps.viewBindingDelegate)
5357
implementation(deps.flowExt)
58+
59+
addUnitTest()
5460
}

0 commit comments

Comments
 (0)