android 测试

迭代测试流程

  • The two cycles associated with iterative, test-driven development

测试金字塔

Although the proportion of tests for each category can vary based on your app’s use cases, we generally recommend the following split among the categories:
70 percent small, 20 percent medium, and 10 percent large.

环境配置

app’s top-level build.gradle 添加 依赖库

1
2
3
4
5
6
7
8
9
10
11
dependencies {
    androidTestCompile 'com.android.support:support-annotations:24.0.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    // Optional -- Hamcrest library
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    // Optional -- UI testing with Espresso
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    // Optional -- UI testing with UI Automator
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

Caution: If your build configuration includes a compile dependency for the support-annotations library and an androidTestCompile dependency for the espresso-core library, your build might fail due to a dependency conflict. To resolve, update your dependency for espresso-core as follows:

1
2
3
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

To use JUnit 4 test classes, make sure to specify AndroidJUnitRunner as the default test instrumentation runner in your project by including the following setting in your app’s module-level build.gradle file:

1
2
3
4
5
android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

Firebase Test Lab

google的云测,每天免费使用5个设备
https://firebase.google.cn

JUnit4 Rules with Testing Support Library

ActivityTestRule

这个 rule 用来测试单个 Activity。

“被测Activity” 在所有 @Test 和 @before 方法之前启动;在 @After 方法结束后关闭。

测试代码中可使用 ActivityTestRule.getActivity() 获取“被测Activity”。

ServiceTestRule

This rule provides a simplified mechanism to start up and shut down your service before and after the duration of your test.

此 rule 不支持 IntentService。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@RunWith(AndroidJUnit4.class)
@MediumTest
public class MyServiceTest {
    @Rule
    public final ServiceTestRule mServiceRule = new ServiceTestRule();

    @Test
    public void testWithStartedService() {
        mServiceRule.startService(
            new Intent(InstrumentationRegistry.getTargetContext(),
            MyService.class));

        // Add your test code here.
    }

    @Test
    public void testWithBoundService() {
        IBinder binder = mServiceRule.bindService(
            new Intent(InstrumentationRegistry.getTargetContext(),
            MyService.class));
        MyService service = ((MyService.LocalBinder) binder).getService();
        assertTrue("Service didn't return true",
            service.doSomethingToReturnTrue());
    }
}

AndroidJUnitRunner

The AndroidJUnitRunner class is a JUnit test runner that lets you run JUnit 3- or JUnit 4-style test classes on Android devices, including those using the Espresso and UI Automator testing frameworks.

This class replaces the InstrumentationTestRunner class, which only supports JUnit 3 tests.