Android Testing Support Library announced

During the Droidcon UK 2014, Stephan Linzner of Google announced the upcoming Android Testing Support Library. This means Google is (finally) taking testing Android apps seriously.

IMG_20141031_163015

The library is a unbundled static testing library for Android that containers all Android testing frameworks by Google. It will become a available through the Android Support repository and as open source in AOSP.

So what does it contain?

    • AndroidJUnitRunner

This Runner, based on android.test.InstrumentationTestRunner, runs JUnit3 and JUnit4 tests against an Android package (application). Let me give an example:

@RunWith (JUnit4.class)
public class MyJUnit4Test {
{
Droidcon mDroidconUK;

@Before
public void setUp() {
mDroidconUK = new Droidcons.get(Droidcon.UK);
mDroidconUK.open();
}

@Test
public void checkPreconditions() {
assertNotNull(“mDroidconUK cannot be null”, mDroidconUK);
}

@After
public void tearDown() {
mDroidconUK.close();
}
}

    • Espresso

Introduced at the GTAC in 2013, Espresso makes it possible to write concise, beautiful, and reliable Android UI tests quickly. Lets look at some examples:


// Find view using a Matcher and type text using a ViewAction
onView(withId(R.id.message_edit_text)).perform(typeText(TEXT_MESSAGE), closeSoftKeyboard());

// Perform a click ViewAction
onView(withId(R.id.send_button)).perform(click());

// Verify using a ViewAssertion
onView(withId(R.id.received_message_text_view)).check(matches(withText((TEXT_MESSAGE))));

    • Intento

Intento is like Mockito but for Intents. Basically a mock framework that allows you to create and configure mock objects. Let me give you an example:

public void testDailerInput_typeNumberAndCall() {

//Type phonenumber in dialer
onView(withId(R.id.send_data_to_call_edit_text)).perform(scrollTo(), typeText(“123-345-6789”), closeSoftKeyboard());

// Click the call button
onView(withId(R.id.send_to_call_button)).perform(scrollTo(), click());

//Validate Intent has been send
intended(allOf(
hasAction(Intent.ACTION_CALL),
toPackage(“com.android.phone”),
hasData(allOf(hasSchemaSpecificPart(“tel”, “123-345-6789”)))),
times(1));
}

In conclusion: Testing with Android is finally becoming a more prominent part of Android Development cycle with the introduction of the Android Testing Support Library

Advertisements

One thought on “Android Testing Support Library announced

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s