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

C4861b1dfdf3bbb21faec4a1acdf183d?s=128

Ellen Shapiro

July 16, 2015
Tweet

Transcript

  1. 1.

    Super-Charging Your Android Tests With JUnit4 and Espresso 2 Bay

    Area Android Meetup, July 2015 Ellen Shapiro @designatednerd | vokal.io
  2. 2.
  3. 3.
  4. 5.

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

    Java — Has subclassable test runners
  5. 8.

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

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

    Testing: Ye JUnit4 Way — Uses annotations instead of naming

    conventions — Allows tests to be marked as ignored
  8. 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
  9. 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*
  10. 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
  11. 15.
  12. 16.
  13. 17.
  14. 19.

    JUnit4 Benefits — Tests don't reference an activity unless they

    need to — Name things in a way that makes sense to YOU
  15. 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
  16. 23.
  17. 24.

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

    JUnit4 tests. — You have to static import a lot more stuff.
  18. 25.

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

    JUnit4 tests. — You have to static import import static a lot more stuff.
  19. 26.
  20. 28.

    Espresso: What is? — Open-source UI testing framework for Android

    created by Google. — A great way to integration-test your application
  21. 30.

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

    hierarchy — Only considers the portions of the view immediately visible to the user
  22. 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
  23. 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
  24. 33.
  25. 35.

    UI Testing Best Practices — Don't navigate through your entire

    UI every single time. — Use class and instance Before/After methods
  26. 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
  27. 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.
  28. 39.

    Other Tips for UI Testing — Make a mock flavor,

    and test on it — Feed your networking stack mock data
  29. 41.
  30. 42.

    Other Tips for UI Testing — Make a mock flavor,

    and test on it — Feed your networking stack mock data (Mocktrofit)
  31. 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.
  32. 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!
  33. 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
  34. 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
  35. 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