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

Effective Espresso #roppongi_aar

Rize MISUMI
November 17, 2015

Effective Espresso #roppongi_aar

at Roppongi.aar #2

Rize MISUMI

November 17, 2015
Tweet

More Decks by Rize MISUMI

Other Decks in Programming

Transcript

  1. Espresso is... → High level wrapper of Instrumentation → High

    level wrapper of InputEvent → View assertion
  2. Espresso Pros. → You can stub out unnecessary APIs →

    Simple synchronous interaction API → Built-in matchers
  3. Espresso Cons. → Requires slow simulator or real device →

    Long test problem → Built-in matchers
  4. Use case of Espresso → Activity integration test → Do

    not test everything using Espresso → Avoid writing too many tests using Espresso
  5. build.gradle android { defaultConfig { // ... testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' }

    // ... packagingOptions { exclude 'LICENSE.txt' } } dependencies { // ... androidTestCompile 'com.android.support.test:runner:0.4.1' androidTestCompile 'com.android.support.test:rules:0.4.1' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' }
  6. Run with JUnit 4 @RunWith(AndroidJUnit4Runner.class) public class MainActivityTest { @Rule

    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class); @Test public void submitButtonIsDisplayed() { Espresso.onView(ViewMatchers.withId(R.id.submit_button)) .check(ViewAssertions.isDisplayed()); } }
  7. Note → @Rule is just JUnit lifecycle hook → You

    can also launch Activity manually → You can also assert regular JUnit way
  8. Intent matching @Test public void testTransitingToBrowser() { Espresso.onView(ViewMatchers.withId(R.id.launch_browser)) .perform(ViewActions.click()); Intents.intended(

    Matchers.allOf( IntentMatchers.hasAction(IsEqual.equalTo(Intent.ACTION_VIEW)), IntentMatchers.hasData("https://google.com") ) ); }
  9. public class MyIdlingResource implements IdlingResource { private ResourceCallback resourceCallback; private

    idle = true; @Override public String getName() { return IntentServiceIdlingResource.class.getName(); } @Override public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { this.resourceCallback = resourceCallback; } @Override public boolean isIdleNow() { if (idle && resourceCallback != null) { resourceCallback.onTransitionToIdle(); } return idle; } public void setIdle(boolean idle) { this.idle = idle; } }
  10. On test @Test public void testHandleAsync() { MyIdlingResource idlingResource =

    new MyIdlingResource(); Espresso.registerIdlingResources(idlingResource); // Do something // Do not forget unregister idlingResource Espresso.unregisterIdlingResources(idlingResource); }
  11. @Test public void submitButtonIsDisplayed() { submitButton().check( ViewAssertions.isDisplayed() ); } private

    ViewInteraction submitButton() { return Espresso.onView( ViewMatchers.withText("Submit") ); }
  12. Use Faker → More natural user data → Bootstrappable (zip

    code, address) → Internet-friendly (email, domain)
  13. private Faker faker; @Before public void setUp() { faker =

    new Faker(); } @Test public void inputEmailAddress() { editEmailAddress() .perform(ViewActions.sendKeys(emailAddress())); } private String emailAddress() { return faker.internet().emailAddress(); }
  14. Check with Lambda @Test public void textIsCorrect() { myTextView() .check((View

    view, NoMatchingViewException e) -> if (e != null) { Assert.fail(e.getMessage()); } Assert.assertEquals( ((TextView) view).getText().toString(), "Hello, world" ); }) }
  15. But little dirty hack is needed with Kotlin... private val

    activityRule = ActivityTestRule(MainActivity::class.java) @Rule public fun getActivityRule(): ActivityTestRule<MainActivity> = activityRule
  16. Does Page Object Pattern adapt? → I think page object

    pattern is too huge → Espresso: Integration test → Selenium (Appium): End-to-end test
  17. Conclusion → Espresso is high level wrapper of Instrumentation →

    There are many tips to make tests solid → Do not test everything using Espresso