Slide 1

Slide 1 text

Android Testing

Slide 2

Slide 2 text

Unit Tests small scoped and isolated

Slide 3

Slide 3 text

public static boolean isNullOrEmpty(String text) { return TextUtils.isEmpty(text) || TextUtils.getTrimmedLength(text) == 0; }

Slide 4

Slide 4 text

JUnit http://junit.org

Slide 5

Slide 5 text

public class UtilsTest extends TestCase { public void testIsNullOrEmpty() { assertEquals(isNullOrEmpty(null), true); assertEquals(isNullOrEmpty(""), true); assertEquals(isNullOrEmpty(" "), true); assertEquals(isNullOrEmpty("foo"), false); } }

Slide 6

Slide 6 text

public class UtilsTest { @Test public void isNullOrEmpty() { assertEquals(isNullOrEmpty(null), true); assertEquals(isNullOrEmpty(""), true); assertEquals(isNullOrEmpty(" "), true); assertEquals(isNullOrEmpty("foo"), false); } }

Slide 7

Slide 7 text

java.lang.RuntimeException: Stub!

Slide 8

Slide 8 text

public class TextUtils { ... public static boolean isEmpty(CharSequence cs) { throw new RuntimeException("Stub!"); } public static int getTrimmedLength(CharSequence cs) { throw new RuntimeException("Stub!"); } }

Slide 9

Slide 9 text

Android Testing Framework https://developer.android.com/tools/testing/ testing_android.html

Slide 10

Slide 10 text

public class UtilsTest extends TestCase { public void testIsNullOrEmpty() { assertEquals(isNullOrEmpty(null), true); assertEquals(isNullOrEmpty(""), true); assertEquals(isNullOrEmpty(" "), true); assertEquals(isNullOrEmpty("foo"), false); } } http://developer.android.com/reference/junit/framework/ TestCase.html

Slide 11

Slide 11 text

Slow!

Slide 12

Slide 12 text

Can’t Test Outside Device

Slide 13

Slide 13 text

Can’t Test Outside Device

Slide 14

Slide 14 text

Java Module

Slide 15

Slide 15 text

public static boolean isNullOrEmpty(String text) { return text == null || text.trim().length() == 0; }

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

settings.gradle include 'library' include 'app' app/build.gradle dependencies { compile(':library') }

Slide 18

Slide 18 text

Robolectric http://robolectric.org/

Slide 19

Slide 19 text

@RunWith(RobolectricTestRunner.class) public class UtilsTest { @Test public void isNullOrEmpty() { assertEquals(isNullOrEmpty(null), true); assertEquals(isNullOrEmpty(""), true); assertEquals(isNullOrEmpty(" "), true); assertEquals(isNullOrEmpty("foo"), false); } }

Slide 20

Slide 20 text

@RunWith(RobolectricTestRunner.class) public class UtilsTest { @Test public void isNullOrEmpty() { assertEquals(isNullOrEmpty(null), true); assertEquals(isNullOrEmpty(""), true); assertEquals(isNullOrEmpty(" "), true); assertEquals(isNullOrEmpty("foo"), false); } }

Slide 21

Slide 21 text

Robolectric + Gradle https://github.com/segmentio/analytics-android/blob/2.5.1/core- tests/build.gradle#L46

Slide 22

Slide 22 text

Instrumentation Tests larger scoped, black box testing Espresso, Spoon, Wiremock, Oh my! - Michael Bailey https://www.youtube.com/watch?v=-xQCNf_5NNM

Slide 23

Slide 23 text

public class LoginActivity extends Activity { @InjectView(R.id.username) EditText username; @InjectView(R.id.password) EditText password; @OnClick(R.id.login) void onLoginClicked() { boolean hasError = false; if (TextUtils.isEmpty(username.getText())) { username.setError(getString(R.string.required)); hasError = true; } else { username.setError(null); } if (TextUtils.isEmpty(password.getText())) { password.setError(getString(R.string.required)); hasError = true; } else { password.setError(null); } if (!hasError) { Intent intent = new Intent(LoginActivity.this, OrderActivity.class); intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); } } }

Slide 24

Slide 24 text

Instrumentation API https://developer.android.com/reference/android/app/ Instrumentation.html

Slide 25

Slide 25 text

