-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a new instrumentation library (#155)
* Deprecate public API in “api” module No further development will take place here, as the testing model for Android moves to a unified approach through ActivityScenario. There’s going to be a new artifact addressing this new API. * Introduce new “core” library, alongside a JUnit 5 implementation of ActivityScenario This is a JUnit 5 Extension which delegates to the ActivityScenario API - pretty straight-forward. Furthermore, add some (Espresso) tests to verify the behavior * Update CircleCI config to include the new module
- Loading branch information
Marcel Schnelle
authored
Mar 18, 2019
1 parent
1d17eb7
commit 3226b13
Showing
17 changed files
with
441 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import org.gradle.api.tasks.testing.logging.TestExceptionFormat | ||
import org.gradle.api.tasks.testing.logging.TestLogEvent | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
buildscript { | ||
repositories { | ||
google() | ||
jcenter() | ||
maven("https://oss.sonatype.org/content/repositories/snapshots") | ||
} | ||
|
||
dependencies { | ||
val latest = Artifacts.Plugin.latestStableVersion | ||
classpath("de.mannodermaus.gradle.plugins:android-junit5:$latest") | ||
} | ||
} | ||
|
||
plugins { | ||
id("com.android.library") | ||
kotlin("android") | ||
} | ||
|
||
apply { | ||
plugin("de.mannodermaus.android-junit5") | ||
} | ||
|
||
android { | ||
compileSdkVersion(Android.compileSdkVersion) | ||
|
||
dexOptions { | ||
javaMaxHeapSize = Android.javaMaxHeapSize | ||
} | ||
|
||
defaultConfig { | ||
minSdkVersion(Android.testCoreMinSdkVersion) | ||
targetSdkVersion(Android.targetSdkVersion) | ||
multiDexEnabled = true | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
testInstrumentationRunnerArgument("runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder") | ||
} | ||
|
||
sourceSets { | ||
getByName("main").java.srcDir("src/main/kotlin") | ||
getByName("test").java.srcDir("src/test/kotlin") | ||
getByName("androidTest").java.srcDir("src/androidTest/kotlin") | ||
} | ||
|
||
compileOptions { | ||
setSourceCompatibility(JavaVersion.VERSION_1_8) | ||
setTargetCompatibility(JavaVersion.VERSION_1_8) | ||
} | ||
|
||
lintOptions { | ||
// JUnit 4 refers to java.lang.management APIs, which are absent on Android. | ||
warning("InvalidPackage") | ||
} | ||
|
||
packagingOptions { | ||
exclude("META-INF/LICENSE.md") | ||
exclude("META-INF/LICENSE-notice.md") | ||
} | ||
|
||
testOptions { | ||
unitTests.apply { | ||
isReturnDefaultValues = true | ||
} | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions.jvmTarget = "1.8" | ||
} | ||
|
||
tasks.withType<Test> { | ||
failFast = true | ||
testLogging { | ||
events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED) | ||
exceptionFormat = TestExceptionFormat.FULL | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(Libs.junit_jupiter_api) | ||
implementation(Libs.kotlin_stdlib) | ||
implementation(Libs.androidx_test_core) | ||
|
||
// This is required by the "instrumentation-runner" companion library, | ||
// since it can't provide any JUnit 5 runtime libraries itself | ||
// due to fear of prematurely incrementing the minSdkVersion requirement. | ||
runtimeOnly(Libs.junit_platform_runner) | ||
|
||
androidTestImplementation(Libs.junit_jupiter_api) | ||
androidTestImplementation(Libs.espresso_core) | ||
|
||
androidTestRuntimeOnly(project(":runner")) | ||
androidTestRuntimeOnly(Libs.junit_jupiter_engine) | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
// Deployment Setup | ||
// | ||
// Releases are pushed to jcenter via Bintray, while snapshots are pushed to Sonatype OSS. | ||
// This section defines the necessary tasks to push new releases and snapshots using Gradle tasks. | ||
// ------------------------------------------------------------------------------------------------ | ||
|
||
val deployConfig by extra<Deployed> { Artifacts.Instrumentation.Core } | ||
apply(from = "$rootDir/gradle/deployment.gradle") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.mannodermaus.junit5"> | ||
<application> | ||
<activity android:name=".TestActivity"/> | ||
</application> | ||
</manifest> |
31 changes: 31 additions & 0 deletions
31
...umentation/core/src/androidTest/java/de/mannodermaus/junit5/JavaInstrumentationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package de.mannodermaus.junit5; | ||
|
||
import androidx.test.core.app.ActivityScenario; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import static androidx.test.espresso.Espresso.onView; | ||
import static androidx.test.espresso.assertion.ViewAssertions.matches; | ||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withText; | ||
|
||
class JavaInstrumentationTests { | ||
|
||
@RegisterExtension | ||
final ActivityScenarioExtension<TestActivity> scenarioExtension = ActivityScenarioExtension.launch(TestActivity.class); | ||
|
||
@Test | ||
void testUsingGetScenario() { | ||
ActivityScenario<TestActivity> scenario = scenarioExtension.getScenario(); | ||
onView(withText("TestActivity")).check(matches(isDisplayed())); | ||
scenario.onActivity(it -> it.changeText("New Text")); | ||
onView(withText("New Text")).check(matches(isDisplayed())); | ||
} | ||
|
||
@Test | ||
void testUsingMethodParameter(ActivityScenario<TestActivity> scenario) { | ||
onView(withText("TestActivity")).check(matches(isDisplayed())); | ||
scenario.onActivity(it -> it.changeText("New Text")); | ||
onView(withText("New Text")).check(matches(isDisplayed())); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...umentation/core/src/androidTest/java/de/mannodermaus/junit5/KotlinInstrumentationTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package de.mannodermaus.junit5 | ||
|
||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.assertion.ViewAssertions.matches | ||
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed | ||
import androidx.test.espresso.matcher.ViewMatchers.withText | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
|
||
class KotlinInstrumentationTests { | ||
|
||
@JvmField | ||
@RegisterExtension | ||
val scenarioExtension = ActivityScenarioExtension.launch<TestActivity>() | ||
|
||
@Test | ||
fun testUsingGetScenario() { | ||
val scenario = scenarioExtension.scenario | ||
onView(withText("TestActivity")).check(matches(isDisplayed())) | ||
scenario.onActivity { it.changeText("New Text") } | ||
onView(withText("New Text")).check(matches(isDisplayed())) | ||
} | ||
|
||
@Test | ||
fun testUsingMethodParameter(scenario: ActivityScenario<TestActivity>) { | ||
onView(withText("TestActivity")).check(matches(isDisplayed())) | ||
scenario.onActivity { it.changeText("New Text") } | ||
onView(withText("New Text")).check(matches(isDisplayed())) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
instrumentation/core/src/androidTest/java/de/mannodermaus/junit5/TestActivities.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.mannodermaus.junit5 | ||
|
||
import android.app.Activity | ||
import android.os.Bundle | ||
import android.widget.TextView | ||
import de.mannodermaus.junit5.test.R | ||
|
||
class TestActivity : Activity() { | ||
|
||
private val textView by lazy { findViewById<TextView>(R.id.textView) } | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_test) | ||
} | ||
|
||
fun changeText(label: String) { | ||
textView.text = label | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
instrumentation/core/src/androidTest/res/layout/activity_test.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="#28c"> | ||
|
||
<TextView | ||
android:id="@+id/textView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="TestActivity" | ||
android:layout_gravity="center"/> | ||
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest package="de.mannodermaus.junit5"/> |
Oops, something went wrong.