tests everywhere
TRANSCRIPT
Tests everywhere
From unit testing to UI testing for Android
★ @laura_morillo★ Software Developer at Cabify★ Tech&Ladies and AgileGirls organizer
Android tests
UNIT TESTS(run on the JVM)
instrumentation tests
(Run on device or emulator)
CONFIGURING UNIT TESTS1. 2.
3.
Class to test
TEST WIZARD
TestCalculator.java
TestCalculator.java
First Test ImplementationJUnit traditional assert methods
JUnit better assert method (with hamcrest!)
Executing Unit Tests
or
Checking test results (Android Studio)
Checking test results (Console)
Implementing the code
(That was easy!)
And done!
Configuring instrumentation tests (I)
Configuring instrumentation tests (II)
Configuring instrumentation tests (III)
UI To test
Activity Test
Espresso test
Espresso test: Step 1
Espresso test: Step 2
Espresso test: Step 3
Espresso is powerfulWe can interact with views (onView)
We can interact with data adapters (onData)
Go back (pressBack)
Open menus (openActionBarOverflowOrOptionsMenu and openContextualActionModeOverflowMenu)
And check if the element is checked (isChecked), can be clicked (isClickable), is being displayed in the screen (isDisplayed)...
Image courtesy of jesadaphorn at FreeDigitalPhotos.net
UI Testing is nice… but too
slow
So let’s do more JVM unit/Small test
And we mock the android dependencies
But mocking is not always niceParcellable example
Services also need android without ui
Services rule!
But not all services rule!
IntentService
Espresso IntentstestRule: Stubbing
Espresso IntentstestRule: Verification
SummaryWe have Unit tests (JVM) and Instrumentation Tests (Android Device)
Add Unit test as much as possible.
Add mocks for your dependencies when needed.
ActivityTestRule to test activities controlling their lifecycle.
ServiceTestRule to test Services controlling their lifecycle (but not valid for IntentService)
Espresso IntentsTestRule to handle activities and stub and verify intents
Test your UI with espresso
Check Google examples for more feature: https://github.com/googlesamples/android-testing