如上图,我们可以创建新的项目,导入已经存在android项目,从版本管理软件中导入,从gradle等其他工具中导入,以及获取android代码样例导入。 google android 所有的测试demo都在以下github地址 https://github.com/googlesamples/android-testing.git 下载完成后,我们进入espresso目录,可以看到espresso的demo还是很丰富的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# lqi @ CNlqi-3 in ~/work/test/android/android-testing on git:master x [0:19:24] $ cd ui/espresso
# lqi @ CNlqi-3 in ~/work/test/android/android-testing/ui/espresso on git:master x [0:19:31] $ ls BasicSample CustomMatcherSample EspressoSpoonDemo IntentsAdvancedSample MultiWindowSample WebBasicSample BasicSampleBundled DataAdapterSample IdlingResourceSample IntentsBasicSample RecyclerViewSample spoon-gradle-plugin
# lqi @ CNlqi-3 in ~/work/test/android/android-testing/ui/espresso on git:master x [0:19:37] $ cd BasicSample
# lqi @ CNlqi-3 in ~/work/test/android/android-testing/ui/espresso/BasicSample on git:master x [0:19:40] $ pwd /Users/lqi/work/test/android/android-testing/ui/espresso/BasicSample
# lqi @ CNlqi-3 in ~/work/test/android/android-testing/ui/espresso/BasicSample on git:master x [0:20:42] $
dependencies { // App dependencies compile'com.android.support:support-annotations:' + rootProject.supportLibVersion; compile'com.google.guava:guava:18.0' // Testing-only dependencies // Force usage of support annotations in the test app, since it is internally used by the runner module. androidTestCompile 'com.android.support:support-annotations:' + rootProject.supportLibVersion; androidTestCompile 'com.android.support.test:runner:' + rootProject.runnerVersion; androidTestCompile 'com.android.support.test:rules:' + rootProject.rulesVersion; androidTestCompile 'com.android.support.test.espresso:espresso-core:' + rootProject.espressoVersion; }
$ adb shell am instrument -w -r -e debug false -e class com.example.android.testing.espresso.BasicSample.ChangeTextBehaviorTest com.example.android.testing.espresso.BasicSample.test/android.support.test.runner.AndroidJUnitRunner Client not ready yet.. Started running tests Tests ran to completion.
编写测试
此实例BasicSample包含1个textView,1个EditText和2个button,当点击change text 按钮时,会将edittext的值填入textview中。当点击open activity and change text 按钮时,将打开一个新的页面(姑且叫这样吧)并将edittext内容显示在这个页面。 @RunWith(AndroidJUnit4.class) 采用了JUnit 4风格进行编写
@Test public void changeText_sameActivity() { // Type text and then press the button. onView(withId(R.id.editTextUserInput)) .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); onView(withId(R.id.changeTextBt)).perform(click());
// Check that the text was changed. onView(withId(R.id.textToBeChanged)).check(matches(withText(STRING_TO_BE_TYPED))); }
@Test public void changeText_newActivity() { // Type text and then press the button. onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); onView(withId(R.id.activityChangeTextBtn)).perform(click());
// This view is in a different Activity, no need to tell Espresso. onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED))); }
@Test public void autoCompleteTextView_onDataClickAndCheck() { // NB: The autocompletion box is implemented with a ListView, so the preferred way // to interact with it is onData(). We can use inRoot here too! onView(withId(R.id.auto_complete_text_view)) .perform(typeText("S"), closeSoftKeyboard());
// This is useful because some of the completions may not be part of the View Hierarchy // unless you scroll around the list. onData(allOf(instanceOf(String.class), is("Baltic Sea"))) .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))) .perform(click());
// The text should be filled in. onView(withId(R.id.auto_complete_text_view)) .check(matches(withText("Baltic Sea"))); }