Agenda • A solution to messy android codebases • Types of Testing • Setting up for Tests • Unit Tests • Instrumentation Tests • Code Coverage Reports • Google Cloud Test Lab
Meet Bleep He wants to make a quick POC for business. Meet Ms. Pink She Hears about a cool library Activity Content Provider API Requests API Manager Retrofit New Fragments + Features
Meet Ms. Pink She Hears about a cool library Meet Bleep He wants to make a quick POC for business. Meet Mr Test Heard about tests – Wants to add unit test but tricky with android and too much that is not tested Activity Content Provider API Requests API Manager Retrofit New Fragments + Features Adds some Flaky UI Tests Story inspired by Romain Piel. Link at end
Observations • Android has no clear way to structure apps • Activities get messy. Everything ends up sitting in the activity class. • Difficult to test this kind of structure. • Hard to make changes • Almost always require a context for android tests.
Example: Folder Structure Data Folder: - Contains Repositories & APIs - Fetching stuff from server and/or device storage - Grouped by functionality Domain Folder: - POJOs - Possibly some business logic classes if wishing to separate it out Presentation Folder: - Grouped by functionality not class type - Contains MVP related classes per functionality folder
• Unit Testing • A unit test generally exercises the functionality of the smallest possible unit of code (which could be a method, class, or component) in a repeatable way. • JUnit • Mockito • PowerMockito • Two types of Unit testing: • Local Testing - Doesn’t run on physical device • Instrumentation Tests - Runs on an android device or emulator. • UI Testing • Mocking of typical user actions with your application. Clicking on buttons, typing in text etc. • Espresso • Robotium • Appium • Calabash • UIAutomator • Robolectric Types of Testing
Where? • Unit Tests - typically live in the test/ folder.* • Instrumentation Tests (UI Tests) - live in the androidTest/ folder. Unit Tests - Don’t need an android device or android APIs Android Instrumentation Tests - UI Tests - Unit tests that need an android device
• Set up folders in the following way • Using gradle build flavors – we can now have different APKs to use to test with • prodDebug • prodRelease • mockDebug • This can extend to point to have different folders per build environment.
List Books Presenter Test • Given: I have my language preference saved as English • When: I try load my english books • Then: The View should show the list of English Books
• Given: I have a language preference saved as English • When: I load books and there is an error loading • Then: The loading indicator should stop and I should see the error screen with a retry button.
Tips • Try to avoid passing a context around further than your View • Try keep android specific things in isolation • Possible to emulate some android.jar functions using PowerMockito
• Android Testing Framework • Provided by Google in support libraries • Instrumentation Based • Simulates User interactions – Clicks , Swipes etc • Automatic synchronisation of test actions • Waits for App to be “idle” before continuing with operations: • No UI events in the message queue • No Async Tasks running in default Async Task thread pool.
Setup • androidTest • Tests that should pass regardless of environment • androidTestMock • Tests that will run using the classes from the mock folder • Run the tests: ./gradlew connectedMockDebugAndroidTest Can setup a build server to run these instrumentation tests that will deploy to a device and test.