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

Acceptance test like a pro

Acceptance test like a pro

Tirando vantagem do Espresso e Android Studio para criar testes de aceitação incríveis

Rodrigo Sicarelli

May 07, 2016
Tweet

More Decks by Rodrigo Sicarelli

Other Decks in Technology

Transcript

  1. Android Dev BR medium.com/android-dev-br androiddevbr.org +1100 membros
 Novo artigo toda

    semana
 Newsletter semanal Canal direto com GDE’s 
 Codehelp, feedbacks, learn etc etc etc
  2. Our prod flavor @Override public void findMoviesByQuery(String query, Callback callback)

    { //Calling the API; } @Override public void findMovieById(String id, Callback callback) { //Calling the API; }
  3. Our mock flavor @Override public void findMoviesByQuery(String query, Callback callback)

    { //Return a fake json; } @Override public void findMovieById(String id, Callback callback) { //Return a fake json; }
  4. Using the ActivityMonitor public void registerMonitor(Class<? extends Activity> activityClass) {

    if (monitor != null) { getInstrumentation().removeMonitor(monitor); } monitor = new Instrumentation.ActivityMonitor(activityClass.getName(), null, false); getInstrumentation().addMonitor(monitor); } @Test public void shouldTestSomething() { onView(withId(R.id.login)).perform(click()); monitor.waitForActivity(); onData(anything()) .inAdapterView(withId(R.id.product_grid_view)) .atPosition(0) .perform(click()); } public class BaseActivityTestCase extends ActivityTestCase {...}
  5. Using the Monitor @Test public void shouldTestSomething() { onView(withId(R.id.login)).perform(click()); monitor.waitForActivity();

    registerMonitor(OtherActivity.class); onData(anything()) .inAdapterView(withId(R.id.product_grid_view)) .atPosition(0) .perform(click()); monitor.waitForActivity();//new monitor! }
  6. Usage public final class SimpleCountingIdlingResource implements IdlingResource {…} public class

    EspressoIdlingResource { ... public static void busy() { simpleCountingIdlingResource.increment(); } public static void idle() { simpleCountingIdlingResource.decrement(); } ... }
  7. Usage @Override public void findMoviesByQuery(String query, Callback callback) { EspressoIdlingResource.busy();

    ... searchResponseCall.enqueue(... { @Override public void onResponse(...) { EspressoIdlingResource.idle(); } @Override public void onFailure(...) { EspressoIdlingResource.idle(); } }); }
  8. Usage @Before private void registerIdlingResource() { Espresso.registerIdlingResources( rule.getActivity().getCountingIdlingResource()); } @After

    public void unregisterIdlingResource() { Espresso.unregisterIdlingResources( rule.getActivity().getCountingIdlingResource()); }