Skip to content

Add unit test #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '11'
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Spotless check
run: ./gradlew spotlessCheck
- name: Build debug APK
run: ./gradlew assembleDebug --warning-mode all --stacktrace
- name: Upload APK
uses: actions/upload-artifact@v2
with:
name: app-debug
path: app/build/outputs/apk/debug/app-debug.apk
- uses: actions/checkout@v2

- name: Set up JDKd
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '11'

- name: Make gradlew executable
run: chmod +x ./gradlew

- name: Spotless check
run: ./gradlew spotlessCheck

- name: Build debug APK
run: ./gradlew assembleDebug --warning-mode all --stacktrace

- name: Upload APK
uses: actions/upload-artifact@v2
with:
name: app-debug
path: app/build/outputs/apk/debug/app-debug.apk
31 changes: 31 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Unit Tests CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up JDK
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '11'

- name: Make gradlew executable
run: chmod +x ./gradlew

- name: Run Android Debug Unit Test
run: ./gradlew jacocoTestReportDebug --warning-mode all --stacktrace

- name: Run Java/Kotlin Unit Test
run: ./gradlew jacocoTestReport --warning-mode all --stacktrace

- name: Upload Test Report
uses: codecov/codecov-action@v2.1.0
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
androidApplication
kotlinAndroid
jacoco
}

android {
Expand All @@ -26,6 +27,10 @@ android {
"proguard-rules.pro"
)
}

// getByName("debug") {
// isTestCoverageEnabled = true
// }
}

compileOptions {
Expand All @@ -34,6 +39,11 @@ android {
}
kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }
buildFeatures { viewBinding = true }

testOptions {
unitTests.isIncludeAndroidResources = true
unitTests.isReturnDefaultValues = true
}
}

