Share
Embed
Getting Started with AndroidX Test
Testing on Android is slowly becoming more approachable. With AndroidX Test, you can use a single test API to run your instrumentation tests either on your mobile device, or on your computer using Robolectric. This is great, so how do you start? In this talk you’ll learn how to use AndroidX Test and Espresso to get started writing Integration tests for your app.
Victoria Gonda
April 23, 2019
Transcript
66 slides
-
@TTGonda victoriagonda.com Why Test? • Make changes with confidence •
Living documentation • Encourages maintainable, clean code -
@TTGonda victoriagonda.com Victoria Gonda • Android Engineer at Buffer •
Author and Editor at RayWenderlich.com • Chicago native • Hedgehog and cat owner • she/her -
@TTGonda victoriagonda.com Testing Tools • JUnit4 • JUnit5 • Retrofit-mock
• Espresso • Mockito • Robolectric • Spek • KotlinTest • MockK • Kakao • Hiroaki • Barista • Biscotti • AndroidX Test -
@TTGonda victoriagonda.com AndroidX Test • Includes some of the libraries
you already know, plus some new ones • Aims to solve the issue of too many libraries • Can be run both on device or on the JVM -
@TTGonda victoriagonda.com What’s Inside? • JUnit • Core • Espresso
• Truth Assertions -
@TTGonda victoriagonda.com @get:Rule val activityRule = ActivityScenarioRule(MainActivity::class.java) // in test
function val scenario = activityRule.scenario -
@TTGonda victoriagonda.com @get:Rule val activityRule = ActivityScenarioRule(MainActivity::class.java) // in test
function val scenario = activityRule.scenario -
@TTGonda victoriagonda.com val applicationInfo = ApplicationInfoBuilder .newBuilder() .setPackageName("com.victoriagonda.tasklist") .build() PackageInfoBuilder.newBuilder()
.setApplicationInfo(applicationInfo) .build() -
@TTGonda victoriagonda.com @Test fun mainActivityShowsAddButton() { val scenario = ActivityScenario
.launch(MainActivity::class.java) onView(withId(R.id.buttonAddList)) .check(matches(isDisplayed())) } -
@TTGonda victoriagonda.com @Test fun mainActivityShowsAddButton() { val scenario = ActivityScenario
.launch(MainActivity::class.java) onView(withId(R.id.buttonAddList)) .check(matches(isDisplayed())) } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition<TaskListViewHolder>(0, click())) Intents.intended(IntentMatchers .hasComponent(DetailActivity::class.java.name)) Intents.release() } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition<TaskListViewHolder>(0, click())) Intents.intended(IntentMatchers .hasComponent(DetailActivity::class.java.name)) Intents.release() } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition<TaskListViewHolder>(0, click())) Intents.intended(IntentMatchers .hasComponent(DetailActivity::class.java.name)) Intents.release() } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition<TaskListViewHolder>(0, click())) Intents.intended(IntentMatchers .hasComponent(DetailActivity::class.java.name)) Intents.release() } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition<TaskListViewHolder>(0, click())) Intents.intended(IntentMatchers .hasComponent(DetailActivity::class.java.name)) Intents.release() } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition<TaskListViewHolder>(0, click())) Intents.intended(IntentMatchers .hasComponent(DetailActivity::class.java.name)) Intents.release() } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition<TaskListViewHolder>(0, click())) Intents.intended(IntentMatchers .hasComponent(DetailActivity::class.java.name)) Intents.release() } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition(0, click())) val intent = getIntents().first() IntentSubject.assertThat(intent) .hasComponentClass(DetailActivity::class.java) Intents.release() } -
@TTGonda victoriagonda.com @Test fun selectingListOpensDetailActivity() { ActivityScenario.launch(MainActivity::class.java) Intents.init() onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions
.actionOnItemAtPosition(0, click())) val intent = getIntents().first() IntentSubject.assertThat(intent) .hasComponentClass(DetailActivity::class.java) Intents.release() } -
@TTGonda victoriagonda.com val intent = DetailActivity.newIntent(tasklist, ApplicationProvider.getApplicationContext()) val bundleSubject: BundleSubject
= assertThat(intent).extras() bundleSubject.containsKey("EXTRA_TASKLIST") val integerSubject: IntegerSubject = bundleSubject.integer("EXTRA_TASKLIST") integerSubject.isEqualTo(tasklist.id) -
@TTGonda victoriagonda.com fun newIntent(list: Tasklist, context: Context): Intent { return
Intent(context, DetailActivity::class.java) .apply { putExtra("EXTRA_TASKLIST", list.id) } } -
@TTGonda victoriagonda.com val intent = DetailActivity.newIntent(tasklist, ApplicationProvider.getApplicationContext()) val bundleSubject: BundleSubject
= assertThat(intent).extras() bundleSubject.containsKey("EXTRA_TASKLIST") val integerSubject: IntegerSubject = bundleSubject.integer("EXTRA_TASKLIST") integerSubject.isEqualTo(tasklist.id) -
@TTGonda victoriagonda.com val intent = DetailActivity.newIntent(tasklist, ApplicationProvider.getApplicationContext()) val bundleSubject: BundleSubject
= assertThat(intent).extras() bundleSubject.containsKey("EXTRA_TASKLIST") val integerSubject: IntegerSubject = bundleSubject.integer("EXTRA_TASKLIST") integerSubject.isEqualTo(tasklist.id) -
@TTGonda victoriagonda.com val intent = DetailActivity.newIntent(tasklist, ApplicationProvider.getApplicationContext()) val bundleSubject: BundleSubject
= assertThat(intent).extras() bundleSubject.containsKey("EXTRA_TASKLIST") val integerSubject: IntegerSubject = bundleSubject.integer("EXTRA_TASKLIST") integerSubject.isEqualTo(tasklist.id) -
@TTGonda victoriagonda.com value of : intent.getComponent().getClassName() expected : com.victoriagonda.android.tasklist.DetailActivity but
was : com.victoriagonda.android.tasklist.MainActivity -
@TTGonda victoriagonda.com Wanted to match 1 intents. Actually matched 0
intents. IntentMatcher: has component: has component with: class name: is "com.victoriagonda.android.tasklist.DetailActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String Matched intents:[] Recorded intents: -Intent { cmp=com.victoriagonda.android.tasklist/.MainActivity (has extras) } handling packages:[[com.victoriagonda.android.tasklist]], extras:[Bundle[{EXTRA_TASKLIST=1}]]) -
@TTGonda victoriagonda.com value of : intent.getExtras() expected to contain key
EXTRA_TASKLIST but was : Bundle[{EXTRA_TASKNAME=Laundry}] -
@TTGonda victoriagonda.com • MotionEvent • PointerCoords • PointerProperties • IntentCorrespondences
• IntentCorrespondences • NotificationAction • Notification • PendingIntent -
@TTGonda victoriagonda.com Wrapping Up • Google wrapped up all testing
under one AndroidX roof • Four parts, JUnit, Core, Espresso, Truth • Can ALL be run on or off device • Hopefully means less APIs to learn and less worry about on or off device