Share
Nitrogen in your (test) pyramid 2
Unit, integration, UI tests are part of the famous test pyramid, different tools were used in order to full fill it, but still today there are lot of issues about how to execute it in a proper manner and how to sync all environments (local, ci, etc) in order to satisfy all the testing criteria, and having a good reports.
From IO 2018 with the announcement of project Nitrogen, Google’d like to “nitrogenize” our tests creating a single entry point and solve all issues
we had in the past. With October release of Robolectric 4.0 and androidx.test 1.0.0, both testing environments are converging on a set of common test APIs. Robolectric now supports the AndroidJUnit4 test runner, ActivityTestRule, and Espresso for interacting with UI components.
Let’s see togheter how to organise different levels of our ‘test pyramid’, with a focus to the last news/releases in order ot be ready for the Nitrogen release.
Enrico Bruno Del Zotto
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
190 slides
-
Unit tests Low level of the pyramid Coverage small set
of our code (a unit) Validate our app behaviour one class a time. -
Integration tests Medium level of the pyramid Validate either interactions
between levels of the stack within a module, or interactions between related modules. Presence of Android Context -
End to end tests Higher level of the pyramid Validate
the user journey in the app and are the slowest and most expensive kind of test. You have to run normally in a device. -
Failing UI test Failing unit test Passing unit test Passing
UI test Refactor -
Failing UI test Failing unit test Passing unit test Passing
UI test Refactor Refactor -
Test for all these frameworks use the same generic format
: “Given, when, then” -
@RunWith(AndroidJUnit4::class) class MainUiTest { @get:Rule val activityRule = ActivityTestRule(MainActivity::class.java) @Before
fun setup() { activityRule.launchActivity(Intent().putExtra("fragment", MainFragment::class.java)) } @Test fun testAssertHelloText() { onView(withId(R.id.subtitle)).check(matches(withText("Hello World!"))) } ActivityRule -
@RunWith(AndroidJUnit4::class) class MainUiTest { @get:Rule val activityRule = ActivityTestRule(MainActivity::class.java) @Before
fun setup() { activityRule.launchActivity(Intent().putExtra("fragment", MainFragment::class.java)) } @Test fun testAssertHelloText() { onView(withId(R.id.subtitle)).check(matches(withText("Hello World!"))) } ActivityRule -
@RunWith(AndroidJUnit4::class) class MainUiTest { @get:Rule val activityRule = ActivityTestRule(MainActivity::class.java) @Before
fun setup() { activityRule.launchActivity(Intent().putExtra("fragment", MainFragment::class.java)) } @Test fun testAssertHelloText() { onView(withId(R.id.subtitle)).check(matches(withText("Hello World!"))) } ActivityRule -
@RunWith(AndroidJUnit4::class) class MainUiTest { @get:Rule val activityRule = ActivityTestRule(MainActivity::class.java) @Before
fun setup() { activityRule.launchActivity(Intent().putExtra("fragment", MainFragment::class.java)) } @Test fun testAssertHelloText() { onView(withId(R.id.subtitle)).check(matches(withText("Hello World!"))) } ActivityRule -
AndroidX test library Core ApplicationProvider : Provides ability to retrieve
the current application Context in tests -
AndroidX test library Core ApplicationProvider : Provides ability to retrieve
the current application Context in tests ActivityScenario -
AndroidX test library Core ApplicationProvider : Provides ability to retrieve
the current application Context in tests ActivityScenario FragmentScenario -
@Test fun should_clear_locations_when_activity_in_STARTED_state() { //Given val activityScenario = ActivityScenario.launch(LocationActivity::class.java) //When
activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then assertThat(activityScenario.getState()).isEqualTo(State.STARTED) activityScenario.onActivity { assertThat(it.noLocationSelected()).isEqualTo(true) } activityScenario.close() } Activity Scenario -
@Test fun should_clear_locations_when_activity_in_STARTED_state() { //Given val activityScenario = ActivityScenario.launch(LocationActivity::class.java) //When
activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then assertThat(activityScenario.getState()).isEqualTo(State.STARTED) activityScenario.onActivity { assertThat(it.noLocationSelected()).isEqualTo(true) } activityScenario.close() } Activity Scenario -
@Test fun should_clear_locations_when_activity_in_STARTED_state() { //Given val activityScenario = ActivityScenario.launch(LocationActivity::class.java) //When
activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then assertThat(activityScenario.getState()).isEqualTo(State.STARTED) activityScenario.onActivity { assertThat(it.noLocationSelected()).isEqualTo(true) } activityScenario.close() } Activity Scenario -
@Test fun should_clear_locations_when_activity_in_STARTED_state() { //Given val activityScenario = ActivityScenario.launch(LocationActivity::class.java) //When
activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then assertThat(activityScenario.getState()).isEqualTo(State.STARTED) activityScenario.onActivity { assertThat(it.noLocationSelected()).isEqualTo(true) } activityScenario.close() } Activity Scenario -
@Test fun should_clear_locations_when_activity_in_STARTED_state() { //Given val activityScenario = ActivityScenario.launch(LocationActivity::class.java) //When
activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then assertThat(activityScenario.getState()).isEqualTo(State.STARTED) activityScenario.onActivity { assertThat(it.noLocationSelected()).isEqualTo(true) } activityScenario.close() } Activity Scenario -
LaunchFragmentInContainer @Test fun locationFragmentIsDisplayed() { launchFragmentInContainer<LocationFragment>() onView(withId(R.id.location_fragment)) .check(matches(isDisplayed())) } val
factory = LocationFragmentFactory() val args = Bundle().apply { putString(FRAGMENT_ARG, FRAGMENT_ARG_KEY) } launchFragmentInContainer<LocationFragment>(args, factory) -
LaunchFragmentInContainer @Test fun locationFragmentIsDisplayed() { launchFragmentInContainer<LocationFragment>() onView(withId(R.id.location_fragment)) .check(matches(isDisplayed())) } val
factory = LocationFragmentFactory() val args = Bundle().apply { putString(FRAGMENT_ARG, FRAGMENT_ARG_KEY) } launchFragmentInContainer<LocationFragment>(args, factory) -
LaunchFragmentInContainer val scenario = launchFragmentInContainer<LocationFragment>() scenario.onFragment { it.checkLocation() } [...]
scenario.recreate() scenario.moveToState(Lifecycle.State.RESUMED) -
LaunchFragmentInContainer val scenario = launchFragmentInContainer<LocationFragment>() scenario.onFragment { it.checkLocation() } [...]
scenario.recreate() scenario.moveToState(Lifecycle.State.RESUMED) -
LaunchFragmentInContainer val scenario = launchFragmentInContainer<LocationFragment>() scenario.onFragment { it.checkLocation() } [...]
scenario.recreate() scenario.moveToState(Lifecycle.State.RESUMED) -
LaunchFragmentInContainer val scenario = launchFragmentInContainer<LocationFragment>() scenario.onFragment { it.checkLocation() } [...]
scenario.recreate() scenario.moveToState(Lifecycle.State.RESUMED) -
@Test fun should_start_selectLocationActivity_when_location_is_not_selected() { //Given Intents.init() val activityScenario = ActivityScenario.launch(LocationActivity::class.java)
//When activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then intended(hasComponent(SelectLocationActivity::class.java.name)) Intents.release() activityScenario.close() } espresso.intent -
@Test fun should_start_selectLocationActivity_when_location_is_not_selected() { //Given Intents.init() val activityScenario = ActivityScenario.launch(LocationActivity::class.java)
//When activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then intended(hasComponent(SelectLocationActivity::class.java.name)) Intents.release() activityScenario.close() } espresso.intent -
@Test fun should_start_selectLocationActivity_when_location_is_not_selected() { //Given Intents.init() val activityScenario = ActivityScenario.launch(LocationActivity::class.java)
//When activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then intended(hasComponent(SelectLocationActivity::class.java.name)) Intents.release() activityScenario.close() } espresso.intent -
@Test fun should_start_selectLocationActivity_when_location_is_not_selected() { //Given Intents.init() val activityScenario = ActivityScenario.launch(LocationActivity::class.java)
//When activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then intended(hasComponent(SelectLocationActivity::class.java.name)) Intents.release() activityScenario.close() } espresso.intent -
@Test fun should_start_selectLocationActivity_when_location_is_not_selected() { //Given Intents.init() val activityScenario = ActivityScenario.launch(LocationActivity::class.java)
//When activityScenario.onActivity { it.clearLocation() } activityScenario.moveToState(Lifecycle.State.STARTED) //Then intended(hasComponent(SelectLocationActivity::class.java.name)) Intents.release() activityScenario.close() } espresso.intent -
Fluent assertions, it’s not AssertJ ! AssertEquals(View.visibilty, View.Visible) -> assertThat(view).isVisible()
AndroidX test library Truth -
AssertJ Puzzlers assertThat(primaryColors).containsAll(RED, YELLOW, BLUE); containsAtLeast assertThat(uniqueIdGenerator.next()).isNotSameAs(uniqueIdGenerator.next()); isNotSameInstanceAs assertThat(defaults).has(new Condition<>(x
-> x instanceof String, "a string")); not supporting condition Style-Assertions -
Readable assertThat(middleName).isAbsent() assertFalse(middleName.isPresent()) assertThat(googleColors).contains(PINK); <[BLUE, RED, YELLOW, BLUE, GREEN, RED]>
should have contained <PINK> assertTrue(googleColors.contains(PINK)); AssertionFailedError AssertJ Truth -
Readable assertThat(middleName).isAbsent() assertFalse(middleName.isPresent()) assertFalse(googleColors.contains(PINK)); assertFalse(googleColors.contains(BLACK)); ... assertThat(googleColors).contains(PINK); <[BLUE, RED, YELLOW,
BLUE, GREEN, RED]> should have contained <PINK> assertTrue(googleColors.contains(PINK)); AssertionFailedError AssertJ Truth -
Readable assertThat(middleName).isAbsent() assertFalse(middleName.isPresent()) assertFalse(googleColors.contains(PINK)); assertFalse(googleColors.contains(BLACK)); ... assertThat(googleColors) .containsNoneOf(PINK, BLACK, WHITE,
ORANGE); assertThat(googleColors).contains(PINK); <[BLUE, RED, YELLOW, BLUE, GREEN, RED]> should have contained <PINK> assertTrue(googleColors.contains(PINK)); AssertionFailedError AssertJ Truth -
Subject class CarSubject(metadata: FailureMetadata, subject: Car) : Subject<CarSubject, Car>(metadata, subject)
{ companion object { private val FACTORY = Factory<CarSubject, Car> { metadata, actual -> CarSubject(metadata, actual) } fun car(): Factory<CarSubject, Car> { return FACTORY } } fun isElectrical(): CarSubject { if (!actual().isElectrical) { failWithActual(simpleFact("expected to be electrical")) } return this } fun hasChargingLevel(chargingLevel: Int): CarSubject { if (actual().chargingLevel != chargingLevel) { failWithActual(simpleFact("expected to be $chargingLevel")) } return this } -
Subject class CarSubject(metadata: FailureMetadata, subject: Car) : Subject<CarSubject, Car>(metadata, subject)
{ companion object { private val FACTORY = Factory<CarSubject, Car> { metadata, actual -> CarSubject(metadata, actual) } fun car(): Factory<CarSubject, Car> { return FACTORY } } fun isElectrical(): CarSubject { if (!actual().isElectrical) { failWithActual(simpleFact("expected to be electrical")) } return this } fun hasChargingLevel(chargingLevel: Int): CarSubject { if (actual().chargingLevel != chargingLevel) { failWithActual(simpleFact("expected to be $chargingLevel")) } return this } -
Subject class CarSubject(metadata: FailureMetadata, subject: Car) : Subject<CarSubject, Car>(metadata, subject)
{ companion object { private val FACTORY = Factory<CarSubject, Car> { metadata, actual -> CarSubject(metadata, actual) } fun car(): Factory<CarSubject, Car> { return FACTORY } } fun isElectrical(): CarSubject { if (!actual().isElectrical) { failWithActual(simpleFact("expected to be electrical")) } return this } fun hasChargingLevel(chargingLevel: Int): CarSubject { if (actual().chargingLevel != chargingLevel) { failWithActual(simpleFact("expected to be $chargingLevel")) } return this } -
Subject class CarSubject(metadata: FailureMetadata, subject: Car) : Subject<CarSubject, Car>(metadata, subject)
{ companion object { private val FACTORY = Factory<CarSubject, Car> { metadata, actual -> CarSubject(metadata, actual) } fun car(): Factory<CarSubject, Car> { return FACTORY } } fun isElectrical(): CarSubject { if (!actual().isElectrical) { failWithActual(simpleFact("expected to be electrical")) } return this } fun hasChargingLevel(chargingLevel: Int): CarSubject { if (actual().chargingLevel != chargingLevel) { failWithActual(simpleFact("expected to be $chargingLevel")) } return this } -
Subject @Test fun `When buy a new car, then it
should be electrical`() { val car = Car() assert_().about(CarSubject.car()).that(car).isElectrical() } -
Subject @Test fun `When buy a new car, then it
should be electrical`() { val car = Car() assert_().about(CarSubject.car()).that(car).isElectrical() } @Test fun `When buy a new car, then it should be electrical, charged, and fast`() { val car = Car() assert_().about(CarSubject.car()).that(car).isElectrical().hasChargingLevel(100).isFast() } -
Subject @Test fun `When buy a new car, then it
should be electrical`() { val car = Car() assert_().about(CarSubject.car()).that(car).isElectrical() } @Test fun `When buy a new car, then it should be electrical, charged, and fast`() { val car = Car() assert_().about(CarSubject.car()).that(car).isElectrical().hasChargingLevel(100).isFast() } -
Subject @Test fun `When buy a new car, then it
should be electrical`() { val car = Car() assert_().about(CarSubject.car()).that(car).isElectrical() } @Test fun `When buy a new car, then it should be electrical, charged, and fast`() { val car = Car() assert_().about(CarSubject.car()).that(car).isElectrical().hasChargingLevel(100).isFast() } -
AndroidX test library 1- JUnit runners & rules 2- Core
ApplicationProvider, Activity/Fragment scenario 3- Espresso yes you know it 4- Truth Fluent assertions, it’s not AssertJ ! Recap -
AndroidX test library Provides common test APIs across test environments
including instrumentation tests. -
AndroidX test library We can now use the new Scenario
API to launch the Activity or Fragment Use Espresso to make some view interactions Use Truth to make view assertions Run instrumented tests on JVM instead on the device using the new AndroidX test runner -
public final class AndroidJUnit4 extends Runner implements Filterable, Sortable {
[...] private static String getRunnerClassName() { String runnerClassName = System.getProperty("android.junit.runner", null); if (runnerClassName == null) { // TODO: remove this logic when nitrogen is hooked up to always pass this property if (System.getProperty("java.runtime.name").toLowerCase().contains("android")) { return "androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner"; } else { return "org.robolectric.RobolectricTestRunner"; } } return runnerClassName; } [...] } -
public final class AndroidJUnit4 extends Runner implements Filterable, Sortable {
[...] private static String getRunnerClassName() { String runnerClassName = System.getProperty("android.junit.runner", null); if (runnerClassName == null) { // TODO: remove this logic when nitrogen is hooked up to always pass this property if (System.getProperty("java.runtime.name").toLowerCase().contains("android")) { return "androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner"; } else { return "org.robolectric.RobolectricTestRunner"; } } return runnerClassName; } [...] } -
public final class AndroidJUnit4 extends Runner implements Filterable, Sortable {
[...] private static String getRunnerClassName() { String runnerClassName = System.getProperty("android.junit.runner", null); if (runnerClassName == null) { // TODO: remove this logic when nitrogen is hooked up to always pass this property if (System.getProperty("java.runtime.name").toLowerCase().contains("android")) { return "androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner"; } else { return "org.robolectric.RobolectricTestRunner"; } } return runnerClassName; } [...] } -
Robolectric 4.x Provides a JVM compliant version of the android.jar
file, it handles inflation of views, resource loading, and lots of other stuff that’s implemented in native C code on Android devices. This enables you to run your Android tests in your continuous integration environment without any additional setup -
testImplementation Libs.junit testImplementation Libs.robolectric testImplementation Libs.Mockito.core testImplementation Libs.AndroidX.Test.archCore testImplementation Libs.AndroidX.Test.espressoCore
testImplementation Libs.AndroidX.Test.espressoContrib testImplementation Libs.AndroidX.Test.espressoIdlingResources testImplementation Libs.AndroidX.Test.junitAssertions testImplementation Libs.AndroidX.Test.core testImplementation Libs.AndroidX.Test.runner testImplementation Libs.AndroidX.Test.rules testImplementation Libs.AndroidX.Test.espressoCore testImplementation Libs.AndroidX.Test.thruth -
testImplementation Libs.junit testImplementation Libs.robolectric testImplementation Libs.Mockito.core testImplementation Libs.AndroidX.Test.archCore testImplementation Libs.AndroidX.Test.espressoCore
testImplementation Libs.AndroidX.Test.espressoContrib testImplementation Libs.AndroidX.Test.espressoIdlingResources testImplementation Libs.AndroidX.Test.junitAssertions testImplementation Libs.AndroidX.Test.core testImplementation Libs.AndroidX.Test.runner testImplementation Libs.AndroidX.Test.rules testImplementation Libs.AndroidX.Test.espressoCore testImplementation Libs.AndroidX.Test.thruth -
testImplementation Libs.junit testImplementation Libs.robolectric testImplementation Libs.Mockito.core testImplementation Libs.AndroidX.Test.archCore testImplementation Libs.AndroidX.Test.espressoCore
testImplementation Libs.AndroidX.Test.espressoContrib testImplementation Libs.AndroidX.Test.espressoIdlingResources testImplementation Libs.AndroidX.Test.junitAssertions testImplementation Libs.AndroidX.Test.core testImplementation Libs.AndroidX.Test.runner testImplementation Libs.AndroidX.Test.rules testImplementation Libs.AndroidX.Test.espressoCore testImplementation Libs.AndroidX.Test.thruth -
testOptions { unitTests { includeAndroidResources = true } unitTests.all {
include '**/*Tests.class' include '**/*Test.class' useJUnit { } ignoreFailures false // use afterTest to listen to the test execution results afterTest { descriptor, result -> println "Executing test for ${descriptor.name} with result: $ {result.resultType}" } systemProperty 'robolectric.logging.enabled', 'true' jvmArgs '-noverify' maxHeapSize = "2g" } } -
testOptions { unitTests { includeAndroidResources = true } unitTests.all {
include '**/*Tests.class' include '**/*Test.class' useJUnit { } ignoreFailures false // use afterTest to listen to the test execution results afterTest { descriptor, result -> println "Executing test for ${descriptor.name} with result: $ {result.resultType}" } systemProperty 'robolectric.logging.enabled', 'true' jvmArgs '-noverify' maxHeapSize = "2g" } } -
testOptions { unitTests { includeAndroidResources = true } unitTests.all {
include '**/*Tests.class' include '**/*Test.class' useJUnit { } ignoreFailures false // use afterTest to listen to the test execution results afterTest { descriptor, result -> println "Executing test for ${descriptor.name} with result: $ {result.resultType}" } systemProperty 'robolectric.logging.enabled', 'true' jvmArgs '-noverify' maxHeapSize = "2g" } } -
class RepositoryListUiTest : BaseInstrumentationTestCase() { @Test fun willDisplayFirstPaginatedListCorrectly() { given.gitHubServer.returnsReposListPageOneSuccessfully()
`when`.user.launchesTheApp() then.user.sees.repositoriesListScreenAssertion.withSuccessfullyLoadedFirstPageItem() } @Test fun willDisplayErrorCorrectly() { given.gitHubServer.returnErrorOnFetchingRepositories() `when`.user.launchesTheApp() then.user.sees.repositoriesListScreenAssertion.withErrorState() } } -
class RepositoryListUiTest : BaseInstrumentationTestCase() { @Test fun willDisplayFirstPaginatedListCorrectly() { given.gitHubServer.returnsReposListPageOneSuccessfully()
`when`.user.launchesTheApp() then.user.sees.repositoriesListScreenAssertion.withSuccessfullyLoadedFirstPageItem() } @Test fun willDisplayErrorCorrectly() { given.gitHubServer.returnErrorOnFetchingRepositories() `when`.user.launchesTheApp() then.user.sees.repositoriesListScreenAssertion.withErrorState() } } -
class RepositoryListUiTest : BaseInstrumentationTestCase() { @Test fun willDisplayFirstPaginatedListCorrectly() { given.gitHubServer.returnsReposListPageOneSuccessfully()
`when`.user.launchesTheApp() then.user.sees.repositoriesListScreenAssertion.withSuccessfullyLoadedFirstPageItem() } @Test fun willDisplayErrorCorrectly() { given.gitHubServer.returnErrorOnFetchingRepositories() `when`.user.launchesTheApp() then.user.sees.repositoriesListScreenAssertion.withErrorState() } } -
class RepositoryListUiTest : BaseInstrumentationTestCase() { @Test fun willDisplayFirstPaginatedListCorrectly() { given.gitHubServer.returnsReposListPageOneSuccessfully()
`when`.user.launchesTheApp() then.user.sees.repositoriesListScreenAssertion.withSuccessfullyLoadedFirstPageItem() } @Test fun willDisplayErrorCorrectly() { given.gitHubServer.returnErrorOnFetchingRepositories() `when`.user.launchesTheApp() then.user.sees.repositoriesListScreenAssertion.withErrorState() } } -
sourceSets { String sharedTestDir = 'src/sharedTest/java' test { debug.assets.srcDirs +=
files("$projectDir/schemas".toString()) debug.assets.srcDirs += files("$projectDir/assets".toString()) debug.assets.srcDirs += files("$projectDir/raw".toString()) java.srcDirs += 'src/androidTest/java' kotlin.srcDirs += 'src/androidTest/java' java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir test.resources.srcDirs += 'src/test/resources' } androidTest { assets.srcDirs += files("$projectDir/schemas".toString()) test.resources.srcDirs += 'src/test/resources' resources.srcDirs += ['src/test/resources'] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir } } -
sourceSets { String sharedTestDir = 'src/sharedTest/java' test { debug.assets.srcDirs +=
files("$projectDir/schemas".toString()) debug.assets.srcDirs += files("$projectDir/assets".toString()) debug.assets.srcDirs += files("$projectDir/raw".toString()) java.srcDirs += 'src/androidTest/java' kotlin.srcDirs += 'src/androidTest/java' java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir test.resources.srcDirs += 'src/test/resources' } androidTest { assets.srcDirs += files("$projectDir/schemas".toString()) test.resources.srcDirs += 'src/test/resources' resources.srcDirs += ['src/test/resources'] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir } } -
sourceSets { String sharedTestDir = 'src/sharedTest/java' test { debug.assets.srcDirs +=
files("$projectDir/schemas".toString()) debug.assets.srcDirs += files("$projectDir/assets".toString()) debug.assets.srcDirs += files("$projectDir/raw".toString()) java.srcDirs += 'src/androidTest/java' kotlin.srcDirs += 'src/androidTest/java' java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir test.resources.srcDirs += 'src/test/resources' } androidTest { assets.srcDirs += files("$projectDir/schemas".toString()) test.resources.srcDirs += 'src/test/resources' resources.srcDirs += ['src/test/resources'] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir } } -
sourceSets { String sharedTestDir = 'src/sharedTest/java' test { debug.assets.srcDirs +=
files("$projectDir/schemas".toString()) debug.assets.srcDirs += files("$projectDir/assets".toString()) debug.assets.srcDirs += files("$projectDir/raw".toString()) java.srcDirs += 'src/androidTest/java' kotlin.srcDirs += 'src/androidTest/java' java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir test.resources.srcDirs += 'src/test/resources' } androidTest { assets.srcDirs += files("$projectDir/schemas".toString()) test.resources.srcDirs += 'src/test/resources' resources.srcDirs += ['src/test/resources'] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir } } -
Using the shared test folder : sourceSets { String sharedTestDir
= 'src/sharedTest/java' test { [...] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir test.resources.srcDirs += 'src/test/resources' } androidTest { [...] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir } } -
Using the shared test folder : sourceSets { String sharedTestDir
= 'src/sharedTest/java' test { [...] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir test.resources.srcDirs += 'src/test/resources' } androidTest { [...] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir } } -
Using the shared test folder : sourceSets { String sharedTestDir
= 'src/sharedTest/java' test { [...] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir test.resources.srcDirs += 'src/test/resources' } androidTest { [...] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir } } -
Using the shared test folder : sourceSets { String sharedTestDir
= 'src/sharedTest/java' test { [...] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir test.resources.srcDirs += 'src/test/resources' } androidTest { [...] java.srcDirs += sharedTestDir kotlin.srcDirs += sharedTestDir } } -
If you have lot of build variants, in which manner
specify which UI test should run into JVM? Mixing everything into one shared folder isn’t a good solution! -
If you have lot of build variants, in which manner
specify which UI test should run into JVM? Mixing everything into one shared folder isn’t a good solution! -
Adding dynamically using a Gradle task could be a good
option : ./gradlew sourceSets | grep src/androidTest'$variantName' -
Adding dynamically using a Gradle task could be a good
option : ./gradlew sourceSets | grep src/androidTest'$variantName' -
sourceSets.all { sourceSet -> Matcher testTaskMatcher = patternOfUnitTestTask.matcher(sourceSet.name) if (testTaskMatcher.find())
{ def variantName = testTaskMatcher.group(1) println "Added androidTest sourceSets to $sourceSet.name (variant name = $ {variantName}" sourceSet.java.srcDirs += "src/androidTest${variantName}/java" sourceSet.kotlin.srcDirs += "src/androidTest${variantName}/java" } } Adding dynamically using a Gradle task could be a good option : -
sourceSets.all { sourceSet -> Matcher testTaskMatcher = patternOfUnitTestTask.matcher(sourceSet.name) if (testTaskMatcher.find())
{ def variantName = testTaskMatcher.group(1) println "Added androidTest sourceSets to $sourceSet.name (variant name = $ {variantName}" sourceSet.java.srcDirs += "src/androidTest${variantName}/java" sourceSet.kotlin.srcDirs += "src/androidTest${variantName}/java" } } Adding dynamically using a Gradle task could be a good option : -
sourceSets.all { sourceSet -> Matcher testTaskMatcher = patternOfUnitTestTask.matcher(sourceSet.name) if (testTaskMatcher.find())
{ def variantName = testTaskMatcher.group(1) println "Added androidTest sourceSets to $sourceSet.name (variant name = $ {variantName}" sourceSet.java.srcDirs += "src/androidTest${variantName}/java" sourceSet.kotlin.srcDirs += "src/androidTest${variantName}/java" } } Adding dynamically using a Gradle task could be a good option : -
sourceSets.all { sourceSet -> Matcher testTaskMatcher = patternOfUnitTestTask.matcher(sourceSet.name) if (testTaskMatcher.find())
{ def variantName = testTaskMatcher.group(1) println "Added androidTest sourceSets to $sourceSet.name (variant name = $ {variantName}" sourceSet.java.srcDirs += "src/androidTest${variantName}/java" sourceSet.kotlin.srcDirs += "src/androidTest${variantName}/java" } } Adding dynamically using a Gradle task could be a good option : -
You can use Junit 4 ‘categories’ to exclude tests which
for some reason has some incompatibilities with JVM: -
You can use Junit 4 ‘categories’ to exclude tests which
for some reason has some incompatibilities with JVM: testOptions { [...] unitTests.all { [...] useJUnit { //With this category we can annotate tests will not run on JVM //due conflicts or other Robolectric Issue, but can run without any issue on Espresso or //normal device. excludeCategories 'com.playground.utils.DoNotRunOnJvm' } [...] } } -
You can use Junit 4 ‘categories’ to exclude tests which
for some reason has some incompatibilities with JVM: testOptions { [...] unitTests.all { [...] useJUnit { //With this category we can annotate tests will not run on JVM //due conflicts or other Robolectric Issue, but can run without any issue on Espresso or //normal device. excludeCategories 'com.playground.utils.DoNotRunOnJvm' } [...] } } -
/** * Marks a test class or test method as
belonging to one or more categories of tests. * The value is an array of arbitrary classes. * * This annotation is only interpreted by the Categories runner (at present). * * For example: * <pre> * public interface FastTests {} * public interface SlowTests {} * * public static class A { * @Test * public void a() { * fail(); * } * * @Category(SlowTests.class) * @Test * public void b() { * } * } * * @Category({SlowTests.class, FastTests.class}) * public static class B { * @Test * public void c() { * * } * } * </pre> * * For more usage, see code example on {@link Categories}. */ @Retention(RetentionPolicy.RUNTIME) @Inherited @ValidateWith(CategoryValidator.class) public @interface Category { Class<?>[] value(); } -
@Category(DoNotRunOnJvm::class) class AirportPickerUiTest : BaseHolidaySearchUiTest() { [...] @Before override fun
setUp() { super.setUp() willOpenHolidaySearchScreenCorrectly() } @Test fun willOpenAirportPickerScreenCorrectly() { `when`.user.apply { onBottomNavigation.clicksSearch() onHolidaySearchActions.clickAirportPicker() } then.user.sees.apply { airportPickerAssertions.withFields() airportPickerAssertions.withSuccessfullyLoadedFlightsFrom() } } [...] } -
@Category(DoNotRunOnJvm::class) class AirportPickerUiTest : BaseHolidaySearchUiTest() { [...] @Before override fun
setUp() { super.setUp() willOpenHolidaySearchScreenCorrectly() } @Test fun willOpenAirportPickerScreenCorrectly() { `when`.user.apply { onBottomNavigation.clicksSearch() onHolidaySearchActions.clickAirportPicker() } then.user.sees.apply { airportPickerAssertions.withFields() airportPickerAssertions.withSuccessfullyLoadedFlightsFrom() } } [...] } -
Create a custom filter to check if our test has
custom @TestFilter(flavors = [FLAVOR_1]) annotation. Based on that and the flavour name it check if we need to run it or no. -
Create a custom filter to check if our test has
custom @TestFilter(flavors = [FLAVOR_1]) annotation. Based on that and the flavour name it check if we need to run it or no. @Test @TestFilter(flavors = [FLAVOR_1]) fun test1() { //... } @Test fun test2() { //... } @Test @TestFilter(flavors = [FLAVOR_2]) fun test3() { //... } -
Create a custom filter to check if our test has
custom @TestFilter(flavors = [FLAVOR_1]) annotation. Based on that and the flavour name it check if we need to run it or no. @Test @TestFilter(flavors = [FLAVOR_1]) fun test1() { //... } @Test fun test2() { //... } @Test @TestFilter(flavors = [FLAVOR_2]) fun test3() { //... } https://medium.com/stepstone-tech/taming-ui-tests-when-using-multiple-gradle-flavors-6190b3692491 -
Create a custom filter to check if our test has
custom @TestFilter(flavors = [FLAVOR_1]) annotation. Based on that and the flavour name it check if we need to run it or no. @Test @TestFilter(flavors = [FLAVOR_1]) fun test1() { //... } @Test fun test2() { //... } @Test @TestFilter(flavors = [FLAVOR_2]) fun test3() { //... } https://medium.com/stepstone-tech/taming-ui-tests-when-using-multiple-gradle-flavors-6190b3692491 -
Create a custom filter to check if our test has
custom @TestFilter(flavors = [FLAVOR_1]) annotation. Based on that and the flavour name it check if we need to run it or no. @Test @TestFilter(flavors = [FLAVOR_1]) fun test1() { //... } @Test fun test2() { //... } @Test @TestFilter(flavors = [FLAVOR_2]) fun test3() { //... } https://medium.com/stepstone-tech/taming-ui-tests-when-using-multiple-gradle-flavors-6190b3692491 -
Create a custom filter to check if our test has
custom @TestFilter(flavors = [FLAVOR_1]) annotation. Based on that and the flavour name it check if we need to run it or no. @Test @TestFilter(flavors = [FLAVOR_1]) fun test1() { //... } @Test fun test2() { //... } @Test @TestFilter(flavors = [FLAVOR_2]) fun test3() { //... } https://medium.com/stepstone-tech/taming-ui-tests-when-using-multiple-gradle-flavors-6190b3692491 -
class FlavorFilter(bundle: Bundle) : ParentFilter() { companion object { private
const val CLASS_BUNDLE_KEY = "class" } private val shouldFilterTests: Boolean init { Timber.i("FlavorFilter bundle: %s", bundle) val listTestsForOrchestrator = java.lang.Boolean.parseBoolean(bundle.getString("listTestsForOrchestrator", "false")) val runsSingleClass = bundle.containsKey(CLASS_BUNDLE_KEY) shouldFilterTests = listTestsForOrchestrator && !runsSingleClass } override fun evaluateTest(description: Description): Boolean { if (!shouldFilterTests) { return true } val classTestFilter = description.testClass.getAnnotation(TestFilter::class.java) val testFilter = description.getAnnotation(TestFilter::class.java) if (testFilter != null) { return evaluateTestWithFilter(testFilter) } else if (classTestFilter != null) { return evaluateTestWithFilter(classTestFilter) } return true } private fun evaluateTestWithFilter(testFilter: TestFilter): Boolean { val filters = testFilter.filters val notFilters = testFilter.notFilters return (filters.contains(BuildConfig.FLAVOR) && !notFilters.contains(BuildConfig.FLAVOR)) } override fun describe(): String { return "Flavor filter" } } -
class FlavorFilter(bundle: Bundle) : ParentFilter() { companion object { private
const val CLASS_BUNDLE_KEY = "class" } private val shouldFilterTests: Boolean init { Timber.i("FlavorFilter bundle: %s", bundle) val listTestsForOrchestrator = java.lang.Boolean.parseBoolean(bundle.getString("listTestsForOrchestrator", "false")) val runsSingleClass = bundle.containsKey(CLASS_BUNDLE_KEY) shouldFilterTests = listTestsForOrchestrator && !runsSingleClass } override fun evaluateTest(description: Description): Boolean { if (!shouldFilterTests) { return true } val classTestFilter = description.testClass.getAnnotation(TestFilter::class.java) val testFilter = description.getAnnotation(TestFilter::class.java) if (testFilter != null) { return evaluateTestWithFilter(testFilter) } else if (classTestFilter != null) { return evaluateTestWithFilter(classTestFilter) } return true } private fun evaluateTestWithFilter(testFilter: TestFilter): Boolean { val filters = testFilter.filters val notFilters = testFilter.notFilters return (filters.contains(BuildConfig.FLAVOR) && !notFilters.contains(BuildConfig.FLAVOR)) } override fun describe(): String { return "Flavor filter" } } -
class FlavorFilter(bundle: Bundle) : ParentFilter() { companion object { private
const val CLASS_BUNDLE_KEY = "class" } private val shouldFilterTests: Boolean init { Timber.i("FlavorFilter bundle: %s", bundle) val listTestsForOrchestrator = java.lang.Boolean.parseBoolean(bundle.getString("listTestsForOrchestrator", "false")) val runsSingleClass = bundle.containsKey(CLASS_BUNDLE_KEY) shouldFilterTests = listTestsForOrchestrator && !runsSingleClass } override fun evaluateTest(description: Description): Boolean { if (!shouldFilterTests) { return true } val classTestFilter = description.testClass.getAnnotation(TestFilter::class.java) val testFilter = description.getAnnotation(TestFilter::class.java) if (testFilter != null) { return evaluateTestWithFilter(testFilter) } else if (classTestFilter != null) { return evaluateTestWithFilter(classTestFilter) } return true } private fun evaluateTestWithFilter(testFilter: TestFilter): Boolean { val filters = testFilter.filters val notFilters = testFilter.notFilters return (filters.contains(BuildConfig.FLAVOR) && !notFilters.contains(BuildConfig.FLAVOR)) } override fun describe(): String { return "Flavor filter" } } -
class FlavorFilter(bundle: Bundle) : ParentFilter() { companion object { private
const val CLASS_BUNDLE_KEY = "class" } private val shouldFilterTests: Boolean init { Timber.i("FlavorFilter bundle: %s", bundle) val listTestsForOrchestrator = java.lang.Boolean.parseBoolean(bundle.getString("listTestsForOrchestrator", "false")) val runsSingleClass = bundle.containsKey(CLASS_BUNDLE_KEY) shouldFilterTests = listTestsForOrchestrator && !runsSingleClass } override fun evaluateTest(description: Description): Boolean { if (!shouldFilterTests) { return true } val classTestFilter = description.testClass.getAnnotation(TestFilter::class.java) val testFilter = description.getAnnotation(TestFilter::class.java) if (testFilter != null) { return evaluateTestWithFilter(testFilter) } else if (classTestFilter != null) { return evaluateTestWithFilter(classTestFilter) } return true } private fun evaluateTestWithFilter(testFilter: TestFilter): Boolean { val filters = testFilter.filters val notFilters = testFilter.notFilters return (filters.contains(BuildConfig.FLAVOR) && !notFilters.contains(BuildConfig.FLAVOR)) } override fun describe(): String { return "Flavor filter" } } -
Using @TestFilter(flavors = [FLAVOR_1]) annotation 1- It is easy to
figure out which test gets run on which flavors as that information is right above the test method. 2- Adding new tests does not require updating any external configuration files. 3- All tests are in androidTest folder so it’s easier to navigate through them. -
How to debug a JVM test which runs by command
line ? ./gradlew test'$buildVariantName'Test - Dorg.gradle.daemon=false --tests '$package.className' -- debug-jvm 'Listening for transport dt_socket at address: 5005' export GRADLE_OPTS="- agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=500 5" -
How to debug a JVM test which runs by command
line ? ./gradlew test'$buildVariantName'Test - Dorg.gradle.daemon=false --tests '$package.className' -- debug-jvm 'Listening for transport dt_socket at address: 5005' export GRADLE_OPTS="- agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=500 5" -
Use test class level qualifiers @RunWith(RobolectricTestRunner::class) @Config(sdk = [21], application
= TestTdaApplication::class) abstract class RobolectricTestBase { -
Use test class level qualifiers @RunWith(RobolectricTestRunner::class) @Config(sdk = [21], application
= TestTdaApplication::class) abstract class RobolectricTestBase { -
Use rules to clean your env! /** * Clear all
application data here. * Done to ensure all UI tests start from the same clean state. */ class ClearAppDataRule : ExternalResource() { private val db: MvRxDb by ApplicationProvider.getApplicationContext<MvRxTestApplication>().inject() override fun before() { if (SystemUtils.isART()) { db.clearAllTables() } super.before() } } -
@RunWith(RobolectricTestRunner::class) @Config(sdk = [21], application = TestTdaApplication::class) abstract class RobolectricTestBase
{ private var app: Application? = null companion object { @JvmStatic @BeforeClass fun beforeClass() { // This forces the usage of com.almworks.sqlite4java:1.0.392, which internally uses SQLite 3.8.7. // This version of SQLite is new enough to support all SQLite features we use SQLiteLibraryLoader.load() } } @Before @CallSuper open fun setUp() { MockitoAnnotations.initMocks(this) app = ApplicationProvider.getApplicationContext() } } …are still a lot… -
@RunWith(RobolectricTestRunner::class) @Config(sdk = [21], application = TestTdaApplication::class) abstract class RobolectricTestBase
{ private var app: Application? = null companion object { @JvmStatic @BeforeClass fun beforeClass() { // This forces the usage of com.almworks.sqlite4java:1.0.392, which internally uses SQLite 3.8.7. // This version of SQLite is new enough to support all SQLite features we use SQLiteLibraryLoader.load() } } @Before @CallSuper open fun setUp() { MockitoAnnotations.initMocks(this) app = ApplicationProvider.getApplicationContext() } } …are still a lot… -
Use shadow classes ! Why “Shadow?” Shadow objects are not
quite Proxies, not quite Fakes, not quite Mocks or Stubs. Shadows are sometimes hidden, sometimes seen, and can lead you to the real object. At least we didn’t call them “sheep”, which we were considering. -
…to avoid lot of JVM incompatibilities ! package com.tui.tda.shadows import
android.util.Log import com.tui.tda.components.messages.fragments.MessagesOnlineDetai lsFragment import org.robolectric.annotation.Implementation import org.robolectric.annotation.Implements @Implements(MessagesOnlineDetailsFragment::class) class MessagesOnlineDetailsFragmentShadow { /** * Run SoomthScrollPosition isn't working in Roboelectric, so we need * to shadow the class in this case. */ @Implementation fun smoothScrollToPosition() { Log.e(MessagesOnlineDetailsFragmentShadow::class.java.name, "smoothScrollToPosition() shadow method due https:// github.com/robolectric/robolectric/issues/3552") } } -
…to avoid lot of JVM incompatibilities ! package com.tui.tda.shadows import
android.util.Log import com.tui.tda.components.messages.fragments.MessagesOnlineDetai lsFragment import org.robolectric.annotation.Implementation import org.robolectric.annotation.Implements @Implements(MessagesOnlineDetailsFragment::class) class MessagesOnlineDetailsFragmentShadow { /** * Run SoomthScrollPosition isn't working in Roboelectric, so we need * to shadow the class in this case. */ @Implementation fun smoothScrollToPosition() { Log.e(MessagesOnlineDetailsFragmentShadow::class.java.name, "smoothScrollToPosition() shadow method due https:// github.com/robolectric/robolectric/issues/3552") } } -
…to avoid lot of JVM incompatibilities ! package com.tui.tda.shadows import
android.util.Log import com.tui.tda.components.messages.fragments.MessagesOnlineDetai lsFragment import org.robolectric.annotation.Implementation import org.robolectric.annotation.Implements @Implements(MessagesOnlineDetailsFragment::class) class MessagesOnlineDetailsFragmentShadow { /** * Run SoomthScrollPosition isn't working in Roboelectric, so we need * to shadow the class in this case. */ @Implementation fun smoothScrollToPosition() { Log.e(MessagesOnlineDetailsFragmentShadow::class.java.name, "smoothScrollToPosition() shadow method due https:// github.com/robolectric/robolectric/issues/3552") } } -
References : Testing Rebooted (with AndroidX Test) (Android Dev Summit
'18) Frictionless Android testing: write once, run everywhere (Google I/O '18) Do Android devs dream of Robolectric Espresso? by Sarah Sharp (The Telegraph) Testing Android Apps at Scale with Nitrogen (Android Dev Summit '18) Android Test documentation How to remote debug a Robolectric test by command line using Gradle Code examples : https://github.com/lupsyn/mvRx_playground