Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automated Testing in Android

Automated Testing in Android

Presentation I gave at Internal forum at work, looking at how to best struture your code for unit testing and Espresso testing.

Rebecca Franks

November 25, 2015
Tweet

More Decks by Rebecca Franks

Other Decks in Programming

Transcript

  1. Automated Testing 

    in Android

    View Slide

  2. Rebecca Franks
    @riggaroo
    www.riggaroo.co.za
    Android Developer
    @DStv for DVT

    View Slide

  3. 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

    View Slide

  4. Let me tell you a
    story…

    View Slide

  5. Meet Bleep
    He wants to make a quick POC for business.
    Activity
    Content
    Provider
    API
    Manager
    API
    Requests

    View Slide

  6. 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

    View Slide

  7. 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

    View Slide

  8. ????
    Crash reports keep coming in
    One year later…

    View Slide

  9. 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.

    View Slide

  10. A Better Solution

    View Slide

  11. App Structure
    User Actions
    Presenter
    Updates View
    Model
    Updates Model
    State-change
    events
    View
    (Activities, Fragments)
    API
    (DB, file, REST API)
    Repositories

    View Slide

  12. Example App
    Loads a list of books that a user can
    click on and download.
    Can change language preference.

    View Slide

  13. 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

    View Slide

  14. Example:View Contract &
    Actions Contract
    Actions that can be
    executed
    from the View
    Methods that the
    presenter
    will call on the View

    View Slide

  15. Example: Activity
    Activity will then implement the View Contract and have a reference to a
    UserActionsListener

    View Slide

  16. Example: Presenter

    View Slide

  17. Example: Repository
    Could hit a cache here

    View Slide

  18. Example: API

    View Slide

  19. Types of Testing

    View Slide

  20. • 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

    View Slide

  21. 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

    View Slide

  22. What?
    • Unit Tests
    • Repositories
    • BookDetailRepositoryImpl
    • Presenters
    • ListBooksPresenter
    • Any logic - calculations etc
    • UI Tests
    • Activities & Fragments
    • ListBooksActivity

    View Slide

  23. Setting up Project
    for tests

    View Slide

  24. • 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.

    View Slide

  25. Gradle Flavors
    org.bookdash.android:
    app-prod-release.apk
    app-prod-debug.apk
    org.bookdash.android.mock:
    app-mock-debug.apk

    View Slide

  26. Dependencies

    View Slide

  27. Unit Tests

    View Slide

  28. Structure
    • GWT
    • Given
    • When
    • Then
    • Example:
    • Given I am logged in
    • When I click logout
    • Then I should see a login button

    View Slide

  29. • Within the test/ directory

    View Slide

  30. List Books Presenter Test

    View Slide

  31. 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

    View Slide

  32. • 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.

    View Slide

  33. 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

    View Slide

  34. PowerMockito

    View Slide

  35. Instrumentation
    Tests

    View Slide

  36. What is Espresso?

    View Slide

  37. • 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.

    View Slide

  38. Formula
    ViewMatcher
    ViewAction
    ViewAssertation
    Basic Espresso Forumla

    View Slide

  39. Espresso Cheat Sheet

    View Slide

  40. Hamcrest Cheat Sheet

    View Slide

  41. Setup
    • Build Variants Dialog > Android
    Instrumentation Tests
    • Switch environment here too - mockDebug
    • Need to disable animations on device

    View Slide

  42. 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.

    View Slide

  43. Fake Book Data

    View Slide

  44. Basic View Assertions

    View Slide

  45. Intent Testing

    View Slide

  46. WebView Testing

    View Slide

  47. Idling Resource

    View Slide

  48. Code Coverage
    Reports

    View Slide

  49. Unit Test Coverage

    View Slide

  50. View Slide

  51. Instrumentation Test
    Coverage
    • ./gradlew createMockDebugCoverageReport

    View Slide

  52. View Slide

  53. Google Cloud Test Lab
    • Upload Test APK and Real APK
    • Select Devices to run it on
    • Get reports for the tests

    View Slide

  54. View Slide

  55. Questions?

    View Slide

  56. Links
    • MVP Code Lab - http://www.code-labs.io/codelabs/android-
    testing/index.html
    • Ingredients for a healthy codebase - http://
    www.slideshare.net/mobile/pileromain/ingredients-for-a-
    healthy-codebase
    • Espresso Testing Documentation - http://developer.android.com/
    training/testing/ui-testing/espresso-testing.html
    • Espresso Cheat Sheet - https://github.com/googlesamples/
    android-testing/blob/master/downloads/Espresso%20Cheat%20Sheet
    %20Master.pdf
    • Hamcrest Cheat Sheet -http://www.marcphilipp.de/downloads/
    posts/2013-01-02-hamcrest-quick-reference/Hamcrest-1.3.pdf
    • Espresso Examples https://github.com/chiuki/espresso-samples
    • Johannesburg Android Developers Community - https://
    plus.google.com/communities/107730573804920392616

    View Slide