18
JUnit for Android @nory_kaname

JUnit for android

Embed Size (px)

Citation preview

Page 1: JUnit for android

JUnit for Android@nory_kaname

Page 2: JUnit for android

● MUST do TDD● Android has Test Framework● We'll use "Android Testing Support Library"

○ test helper libary

まとめ

Note : "MUST": RFC2119

Page 3: JUnit for android

Android Test Policy

Debugging and testing in Android Studio (Ep 4, Android Studio)

https://youtu.be/2I6fuD20qlY

Page 4: JUnit for android

Test driven development

Test FirstTest MUST run on Master branch!!

Page 5: JUnit for android

Test fundamental in Android

Android Test frameworks

Page 6: JUnit for android

2 type of JUnit

● Normal JUnit (3.0 , 4.0)○ running on JVM○ Java class , module test

● Android JUnit (3.0 , 4.0)○ running on Adroid Device(Emulator)○ UI Testing

Page 7: JUnit for android

Learn JUnit

● Recommend Books

Page 8: JUnit for android

UI Testing

Tools● Android Studio● Espresso(android-test-kit, for Google)

Page 9: JUnit for android

Layout file

Layout file to configure the screen.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/text_view" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/></RelativeLayout>

Widget ID

Page 10: JUnit for android

Old UI Testing

@MediumTestpublic void testClickMeButton_clickButtonAndExpectInfoText() { // get Object Button mClickMeButton= (Button)mClickFunActivity.findViewById(R.id.button); TextView mInfoTextView= (Button)mClickFunActivity.findViewById(R.id.text); // expected text String expectedInfoText = mClickFunActivity.getString(R.string.info_text); // click button TouchUtils.clickView(this, mClickMeButton);

// check assertTrue(View.VISIBLE == mInfoTextView.getVisibility()); assertEquals(expectedInfoText, mInfoTextView.getText());}

Test code w/o Espresso● test for text object ,if click button

Page 11: JUnit for android

UI Testing w/ Espresso

@MediumTestpublic void testClickMeButton_clickButtonAndExpectInfoText() { // click button onView(withId(R.id.button)).perform(click());

// check onView(withId(R.id.text)).check(matches(isDisplayed(),

withText(R.string.info_text)));}

Test code w/ Espresso● test for text object ,if click button

Only 2 Lines !! OMG!!

Page 12: JUnit for android

Assertion

onView(withId(R.id.button)).perform(click())check(matches(isDisplayed()));

Select view -> perform -> check

ViewAction

ViewMatcher

ViewAssertion

Page 13: JUnit for android

Esprsso CheatSheet

● onView()○ pick up view object

● onData()○ pick up data(for List)

● intended,intending○ check Intent Data

We can use many Matcher methods.

Page 14: JUnit for android

not enough method...

We want to check ... ● inRoot

○ pick up view on the other Window● Check

○ text color○ hint text

● Other○ wait for display

Page 15: JUnit for android

Original Matcher method

public static Matcher<View> withTextHintColor(final int resourceId) { return new BoundedMatcher<View, TextView>(TextView.class) {

:::

}; }

We can make matcher method which check test.

Page 16: JUnit for android

ex) Toast Window

public static Matcher<Root> isToast() { return new TypeSafeMatcher<Root>() {

@Override public boolean matchesSafely(Root root) { int type = root.getWindowLayoutParams().get().type; if ((type == WindowManager.LayoutParams.TYPE_TOAST)) { IBinder windowToken = root.getDecorView().getWindowToken(); IBinder appToken = root.getDecorView().getApplicationWindowToken(); if (windowToken == appToken) { // windowToken == appToken means this window isn't contained by any other windows. // if it was a window for an activity, it would have TYPE_BASE_APPLICATION. return true; } } return false; } }; }

Toast window Matcher.(We have to know the Android Frameworks)

Page 17: JUnit for android

● MUST do TDD● Android has Test Framework● We use "Android Testing Support Library"

○ test helper libary● Make Matchers.

Note : "MUST": RFC2119

Page 18: JUnit for android

Thank you !!