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

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 full-size slide

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

    View full-size slide

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

    View full-size slide

  4. Testing: Ye Olde Way
    — ActivityInstrumentationTestCase2extends Activity>

    View full-size slide

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

    View full-size slide

  6. 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 full-size slide

  7. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  10. 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 full-size slide

  11. 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 full-size 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
    — Allows you to have tests skipped based on an
    assumption*
    * - in theory

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. 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 full-size slide

  16. JUnit4 Drawbacks
    — !Assume.isWorkingRightNow()

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. 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 full-size slide

  25. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  28. 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 full-size slide

  29. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  32. Shameless
    Plug Alert!

    View full-size slide

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

    View full-size slide

  34. 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 full-size slide

  35. 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 full-size slide

  36. 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 full-size slide

  37. Question Time!

    View full-size slide

  38. 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 full-size slide

  39. 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 full-size slide

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

    View full-size slide