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

Super-Charging Your Android Testing With JUnit 4 and Espresso 2 - Bay Area Android Meetup, July 2015

Super-Charging Your Android Testing With JUnit 4 and Espresso 2 - Bay Area Android Meetup, July 2015

All the new hotness (c. July 2015) of testing on Android.

Sample code is finally fixed and is at https://github.com/designatednerd/Wino

Ellen Shapiro
PRO

July 16, 2015
Tweet

More Decks by Ellen Shapiro

Other Decks in Technology

Transcript

  1. Super-Charging
    Your Android Tests
    With JUnit4 and
    Espresso 2
    Bay Area Android Meetup, July 2015
    Ellen Shapiro
    @designatednerd | vokal.io

    View Slide

  2. View Slide

  3. View Slide

  4. JUnit4: What is?
    — A huge, open-source testing library for Java

    View Slide

  5. JUnit4: What is?
    — A huge, open-source testing library for Java
    — Has subclassable test runners

    View Slide

  6. Testing: Ye Olde Way
    — ActivityInstrumentationTestCase2extends Activity>

    View Slide

  7. Testing: Ye Olde Way
    — ActivityInstrumentationTestCase2extends Activity>
    — Name of method to be tested starts with test

    View Slide

  8. Testing: Ye Olde Way
    — ActivityInstrumentationTestCase2extends Activity>
    — Name of method to be tested starts with test
    — If something is temporarily broken, either delete it or
    comment it out.

    View Slide

  9. Testing: Ye Olde Way
    — ActivityInstrumentationTestCase2extends Activity>
    — Name of method to be tested starts with test
    — If something is temporarily broken, either delete it or
    comment it out.
    — Testing different data requires a separate test calling
    a private helper.

    View Slide

  10. Testing: Ye JUnit4 Way
    — Uses annotations instead of naming conventions

    View Slide

  11. Testing: Ye JUnit4 Way
    — Uses annotations instead of naming conventions
    — Allows tests to be marked as ignored

    View Slide

  12. Testing: Ye JUnit4 Way
    — Uses annotations instead of naming conventions
    — Allows tests to be marked as ignored
    — Runner has subclasses allowing all sorts of fancy
    things

    View Slide

  13. Testing: Ye JUnit4 Way
    — Uses annotations instead of naming conventions
    — Allows tests to be marked as ignored
    — Runner has subclasses allowing all sorts of fancy
    things
    — Allows you to have tests skipped based on an
    assumption*

    View Slide

  14. Testing: Ye JUnit4 Way
    — Uses annotations instead of naming conventions
    — Allows tests to be marked as ignored
    — Runner has subclasses allowing all sorts of fancy
    things
    — Allows you to have tests skipped based on an
    assumption*
    * - in theory

    View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. JUnit4 Benefits
    — Tests don't reference an activity unless they need to

    View Slide

  19. JUnit4 Benefits
    — Tests don't reference an activity unless they need to
    — Name things in a way that makes sense to YOU

    View Slide

  20. JUnit4 Benefits
    — Tests don't reference an activity unless they need to
    — Name things in a way that makes sense to YOU
    — Run the same test many times with different params

    View Slide

  21. JUnit4 Drawbacks
    — !Assume.isWorkingRightNow()

    View Slide

  22. JUnit4 Drawbacks
    — !Assume.isWorkingRightNow()
    — FlakyTest does not work with JUnit4 tests.

    View Slide

  23. View Slide

  24. JUnit4 Drawbacks
    — !Assume.isWorkingRightNow()
    — FlakyTest does not work with JUnit4 tests.
    — You have to static import a lot more stuff.

    View Slide

  25. JUnit4 Drawbacks
    — !Assume.isWorkingRightNow()
    — FlakyTest does not work with JUnit4 tests.
    — You have to static import import static a lot
    more stuff.

    View Slide

  26. DEMO

    View Slide

  27. Espresso: What is?
    — Open-source UI testing
    framework for Android created
    by Google.

    View Slide

  28. Espresso: What is?
    — Open-source UI testing
    framework for Android created
    by Google.
    — A great way to integration-test
    your application

    View Slide

  29. Espresso: How work?
    — Hamcrest Matchers dig
    through the view hierarchy

    View Slide

  30. Espresso: How work?
    — Hamcrest Matchers dig
    through the view hierarchy
    — Only considers the portions of
    the view immediately visible
    to the user

    View Slide

  31. Espresso: How work?
    — Hamcrest Matchers dig
    through the view hierarchy
    — Only considers the portions of
    the view immediately visible
    to the user
    — Can enter text, tap things,
    scroll, swipe out of the box

    View Slide

  32. Espresso: How work?
    — Hamcrest Matchers dig
    through the view hierarchy
    — Only considers the portions of
    the view immediately visible
    to the user
    — Can enter text, tap things,
    scroll, swipe out of the box
    — Create your own actions

    View Slide

  33. DEMO

    View Slide

  34. UI Testing Best Practices
    — Don't navigate through your entire UI every single
    time.

    View Slide

  35. UI Testing Best Practices
    — Don't navigate through your entire UI every single
    time.
    — Use class and instance Before/After methods

    View Slide

  36. UI Testing Best Practices
    — Don't navigate through your entire UI every single
    time.
    — Use class and instance Before/After methods
    — Create a test class for each piece of your UI

    View Slide

  37. UI Testing Best Practices
    — Don't navigate through your entire UI every single
    time.
    — Use class and instance Before/After methods
    — Create a test class for each piece of your UI
    — If there are things you can test without the UI, test
    them without the UI.

    View Slide

  38. Other Tips for UI Testing
    — Make a mock flavor, and test on it

    View Slide

  39. Other Tips for UI Testing
    — Make a mock flavor, and test on it
    — Feed your networking stack mock data

    View Slide

  40. Shameless
    Plug Alert!

    View Slide

  41. View Slide

  42. Other Tips for UI Testing
    — Make a mock flavor, and test on it
    — Feed your networking stack mock data (Mocktrofit)

    View Slide

  43. Other Tips for UI Testing
    — Make a mock flavor, and test on it
    — Feed your networking stack mock data (Mocktrofit)
    — Compare R.string string resources to what's on
    screen.

    View Slide

  44. Other Tips for UI Testing
    — Make a mock flavor, and test on it
    — Feed your networking stack mock data (Mocktrofit)
    — Compare R.string string resources to what's on
    screen.
    — Remember when you need to scroll!

    View Slide

  45. Other Tips for UI Testing
    — Make a mock flavor, and test on it
    — Feed your networking stack mock data (Mocktrofit)
    — Compare R.string string resources to what's on
    screen.
    — Remember when you need to scroll!
    — Get to a piece of your UI that's buried without having
    to tap 800 buttons yourself

    View Slide

  46. Question Time!

    View Slide

  47. Code Links!
    — Sample Project: http://github.com/designatednerd/
    Wino
    — AndroidListener Parameterized Example: https://
    github.com/designatednerd/
    AndroidListenerExamples/ in the tests
    — Mocktrofit: https://github.com/vokal/mocktrofit

    View Slide

  48. JUnit4 Links!
    — User Guide: https://code.google.com/p/android-test-
    kit/wiki/AndroidJUnitRunnerUserGuide
    — Google Sample: https://github.com/googlesamples/
    android-testing/tree/master/testrunner/
    AndroidJunitRunnerSample
    — JUnit Docs: http://junit.org/javadoc/latest/
    index.html

    View Slide

  49. Espresso Links!
    — Quickstart Guide: https://code.google.com/p/
    android-test-kit/wiki/EspressoStartGuide

    View Slide