dependencies {
Expand Down Expand Up @@ -61,4 +71,6 @@ dependencies {
testImplementation(deps.test.junit)
androidTestImplementation(deps.test.androidxJunit)
androidTestImplementation(deps.test.androidXSspresso)

addUnitTest()
}
4 changes: 2 additions & 2 deletions app/src/test/java/com/hoc/flowmvi/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hoc.flowmvi

import org.junit.Assert.assertEquals
import org.junit.Test
import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Example local unit test, which will execute on the development machine (host).
Expand Down
24 changes: 24 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ buildscript {
google()
mavenCentral()
gradlePluginPortal()
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
}
dependencies {
classpath("com.android.tools.build:gradle:7.0.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
classpath("com.diffplug.spotless:spotless-plugin-gradle:5.15.1")
classpath("dev.ahmedmourad.nocopy:nocopy-gradle-plugin:1.4.0")
classpath("org.jacoco:org.jacoco.core:0.8.7")
classpath("com.vanniktech:gradle-android-junit-jacoco-plugin:0.17.0-SNAPSHOT")
}
}

subprojects {
apply(plugin = "com.diffplug.spotless")
apply(plugin = "com.vanniktech.android.junit.jacoco")

configure<com.diffplug.gradle.spotless.SpotlessExtension> {
kotlin {
Expand Down Expand Up @@ -59,6 +63,26 @@ subprojects {
endWithNewline()
}
}

configure<com.vanniktech.android.junit.jacoco.JunitJacocoExtension> {
jacocoVersion = "0.8.7"
includeNoLocationClasses = true
includeInstrumentationCoverageInMergedReport = true
csv.isEnabled = false
xml.isEnabled = true
html.isEnabled = true
}

afterEvaluate {
tasks.withType<Test> {
extensions
.getByType<JacocoTaskExtension>()
.run {
isIncludeNoLocationClasses = true
excludes = listOf("jdk.internal.*")
}
}
}
}

allprojects {
Expand Down
18 changes: 15 additions & 3 deletions buildSrc/src/main/kotlin/deps.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@file:Suppress("unused", "ClassName", "SpellCheckingInspection")

import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.kotlin.dsl.DependencyHandlerScope
import org.gradle.kotlin.dsl.project
import org.gradle.plugin.use.PluginDependenciesSpec
import org.gradle.plugin.use.PluginDependencySpec
Expand All @@ -11,8 +12,8 @@ const val kotlinVersion = "1.5.21"
object appConfig {
const val applicationId = "com.hoc.flowmvi"

const val compileSdkVersion = 30
const val buildToolsVersion = "30.0.3"
const val compileSdkVersion = 31
const val buildToolsVersion = "31.0.0"

const val minSdkVersion = 21
const val targetSdkVersion = 30
Expand Down Expand Up @@ -51,6 +52,7 @@ object deps {

const val core = "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version"
const val android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version"
const val test = "org.jetbrains.kotlinx:kotlinx-coroutines-test:$version"
}

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

object test {
const val junit = "junit:junit:4.13"
const val junit = "junit:junit:4.13.2"
const val androidxJunit = "androidx.test.ext:junit:1.1.2"
const val androidXSspresso = "androidx.test.espresso:espresso-core:3.3.0"

const val mockk = "io.mockk:mockk:1.12.0"
const val kotlinJUnit = "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
}
}

Expand All @@ -85,3 +90,10 @@ inline val DependencyHandler.data get() = project(":data")
inline val DependencyHandler.featureMain get() = project(":feature-main")
inline val DependencyHandler.featureAdd get() = project(":feature-add")
inline val DependencyHandler.featureSearch get() = project(":feature-search")

fun DependencyHandler.addUnitTest() {
add("testImplementation", deps.test.junit)
add("testImplementation", deps.test.mockk)
add("testImplementation", deps.test.kotlinJUnit)
add("testImplementation", deps.coroutines.test)
}
7 changes: 7 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ android {
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }

testOptions {
unitTests.isIncludeAndroidResources = true
unitTests.isReturnDefaultValues = true
}
}

dependencies {
Expand All @@ -42,4 +47,6 @@ dependencies {

implementation(deps.lifecycle.commonJava8)
implementation(deps.lifecycle.runtimeKtx)

addUnitTest()
}
10 changes: 10 additions & 0 deletions core/src/test/java/com/hoc/flowmvi/core/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
package com.hoc.flowmvi.core

import kotlin.test.Test
import kotlin.test.assertEquals

class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
36 changes: 36 additions & 0 deletions coverage.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apply(plugin = "jacoco")

tasks {
val debugCoverageReport by registering(JacocoReport::class)
debugCoverageReport {
dependsOn("testDebugUnitTest")

reports {
xml.run {
required.value(true)
outputLocation.set(file("$buildDir/reports/jacoco/test/jacocoTestReport.xml"))
}
html.required.value(true)
}

val kotlinClasses = fileTree("$buildDir/tmp/kotlin-classes/debug")
val coverageSourceDirs = arrayOf(
"src/main/java",
"src/debug/java"
)
val executionDataDirs = fileTree("$buildDir") {
setIncludes(
listOf(
"jacoco/testDebugUnitTest.exec",
"outputs/code_coverage/debugAndroidTest/connected/*.ec",
"outputs/code-coverage/connected/*coverage.ec"
)
)
}

classDirectories.setFrom(files(kotlinClasses))
sourceDirectories.setFrom(coverageSourceDirs)
additionalSourceDirs.setFrom(files(coverageSourceDirs))
executionData.setFrom(executionDataDirs)
}
}
7 changes: 7 additions & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ android {
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }

testOptions {
unitTests.isIncludeAndroidResources = true
unitTests.isReturnDefaultValues = true
}
}

dependencies {
Expand All @@ -43,4 +48,6 @@ dependencies {
implementation(deps.squareup.loggingInterceptor)

implementation(deps.koin.core)

addUnitTest()
}
8 changes: 2 additions & 6 deletions data/src/test/java/com/hoc/flowmvi/data/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.hoc.flowmvi.data

import org.junit.Test
import kotlin.test.Test
import kotlin.test.assertEquals

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
Expand Down
2 changes: 2 additions & 0 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ plugins {
dependencies {
implementation(deps.coroutines.core)
implementation(deps.koin.core)

addUnitTest()
}
11 changes: 11 additions & 0 deletions domain/src/test/java/com/hoc/flowmvi/domain/ExampleTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hoc.flowmvi.domain

import kotlin.test.Test
import kotlin.test.assertEquals

class ExampleTest {
@Test
fun example() {
assertEquals(1, 1)
}
}
8 changes: 7 additions & 1 deletion feature-add/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ android {
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }

buildFeatures { viewBinding = true }

testOptions {
unitTests.isIncludeAndroidResources = true
unitTests.isReturnDefaultValues = true
}
}

dependencies {
Expand All @@ -51,4 +55,6 @@ dependencies {

implementation(deps.viewBindingDelegate)
implementation(deps.flowExt)

addUnitTest()
}
Loading