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