Skip to content

Commit

Permalink
Implement android test (#1141)
Browse files Browse the repository at this point in the history
Executes AndroidTest through a Github action that runs an emulator and runs all android tests.

This commit only implements a very basic hello world test, but we can escalate it from here.
  • Loading branch information
LeoColman committed Jan 4, 2020
1 parent b9f34bf commit 0d556b6
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/android-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "Android Test"

on:
push:

jobs:
test:
runs-on: macOS-latest
steps:
- name: checkout
uses: actions/checkout@v2

- name: run tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 28
script: "./gradlew kotest-tests-android:connectedCheck kotest-tests-android:testDebugUnitTest"
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ buildscript {

dependencies {
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokkaVersion"
classpath 'com.android.tools.build:gradle:3.5.0'
// classpath "io.kotest:kotest-gradle-plugin:1.1.1-LOCAL"
}

repositories {
mavenCentral()
google()
}
}

plugins {
Expand All @@ -43,6 +49,7 @@ allprojects {
repositories {
mavenCentral()
jcenter()
google()
}

sourceCompatibility = 1.8
Expand Down
64 changes: 64 additions & 0 deletions kotest-tests-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import org.jetbrains.kotlin.config.KotlinCompilerVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions

plugins {
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
}

android {
compileSdkVersion(28)

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

lintOptions {
tasks.lint.get().enabled = false
}

kotlinOptions {
val opts = this as KotlinJvmOptions
opts.jvmTarget = "1.8"
}

defaultConfig {
applicationId = "io.kotlintest.androidtests"
minSdkVersion(21)
targetSdkVersion(28)

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

testOptions {
unitTests.isIncludeAndroidResources = true
unitTests.all(closureOf<Test> {
useJUnitPlatform()
testLogging.showStackTraces = true
} as groovy.lang.Closure<Test>)
}

packagingOptions {
exclude("META-INF/LICENSE.md")
exclude("META-INF/LICENSE-notice.md")
}
}

dependencies {
// Kotlin
implementation(kotlin("stdlib-jdk8", KotlinCompilerVersion.VERSION))
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.61")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0")

// Android
implementation("androidx.core:core-ktx:1.2.0-rc01")

// AndroidTest
androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0")
androidTestImplementation("io.kotlintest:kotlintest-runner-junit4:3.4.2") { exclude(module = "objenesis") }
androidTestImplementation("androidx.test:core:1.2.0")
androidTestImplementation("androidx.test:core-ktx:1.2.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.kotest.androidtest

import androidx.test.core.app.launchActivity
import io.kotlintest.shouldBe
import io.kotlintest.specs.StringSpec
import kotlinx.android.synthetic.main.activity_main.textview

class MainActivityTest : StringSpec() {

init {
"Hello World test" {
val scenario = launchActivity<MainActivity>()

scenario.onActivity {
it.textview.text shouldBe "Kotest!"
}
}
}
}
12 changes: 12 additions & 0 deletions kotest-tests-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.kotest.androidtest">

<application>
<activity android:name="io.kotest.androidtest.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.kotest.androidtest

import android.app.Activity
import android.os.Bundle

class MainActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
14 changes: 14 additions & 0 deletions kotest-tests-android/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.kotest.androidtest.MainActivity">

<TextView
android:id="@+id/textview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Kotest!"/>

</LinearLayout>
1 change: 1 addition & 0 deletions kotest-tests-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<resources></resources>
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ include("kotest-tests:kotest-tests-timeout")
//include("kotest-tests:kotest-tests-junit4-assertions")
include("kotest-tests:kotest-tests-junit5")
include("kotest-tests:kotest-tests-parallelism")
include("kotest-tests-android")

0 comments on commit 0d556b6

Please sign in to comment.