Espresso 简单的 UI 测试

UI 测试工具

目前主要用于 UI 测试的两个主要工具是 Appium 和 Espresso。

Appium Espresso
黑盒测试 白色/灰色盒子测试
你看到的是你可以测试的东西 可以更改应用程序的内部工作方式并准备进行测试,例如在运行测试之前将一些数据保存到数据库或共享首选项
主要用于集成端到端测试和整个用户流程 测试屏幕和/或流程的功能
可以抽象,因此可以在 iOS 和 Android 上执行测试编写 Android 仅限
很好的支持 很好的支持
支持使用 selenium 网格在多个设备上进行并行测试 没有开箱即用的并行测试,像 Spoon 这样的插件一直存在,直到真正的 Google 支持出现

如何将 espresso 添加到项目中

dependencies {
  // Set this dependency so you can use Android JUnit Runner
  androidTestCompile 'com.android.support.test:runner:0.5'
  // Set this dependency to use JUnit 4 rules
  androidTestCompile 'com.android.support.test:rules:0.5'
  // Set this dependency to build and run Espresso tests
  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
  // Set this dependency to build and run UI Automator tests
  androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.2.2'
}

注意如果你使用的是最新的支持库,注释等,则需要从 espresso 中排除旧版本以避免冲突:

    // there is a conflict with the test support library (see http://stackoverflow.com/questions/29857695)
    // so for now re exclude the support-annotations dependency from here to avoid clashes
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
        exclude module: 'support-v4'
        exclude module: 'support-v7'
    }
    // exclude a couple of more modules here because of <http://stackoverflow.com/questions/29216327> and
    // more specifically of <https://code.google.com/p/android-test-kit/issues/detail?id=139>
    // otherwise you'll receive weird crashes on devices and dex exceptions on emulators
    // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'design'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
        exclude module: 'support-v4'
        exclude module: 'support-v7'
    }
    //excluded specific packages due to https://code.google.com/p/android/issues/detail?id=183454
    androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
        exclude module: 'support-v4'
        exclude module: 'support-v7'
    }

    androidTestCompile('com.android.support.test.espresso:espresso-web:2.2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
        exclude module: 'support-v4'
        exclude module: 'support-v7'
    }

    androidTestCompile('com.android.support.test:runner:0.5') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
        exclude module: 'support-v4'
        exclude module: 'support-v7'
    }
    androidTestCompile('com.android.support.test:rules:0.5') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
        exclude module: 'support-v4'
        exclude module: 'support-v7'
    }

除了这些导入之外,还需要将 android instrumentation test runner 添加到 build.gradle android.defaultConfig:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

设备设置

对于非片状测试,建议在你的设备上设置以下设置:

  • 开发人员选项/禁用动画 - 减少测试的不稳定性
  • 开发人员选项/保持清醒 - 如果你有专门的测试设备,这是有用的
  • 开发人员选项/记录器缓冲区大小 - 如果你在手机上运行非常大的测试套件,则设置为更高的数字
  • 辅助功能/触摸和保持延迟 - 长时间以避免在 Espresso 中轻敲问题

相当于现实世界的设置 ha?那么现在,当这些让我们看看如何设置一个小测试

写测试

让我们假设我们有以下屏幕: StackOverflow 文档 屏幕包含:

  • 文本输入字段 - R.id.textEntry
  • 单击时显示带有键入文本的快餐栏的按钮 - R.id.shownSnackbarBtn
  • snackbar 应该包含用户输入的文本 - android.support.design.R.id.snackbar_text

现在让我们创建一个测试流程的类:

/**
* Testing of the snackbar activity.
## /
@RunWith(AndroidJUnit4.class)
@LargeTest
public class SnackbarActivityTest{
    //espresso rule which tells which activity to start
    @Rule
    public final ActivityTestRule<SnackbarActivity> mActivityRule = 
        new ActivityTestRule<>(SnackbarActivity.class, true, false);

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        //just an example how tear down should cleanup after itself
        mDatabase.clear();
        mSharedPrefs.clear();
    }
    
    @Override
    public void setUp() throws Exception {
        super.setUp();
        //setting up your application, for example if you need to have a user in shared
        //preferences to stay logged in you can do that for all tests in your setup
        User mUser = new User();
        mUser.setToken("randomToken");
    }
    
    /
    *Test methods should always start with "testXYZ" and it is a good idea to 
    *name them after the intent what you want to test
    **/
    @Test
    public void testSnackbarIsShown() {
        //start our activity
        mActivityRule.launchActivity(null);
        //check is our text entry displayed and enter some text to it
        String textToType="new snackbar text";
        onView(withId(R.id.textEntry)).check(matches(isDisplayed()));
        onView(withId(R.id.textEntry)).perform(typeText(textToType));
        //click the button to show the snackbar
        onView(withId(R.id.shownSnackbarBtn)).perform(click());
        //assert that a view with snackbar_id with text which we typed and is displayed
        onView(allOf(withId(android.support.design.R.id.snackbar_text), 
        withText(textToType))) .check(matches(isDisplayed()));
    }
}

正如你所注意到的,你经常会注意到 3-4 件事:

onView(withXYZ) < - viewMatchers,你可以在屏幕上找到元素

执行(单击()) < - viewActions,你可以对先前找到的元素执行操作

check(matches(isDisplayed())) < - viewAssertions,检查你想要在你以前找到的屏幕上做什么

所有这些以及许多其他内容都可以在这里找到: https//google.github.io/android-testing-support-library/docs/espresso/cheatsheet/index.html

多数民众赞成,现在你可以通过右键单击类名/测试并选择运行测试或使用命令来运行测试:

./gradlew connectedFLAVORNAMEAndroidTest