Sharing (tests) is caring ❤️

Sharing (tests) is caring ❤️

6923bdeb363961b064d2cdb6329982d6?s=128

Roberto Orgiu

March 26, 2019
Tweet

Transcript

  1. SHARING (TESTS) IS CARING

  2. THE ISSUE Device tests are slow. Like real slow.

  3. A HACKY SOLUTION Running Android tests on JVM

  4. SHARED FOLDER Same level as androidTest and test. Same structure

    as well.
  5. android { ... sourceSets { String sharedTestDir = 'src/sharedTest/java' test

    { java.srcDir sharedTestDir } androidTest { java.srcDir sharedTestDir } } } What happens in build.gradle stays in build.gradle
  6. testImplementation 'androidx.test:runner:1.1.1' testImplementation 'androidx.test.espresso:espresso-core:3.1.1' testImplementation 'androidx.test.ext:junit:1.1.0' testImplementation 'org.robolectric:robolectric:4.1' androidTestImplementation 'androidx.test:runner:1.1.1'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.0' We ❤ dependencies
  7. testImplementation 'androidx.test:runner:1.1.1' testImplementation 'androidx.test.espresso:espresso-core:3.1.1' testImplementation 'androidx.test.ext:junit:1.1.0' testImplementation 'org.robolectric:robolectric:4.1' androidTestImplementation 'androidx.test:runner:1.1.1'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.0' We ❤ dependencies
  8. Everything always works as planned — Nobody ever

  9. testOptions { unitTests { includeAndroidResources = true returnDefaultValues = true

    } } JVM needs to know, after all.
  10. @RunWith(AndroidJUnit4::class) class MainActivityTest { @Test fun simple_test() { ActivityScenario.launch(MainActivity::class.java) onView(withId(R.id.fab)).check(matches(isDisplayed()))

    } }
  11. @RunWith(AndroidJUnit4::class) class MainActivityTest { @Test fun simple_test() { ActivityScenario.launch(MainActivity::class.java) onView(withId(R.id.fab)).check(matches(isDisplayed()))

    } } It wouldn't really work without that
  12. RUNNING TESTS

  13. DEVICE TESTS

  14. JVM TESTS

  15. Test configuration should have different names — Me. So it's

    true.
  16. FUN FACT

  17. Default is Device tests! — Me again.

  18. WAIT A MINUTE !

  19. IF BOTH CONFIGURATIONS CAN SEE SHARED TESTS...

  20. ... DOES IT MEAN JENKINS WOULD RUN THEM TWICE?

  21. YES!

  22. BUT THAT IS A PROBLEM

  23. YES!

  24. BUT WE CAN FIX IT! !

  25. ! EXCLUDE SOME TESTS!

  26. interface DoNotRunOnJvm Inside sharedTest folder

  27. testOptions { ... unitTests.all { useJUnit { excludeCategories 'net.orgiu.tests.DoNotRunOnJvm' }

    } } Once more in build.gradle
  28. testOptions { ... unitTests.all { useJUnit { excludeCategories 'net.orgiu.tests.DoNotRunOnJvm' }

    } } Once more in build.gradle
  29. ... AND FINALLY @RunWith(AndroidJUnit4::class) @Category(DoNotRunOnJvm::class) class MainActivityTest { @Test fun

    simple_test() { ActivityScenario.launch(MainActivity::class.java) onView(withId(R.id.fab)).check(matches(isDisplayed())) } }
  30. ... AND FINALLY @RunWith(AndroidJUnit4::class) @Category(DoNotRunOnJvm::class) class MainActivityTest { @Test fun

    simple_test() { ActivityScenario.launch(MainActivity::class.java) onView(withId(R.id.fab)).check(matches(isDisplayed())) } }
  31. REMEMBER TO TEST

  32. YOU'RE BEING WATCHED.

  33. ! THANKS FOR WATCHING!