JUnit for android
-
Upload
baroqueworksdev -
Category
Engineering
-
view
42 -
download
1
Transcript of JUnit for android
JUnit for Android@nory_kaname
● MUST do TDD● Android has Test Framework● We'll use "Android Testing Support Library"
○ test helper libary
まとめ
Note : "MUST": RFC2119
Android Test Policy
Debugging and testing in Android Studio (Ep 4, Android Studio)
https://youtu.be/2I6fuD20qlY
Test driven development
Test FirstTest MUST run on Master branch!!
Test fundamental in Android
Android Test frameworks
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
Learn JUnit
● Recommend Books
UI Testing
Tools● Android Studio● Espresso(android-test-kit, for Google)
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
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
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!!
Assertion
onView(withId(R.id.button)).perform(click())check(matches(isDisplayed()));
Select view -> perform -> check
ViewAction
ViewMatcher
ViewAssertion
Esprsso CheatSheet
● onView()○ pick up view object
● onData()○ pick up data(for List)
● intended,intending○ check Intent Data
We can use many Matcher methods.
not enough method...
We want to check ... ● inRoot
○ pick up view on the other Window● Check
○ text color○ hint text
● Other○ wait for display
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.
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)
● MUST do TDD● Android has Test Framework● We use "Android Testing Support Library"
○ test helper libary● Make Matchers.
Note : "MUST": RFC2119
Thank you !!