Pro Yearly is on sale from $80 to $50! »

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
PRO

July 16, 2015
Tweet

Transcript

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

    Area Android Meetup, July 2015 Ellen Shapiro @designatednerd | vokal.io
  2. None
  3. None
  4. JUnit4: What is? — A huge, open-source testing library for

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

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

  7. Testing: Ye Olde Way — ActivityInstrumentationTestCase2<T extends Activity> — Name

    of method to be tested starts with test
  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.
  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.
  10. Testing: Ye JUnit4 Way — Uses annotations instead of naming

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

    conventions — Allows tests to be marked as ignored
  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
  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*
  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
  15. None
  16. None
  17. None
  18. JUnit4 Benefits — Tests don't reference an activity unless they

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

    need to — Name things in a way that makes sense to YOU
  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
  21. JUnit4 Drawbacks — !Assume.isWorkingRightNow()

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

    JUnit4 tests.
  23. None
  24. JUnit4 Drawbacks — !Assume.isWorkingRightNow() — FlakyTest does not work with

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

    JUnit4 tests. — You have to static import import static a lot more stuff.
  26. DEMO

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

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

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

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

    hierarchy — Only considers the portions of the view immediately visible to the user
  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
  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
  33. DEMO

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

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

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

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

    and test on it — Feed your networking stack mock data
  40. Shameless Plug Alert!

  41. None
  42. Other Tips for UI Testing — Make a mock flavor,

    and test on it — Feed your networking stack mock data (Mocktrofit)
  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.
  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!
  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
  46. Question Time!

  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
  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
  49. Espresso Links! — Quickstart Guide: https://code.google.com/p/ android-test-kit/wiki/EspressoStartGuide