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
  2. JUnit4: What is? — A huge, open-source testing library for

    Java — Has subclassable test runners
  3. Testing: Ye Olde Way — ActivityInstrumentationTestCase2<T extends Activity> — Name

    of method to be tested starts with test — If something is temporarily broken, either delete it or comment it out.
  4. Testing: Ye Olde Way — ActivityInstrumentationTestCase2<T extends 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.
  5. Testing: Ye JUnit4 Way — Uses annotations instead of naming

    conventions — Allows tests to be marked as ignored
  6. 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
  7. 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*
  8. 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
  9. JUnit4 Benefits — Tests don't reference an activity unless they

    need to — Name things in a way that makes sense to YOU
  10. 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
  11. JUnit4 Drawbacks — !Assume.isWorkingRightNow() — FlakyTest does not work with

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

    JUnit4 tests. — You have to static import import static a lot more stuff.
  13. Espresso: What is? — Open-source UI testing framework for Android

    created by Google. — A great way to integration-test your application
  14. Espresso: How work? — Hamcrest Matchers dig through the view

    hierarchy — Only considers the portions of the view immediately visible to the user
  15. 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
  16. 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
  17. UI Testing Best Practices — Don't navigate through your entire

    UI every single time. — Use class and instance Before/After methods
  18. 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
  19. 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.
  20. Other Tips for UI Testing — Make a mock flavor,

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

    and test on it — Feed your networking stack mock data (Mocktrofit)
  22. 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.
  23. 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!
  24. 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
  25. 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
  26. 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