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

Android Testing

Android Testing

Android testing intro

Roi Sagiv

April 30, 2015
Tweet

More Decks by Roi Sagiv

Other Decks in Programming

Transcript

  1. Agenda Hello world Real world examples Espresso & Robotium The

    new unit test plugin Robolectic JUnit 4
  2. What is testing? Testing Fundamentals (developer.android.com) "Provides an architecture and

    powerful tools that help you test every aspect of your application at every level from unit to framework." In simple words: Run your app and interact with it, without touching your device/emulator.
  3. Let's see a demo If you want to follow along

    g i t c h e c k o u t h e l l o - w o r l d # g i t c l e a n i s r e c o m m e n d e d g i t c l e a n - d f
  4. Android Testing Framework Based on JUnit 3 Each test method

    must start with t e s t Test class need to extend one of the a n d r o i d . t e s t . * T e s t C a s e
  5. A c t i v i t y I n

    s t r u m e n t a t i o n T e s t C a s e 2 < T > Launches new activity before each test Finishes the activity after each test g e t A c t i v i t y ( )
  6. @ U i T h r e a d T

    e s t Runs the test method in the target app's main thread. Allows you to interact with UI elements (TextView, EditText, ...)
  7. Instrumentation Separated app, separated package name AndroidManifest.xml with < i

    n s t r u m e n t a t i o n / > Loaded into the same process Instrumentation provides access to target's C o n t e x t Start activities Get resources Main thread Instrumentation provides access to target's main thread
  8. Few real world examples Display list of users AsyncTaskLoader for

    the backend RecyclerView "Random User Generator"
  9. LocalFileUsersAPIClient g i t c h e c k o

    u t L o c a l F i l e U s e r s A P I C l i e n t T e s t Hide network calls behind an interface (UsersAPIClient) One implementation uses local files Another implementation actually performs HTTP requests
  10. ListUsersRecyclerViewAdapterTest Part 1 g i t c h e c

    k o u t L i s t U s e r s R e c y c l e r V i e w A d a p t e r T e s t - p a r t 1 Keep implementation simple Focus on testing the interface methods: g e t I t e m C o u n t ( ) o n C r e a t e V i e w H o l d e r ( ) o n B i n d V i e w H o l d e r ( )
  11. ListUsersRecyclerViewAdapterTest Part 2 g i t c h e c

    k o u t L i s t U s e r s R e c y c l e r V i e w A d a p t e r T e s t - p a r t 2 Feed the adapter with data as part of the test. R e c y c l e r V i e w . A d a p t e r D a t a O b s e r v e r
  12. ListUsersRecyclerViewAdapterTest Part 3 g i t c h e c

    k o u t L i s t U s e r s R e c y c l e r V i e w A d a p t e r T e s t - p a r t 3 I m a g e D o w n l o a d e r interface P i c a s s o I m a g e D o w n l o a d e r
  13. GetUsersLoaderTest Part 1 g i t c h e c

    k o u t G e t U s e r s L o a d e r T e s t Call l o a d I n B a c k g r o u n d ( ) directly to avoid difficult async testing T e s t a b l e U s e r s A P I C l i e n t - verify interaction with components
  14. GetUsersLoaderTest Part 2 g i t c h e c

    k o u t G e t U s e r s L o a d e r T e s t _ m o c k i t o Quick way to get T e s t a b l e * * * implementations Use version 1.9.5 & Add to s e t U p ( ) method: S y s t e m . s e t P r o p e r t y ( " d e x m a k e r . d e x c a c h e " , g e t C o n t e x t ( ) . g e t C a c h e D i r ( ) . g e t P a t h ( ) ) ; Mockito dexmaker
  15. UsersListActivityEspressoTest g i t c h e c k o

    u t U s e r s L i s t A c t i v i t y E s p r e s s o T e s t UI interactions Waits for A s y n c T a s k s & L o a d e r s to finish. Espresso
  16. UsersListActivityRobotiumTest g i t c h e c k o

    u t U s e r s L i s t A c t i v i t y R o b o t i u m T e s t UI interactions WebView / Mobile Web automation capabilities Slower than Espresso and less feature rich Robotium
  17. Android "Unit Tests" g i t c h e c

    k o u t L o c a l F i l e U s e r s A P I C l i e n t T e s t _ u n i t t e s t s Runs on the JVM -> no device is required Special version of Android (mockable-android-XX.jar) Useful for non-Android related code
  18. Robolectric g i t c h e c k o

    u t L o c a l F i l e U s e r s A P I C l i e n t _ r o b o l e c t r i c g i t c h e c k o u t L i s t U s e r s R e c y c l e r V i e w A d a p t e r T e s t _ r o b o l e c t r i c Special "Android SDK", tailored to testing Runs on the JVM -> no device is required A Lot of gotchas & quirks!
  19. MockWebServer g i t c h e c k o

    u t H t t p U s e r s A P I C l i e n t T e s t Local HTTP Server, tailored to testing Can simulate different network behaviors HTTP codes (200, 404, 500, ...) Timeouts Throttling Delays