Testing in Android
On November 23 & 24, I attended the first ever Android Dev Summit. The following are notes that I took during talks. I have included the video of the talk as well. Again, these are my notes from the talk, not my original content.
Chiu-Ki Chan (Android GDE) took really great notes during the session as well, here’s what she had:
Android Testing with @onlythoughtwork @ppvi @jfschmakeit #AndroidDevSummit #Sketchnotes pic.twitter.com/w30eLkXZvm
— Chiu-Ki Chan (@chiuki) November 24, 2015
People want automated testing. Android has not provided a ton of testing APIs, and it hasn’t evolved much since API level 3. Until recently.
Android provided tools for testing:
- Android Studio / Gradle
- Android Testing Support Library
- Espresso
Refactor across unit and instrumentation tests.
Hermetic testing
Flaky tests are worse than no tests. (When it fails sometimes.)
Steps:
- Isolate from external dependencies
- Especially the network
- Replace the components that talk to external dependencies, with things that replace with fake data
- Using product flavor ‘mockDebug’, with source sets
- Inject lives in both mock and prod source sets
When to use:
- Mock mode for manual testing & concurrent development
- Prod mode for end-to-end tests
- great pre-release check
Unit testing
Unit tests are fundamental. Run locally, generally fast, should be small and well contained. Focused on methods.
Don’t put everything into Activities, it makes it really hard to test. Move business logic out, so that it is more testable.
Mockable Android Jar.
Android Studio helps with Test-Driven-Development. If everything is set up correctly, AS can even help with new features, if tests are defined first.
UI Testing with Espresso
Integration and UI tests check your application through its interface. You need a device or emulator for this. CloutTest Lab allows you to test on real devices.
UI testing is hard! Espresso helps with this.
Tried to ask: What would a user do?
Find, perform, check
onView(ViewMatcher)
.perform(ViewAction)
.check(ViewAssertion);