Share
Embed
Android Testing Support Library - the Nitty Gritty
A talk I gave at mdevcon 2016 in Amsterdam
It goes through bits and pieces that make up the Testing Support Library
Zan Markan
March 18, 2016
Transcript
59 slides
-
Testing 101 • on JVM vs on device • unit
/ integration / functional • Robolectric, Calabash, Instrumentation, Robotium -
“Support” • Framework vs Support libraries • Trend to unbundle
• support-v4, appcompat-v7, recyclerview, … -
Good Old Times… • jUnit 3 syntax • Remember ActivityInstrumentationTestCase2?
public class OldSchoolTest extends ActivityInstrumentationTestCase2<ResourceListActivity> { public OldSchoolTest() { super(ResourceListActivity.class); } } -
More jUnit3 goodness • overriding setUp & tearDown • testPrefixedMethods()
& test_prefixedMethods() -
public class OldSchoolTest extends ActivityInstrumentationTestCase2<ResourceListActivity> { public OldSchoolTest() { super(ResourceListActivity.class);
} @Override protected void setUp() throws Exception { super.setUp(); //Omg. } @Override protected void tearDown() throws Exception { super.tearDown(); //Srsly?? } @SmallTest public void testMustHaveTheTestPrefix(){ fail("No."); } @SmallTest public void test_canHaveUnderscoreInPrefixAsWell(){ fail("Just no."); } } -
Welcome to the future • jUnit4 syntax • No more
extending • @Test, @Before, @After, @AfterClass… • ActivityTestRule, InstrumentationRegistry -
@RunWith(AndroidJUnit4.class) public class ModernTest { @Rule ActivityTestRule<ResourceListActivity> activityRule =
new ActivityTestRule<>(ResourceListActivity.class); @Before public void setupMocks(){} @After public void clearMocks(){} @Test public void anyNameYouWish(){ //Yay } } -
@RunWith(AndroidJUnit4.class) public class ModernTest { @Rule ActivityTestRule<ResourceListActivity> activityRule =
new ActivityTestRule<>(ResourceListActivity.class); @Before public void setupMocks(){} @After public void clearMocks(){} @Test public void anyNameYouWish(){ //Yay } } -
@RunWith(AndroidJUnit4.class) public class ModernTest { @Rule ActivityTestRule<ResourceListActivity> activityRule =
new ActivityTestRule<>(ResourceListActivity.class); @Before public void setupMocks(){} @After public void clearMocks(){} @Test public void anyNameYouWish(){ //Yay } } -
What else? • Espresso • More Espresso - there’s a
lot to it • UIAutomator • … -
defaultConfig { applicationId "com.zmarkan.testingresources" minSdkVersion 23 targetSdkVersion 23 versionCode 1
versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } -
Gradle • Set your test runner to be AndroidJUnitRunner •
Add dependencies -
dependencies { compile 'com.android.support:appcompat-v7:23.2.1' compile 'com.android.support:design:23.2.1' //Unit testing
testCompile 'junit:junit:4.12' // JUnit4 Rules androidTestCompile 'com.android.support.test:rules:0.5' // Android JUnit Runner androidTestCompile 'com.android.support.test:runner:0.5' // Espresso core androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1' -
Gradle • Set your test runner to be AndroidJUnitRunner •
Add dependencies • Voila! -
Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.2.1) and
test app (23.1.1) differ. See http://g.co/ androidstudio/app-test-app-conflict for details. -
Gradle • Set your test runner to be AndroidJUnitRunner •
Add dependencies • Voila! • Resolve dependencies -
Dependency resolution • App & Test app depend on different
lib versions • Run ./gradlew :app:dependencies -
Dependency resolution • App & Test app depend on different
lib versions • Run ./gradlew :app:dependencies • Resolve with: • exclude dependency (everywhere applicable) • use ResolutionStrategy -
Gradle • Set your test runner to be AndroidJUnitRunner •
Add dependencies • Resolve dependencies • Voila! -
Espresso - components • Android view interactions & assertions •
Hamcrest syntax • No waits -
Espresso - contrib • RecyclerView • Drawers • Pickers •
Accessibility -
Espresso++ • Custom matchers • Idling Resources • Page objects
• Intent mocking -
Custom Matchers • Find a view in the hierarchy •
Good for custom views & Android components • Override: • describeTo • matchesSafely -
interface MyView{ boolean hasCustomProperty(); } static BoundedMatcher withCustomPropery() { return
new BoundedMatcher<Object, MyView>(MyView.class){ @Override public void describeTo(Description description) { description.appendText("Custom property is enabled"); } @Override protected boolean matchesSafely(MyView item) { return item.hasCustomProperty(); } }; } Custom Matchers -
Custom IdlingResource • A better way to wait • Use
when you have stuff going on in the background • Methods to override: • getName • isIdleNow • registerIdleTransitionCallback -
public class CustomIdlingResource implements IdlingResource{ private ResourceCallback resourceCallback; private
EventBus bus; private boolean loadingCompleted = false; public CustomIdlingResource(EventBus bus){ bus.register(this); } public void onEvent(LoadingCompletedEvent event){ loadingCompleted = true; isIdleNow(); } Custom IdlingResource -
@Override public String getName() { return CustomIdlingResource.class.getName(); } @Override
public void registerIdleTransitionCallback( ResourceCallback resourceCallback) { this.resourceCallback = resourceCallback; } @Override public boolean isIdleNow() { boolean idle = loadingCompleted; if (idle && resourceCallback != null) { resourceCallback.onTransitionToIdle(); bus.unregister(this); } return idle; } } Custom IdlingResource -
Page Object Pattern • Build your own DSL • Every
screen is a Page • Page-specific Assertion/Action methods • (as seen in Calabash, etc…) -
public class MainViewTestUtils { public void enterUserName(String text){ //
espresso goes here } public void enterPassword(String text){ // ... } public void pressContinue(){ // ... } public void assertErrorShown(boolean shown){ // ... } } Page Class -
@Test public void errorShownWhenPasswordIncorrect(){ MainViewTestUtils view = new MainViewTestUtils();
view.enterUsername(username); view.enterPassword(incorrectPassword); view.pressContinue(); view.assertErrorShown(true); } Page Class in Test -
UIAutomator • Interact with any installed app • Use to
create full end to end tests • Can coexist with Espresso in the same app • Use uiautomatorviewer to find items -
//Inside a test, press home and click on Google search.
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation(); device.pressHome(); device.findObject(new UiSelector().descriptionContains("Google search")).clickAndWaitForNewWindow(); Syntax -
Test Runner • Spins up tests and runs them •
Customise to prepare mocks • Easier run/debug via command line -
Extend the Runner • Use custom Application class • Provide
mocked dependencies • Specify this new runner in build.gradle -
public class CustomRunner extends AndroidJUnitRunner{ @Override public Application newApplication(ClassLoader cl,
String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException { return Instrumentation.newApplication(TestApplication.class, context); } } Extend the Runner -
Command Line-fu • Run module/package/class/method • Run Large/Medium/Small test only
• Shard to run in parallel • Debug without reinstalling -
“Simple” API • send commands to runner via adb (adb
shell [commands]) • am instrument -w -e class your.classname#methodName test.package.name/your.test.Runner.class • d.android.com/tools/testing/testing_otheride.html -
Test Rules • Set starting activity / Service • Replace
ActivityInstrumentationTestCase2 • (for most cases) • Add / Extend to create more components -
Rules - example • MockWebServerRule - sets up MockWebServer when
required • Source: github.com/artem-zinnatullin/qualitymatters -
Above & Beyond? • Espresso Web for WebViews • Running
on Google Device Lab • JankTestHelper -
W E ’ R E H I R I N
G ! mandsdigital.com/careers @mandsdigital -
• Twitter, Github, androidchat.co, /r/androiddev - zmarkan • www.spacecowboyrocketcompany.com •
zan at markan dot me • ATSL - the Nitty Gritty (the book) - in progress -
Resources • http://developer.android.com/tools/testing-support-library/index.html • https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex#6 • https://github.com/googlesamples/android-testing • http://artemzin.com/blog/android-development-culture-the-document-qualitymatters/ •
http://chiuki.github.io/advanced-android-espresso/#/ • https://developers.google.com/cloud-test-lab/