public void testEmptyPassword_ShowsError() { // Make sure the initial state does not show any errors. assertEquals(username.getError(), null); assertEquals(password.getError(), null); instrumentation.runOnMainSync(new Runnable() { @Override public void run() { // Type a value into the username and password fields. username.setText("elvis"); password.setText(""); // Click the "login" button. login.performClick(); } }); instrumentation.waitForIdleSync(); // Verify error was shown only for password field. assertEquals(username.getError(), null); assertEquals(password.getError(), getString(R.string.required)); }

Slide 26

Slide 26 text

public void testEmptyPassword_ShowsError() { // Make sure the initial state does not show any errors. assertEquals(username.getError(), null); assertEquals(password.getError(), null); instrumentation.runOnMainSync(new Runnable() { @Override public void run() { // Type a value into the username and password fields. username.setText("elvis"); password.setText(""); // Click the "login" button. login.performClick(); } }); instrumentation.waitForIdleSync(); // Verify error was shown only for password field. assertEquals(username.getError(), null); assertEquals(password.getError(), getString(R.string.required)); }

Slide 27

Slide 27 text

assertEquals(password.getError(), null); assertEquals(password.getVisibility(), VISIBLE); assertEquals(password.getText(), null); assertEquals(password.getHint(), getString(R.string.password));

Slide 28

Slide 28 text

expected: <8> but was: <4>

Slide 29

Slide 29 text

AssertJ-Android http://square.github.io/assertj-android/

Slide 30

Slide 30 text

assertThat(username) .hasNoError() .isVisible() .isEmpty() .hasHint(R.string.hint);

Slide 31

Slide 31 text

Expected visibility but was .

Slide 32

Slide 32 text

Robotium https://code.google.com/p/robotium/

Slide 33

Slide 33 text

public void testEmptyPassword_ShowsError() { // Make sure the initial state does not show any errors. assertThat(username).hasNoError(); assertThat(password).hasNoError(); // Type a value into the username and password fields. solo.typeText(username, "elvis"); solo.typeText(password, ""); // Click the "login" button. solo.clickOnView(login); // Verify error was shown only for username field. assertThat(username).hasNoError(); assertThat(password).hasError(R.string.required); }

Slide 34

Slide 34 text

Espresso https://code.google.com/p/android-test-kit/

Slide 35

Slide 35 text

public void testEmptyPassword_ShowsError() { // Make sure the initial state does not show any errors. onView(withId(R.id.username)).check(matches(hasNoError())); onView(withId(R.id.password)).check(matches(hasNoError())); // Type a value into the username and password fields. onView(withId(R.id.username)).perform(typeText("elvis")); onView(withId(R.id.password)).perform(typeText("")); // Click the "login" button. onView(withId(R.id.login)).perform(click()); // Verify error was shown only for username field. onView(withId(R.id.password)).check(matches(hasNoError())); onView(withId(R.id.password)) .check(matches(hasError(R.string.required))); } https://code.google.com/p/android-test-kit/wiki/ EspressoV2CheatSheet

Slide 36

Slide 36 text

Spoon https://github.com/square/spoon http://square.github.io/spoon/sample/index.html

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

public void testEmptyPassword_ShowsError() { Spoon.screenshot(activity, "initial_state"); // Make sure the initial state does not show any errors. onView(withId(R.id.username)).check(matches(hasNoError())); onView(withId(R.id.password)).check(matches(hasNoError())); // Type a value into the username and password fields. onView(withId(R.id.username)).perform(typeText("elvis")); onView(withId(R.id.password)).perform(typeText("")); Spoon.screenshot(activity, "typed_text"); // Click the "login" button. onView(withId(R.id.login)).perform(click()); Spoon.screenshot(activity, "login_clicked"); // Verify error was shown only for username field. onView(withId(R.id.password)).check(matches(hasNoError())); onView(withId(R.id.password)) .check(matches(hasError(R.string.required))); }

Slide 39

Slide 39 text

public void testEmptyPassword_ShowsError() { Spoon.screenshot(activity, "initial_state"); // Make sure the initial state does not show any errors. onView(withId(R.id.username)).check(matches(hasNoError())); onView(withId(R.id.password)).check(matches(hasNoError())); // Type a value into the username and password fields. onView(withId(R.id.username)).perform(typeText("elvis")); onView(withId(R.id.password)).perform(typeText("")); Spoon.screenshot(activity, "typed_text"); // Click the "login" button. onView(withId(R.id.login)).perform(click()); Spoon.screenshot(activity, "login_clicked"); // Verify error was shown only for username field. onView(withId(R.id.password)).check(matches(hasNoError())); onView(withId(R.id.password)) .check(matches(hasError(R.string.required))); }

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Fork https://github.com/shazam/fork

Slide 42

Slide 42 text

Mocks script responses, verify interactions http://martinfowler.com/articles/mocksArentStubs.html

Slide 43

Slide 43 text

Mockito http://mockito.org https://corner.squareup.com/2012/10/mockito-android.html

Slide 44

Slide 44 text

analytics.identify("elvis", new Traits() .putFirstName("Elvis") .putLastName("Presley") .putEmail("[email protected]"));

Slide 45

Slide 45 text

class MixpanelAdapter implements Adapter { final MixpanelAPI.People people; … void identify(IdentifyMessage message) { JSONObject json = new JSONObject(); json.put("$email", message.email()); json.put("$first_name", message.firstName()); json.put("$last_name", message.lastName()); people.identify(message.userId()); people.set(json); } }

Slide 46

Slide 46 text

@Test public void identify() throws JSONException { // 1. Prepare our mocks MixpanelAPI.People people = Mockito.mock(MixpanelAPI.People.class); MixpanelAdapter adapter = mockAdapter(people); Analytics analytics = createAnalyticsWithMock(adapter); // 2. Exercise the test analytics.identify("elvis", new Traits() .putFirstName("Elvis") .putLastName("Presley") .putEmail("[email protected]")); JSONObject expected = new JSONObject(); expected.put("userId", "elvis"); expected.put("$email", "[email protected]"); expected.put("$first_name", "Elvis"); expected.put("$last_name", "Presley"); // 3. Verify that we saw what we expected Mockito.verify(people).identify("elvis"); Mockito.verify(people).set(eq(expected)); }

Slide 47

Slide 47 text

PowerMockito https://code.google.com/p/powermock/

Slide 48

Slide 48 text

class LocalyticsAdapter implements Adapter { void identify(IdentifyMessage message) { Localytics.setCustomerId(message.userId()); Localytics.setIdentifier("email", message.email()); Localytics.setIdentifier("customer_name", message.name()); } }

Slide 49

Slide 49 text

@Test public void identify() { // 1. Prepare mocks. PowerMockito.mockStatic(Localytics.class); Analytics analytics = createAnalytics(); // 2. Test the code of interest. analytics.identify("elvis", new Traits() .putFirstName("Elvis") .putLastName("Presley") .putEmail("[email protected]")); // 3. Validate that we saw exactly what we wanted. PowerMockito.verifyStatic(); Localytics.setCustomerId("elvis"); PowerMockito.verifyStatic(); Localytics.setIdentifier("email", "[email protected]"); PowerMockito.verifyStatic(); Localytics.setIdentifier("customer_name", "Elvis Presley"); }

Slide 50

Slide 50 text

MockWebServer https://github.com/square/okhttp/tree/master/mockwebserver

Slide 51

Slide 51 text

public void test() throws Exception { // Create a MockWebServer. MockWebServer server = new MockWebServer(); // Schedule some responses. server.enqueue(new MockResponse().setBody("hello, world!")); // Start the server. server.start(); // Ask the server for its URL. You'll need this to make HTTP requests. URL baseUrl = server.getUrl(“/v1/chat/“); // Exercise your application code, which makes those HTTP requests. // Responses are returned in the same order that they are enqueued. Chat chat = new Chat(baseUrl); chat.loadMore(); assertThat(chat.messages()).containsExactly("hello, world!"); // Confirm that your app made the HTTP requests you were expecting. RecordedRequest request1 = server.takeRequest(); assertEquals("/v1/chat/messages/", request1.getPath()); assertThat(request1.getHeader("Authorization")).isNotNull(); // Shut down the server. server.shutdown(); }

Slide 52

Slide 52 text

WireMock http://wiremock.org

Slide 53

Slide 53 text

Monkey http://developer.android.com/tools/help/monkey.html

Slide 54

Slide 54 text

adb shell monkey -p your.package.name -v 500

Slide 55

Slide 55 text

Caliper https://code.google.com/p/caliper/ https://code.google.com/p/caliper/wiki/CaliperOnAndroid

Slide 56

Slide 56 text

public class MyBenchmark extends Benchmark { public void timeMyOperation(int reps) { for (int i = 0; i < reps; i++) { MyClass.myOperation(); } } }

Slide 57

Slide 57 text

Vogar https://code.google.com/p/vogar/ https://android.googlesource.com/platform/external/vogar/

Slide 58

Slide 58 text

Vogar JAR : https://www.dropbox.com/s/ddjv2slngzon78r/vogar.jar?dl=0 Sample Script and Benchmarks: https://github.com/segmentio/cartographer/blob/master/ run_vogar.sh

Slide 59

Slide 59 text

java -jar vogar.jar --benchmark --verbose --mode device --sourcepath …

Slide 60

Slide 60 text

Questions? @f2prateek