发测试篇-android测试-android组件测试(翻译自android training官网)

官网链接

Building Instrumented Unit Tests

组件单元测试指的是: 在真实的设备或者模拟器上运行单元测试,这样可以利用android的framework APIs和supporting APIs,比如说: Android Testing Support Library.当你的测试需要访问组件信息(比如说目标app的Context)或者他们需要实现真实的Android framework 组件的信息(eg: Parcelable或者SharePreference)时,你应该使用组件的单元测试.

使用组件单元测试也会帮助减少你写和维护mock 代码的时间.你仍然可以使用mocking的framwork,如果你任性的话,来模拟任何的依赖关系.

Set Up Your Testing Environment

在android studio的工程中,你需要将组件测试的源文件放置到module-name/src/androidTest/java/中.在你创建工程师,你这个文件夹就已经存在,并且已经包含了了一个组件测试的案例.

在你开始之前,你需要下载Android Testing Support Library Setup,这些APIs中允许你快速的构建和运行组件测试代码.这个The Testing Support Library包含了JUnit的runner(AndroidJUnitRunner)和用于功能测试(ESpressoUI Automator)的APIs

同样,你还需要配置用于由Testing Support Library提供的工程测试的运行和规则的Android 测试的依赖.为了测试工作的简化,你同样可以依赖Hamcrest库,同归这个库的APIs,可以用来创建更多灵活的断言.

在App的顶层的build.gradle文件中,你需要制定以下的库作为依赖.

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'
}

注意: 如果在配置中包含了support-annotations的compile级别的库和androidTestCompile级别依赖的espresso-core的库,那么你在build是,可能会因为依赖冲突,导致build失败.解决办法: 更新你的espresso-core的依赖,如下操作:

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

要使用Junit4的测试类,确保在app的module级别的build.gradle文件中,指定使用的是AndroidJunitRunenr作为默认的测试组件.

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

Create an Instrumented Unit Test Class

你的组件测试类应该要写成Junit 4 测试的类.

为了创建一个Junit 4测试的类,在你的测试类的注解上添加@RunWith(AndroidJUnit4.class). 并且你需要指定由Android Testing Support Library提供的AndroidJUnitRunner作为默认的test runner.

接下来的这个案例,告诉你如何测试在LogHistory类中的Parcelable接口

import android.os.Parcel;
import android.support.test.runner.AndroidJUnit4;
import android.util.Pair;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class LogHistoryAndroidUnitTest {

    public static final String TEST_STRING = "This is a string";
    public static final long TEST_LONG = 12345678L;
    private LogHistory mLogHistory;

    @Before
    public void createLogHistory() {
        mLogHistory = new LogHistory();
    }

    @Test
    public void logHistory_ParcelableWriteRead() {
        // Set up the Parcelable object to send and receive.
        mLogHistory.addEntry(TEST_STRING, TEST_LONG);

        // Write the data.
        Parcel parcel = Parcel.obtain();
        mLogHistory.writeToParcel(parcel, mLogHistory.describeContents());

        // After you're done with writing, you need to reset the parcel for reading.
        parcel.setDataPosition(0);

        // Read the data.
        LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);
        List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData();

        // Verify that the received data is correct.
        assertThat(createdFromParcelData.size(), is(1));
        assertThat(createdFromParcelData.get(0).first, is(TEST_STRING));
        assertThat(createdFromParcelData.get(0).second, is(TEST_LONG));
    }
}   

Create a test suite

为了组织组件单元测试的运行,你可以将几个测试类放到同一个测试的组件中并且将他们一起运行.这些这是组件可以是交叉的, 你的测试组件可以组织其他的测试组件,并且把他一起运行测试.

一个测试组件包含了一个测试的package中,像主应用的package一样.惯例上,会将测试组件的包以.suffix来结尾(个eg.com.example.android.testing.mysample.suite)

创建自己的单元测试时,需要引入JUnit的RunWithSuit类.在你的测试组件中,需要添加的是* @RunWith(Suite.class) @Suite.SuitClasses() 注解.在@Suite.SuiteClasses() *注解的括号里面,列出单独的测试类或者测试组件

下面的案例说明了如何实现一个UnitTestSuite的测试组件,包含了CalculatorInstrumentationTestCalculatorAddParameterizedTest两个测试类.

import com.example.android.testing.mysample.CalculatorAddParameterizedTest;
import com.example.android.testing.mysample.CalculatorInstrumentationTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

// Runs all unit tests.
@RunWith(Suite.class)
@Suite.SuiteClasses({CalculatorInstrumentationTest.class,
        CalculatorAddParameterizedTest.class})
public class UnitTestSuite {}

Run Instrumented Unit Tests

为了运行组件测试,需要以下的几个步骤:

  1. Gradle已经做了Sync Project**
  2. 有以下的集中方式来运行你的测试

    • 单个测试,打开Project的窗口,右键你的测试,并且点击Run
    • 测试类中的所有方法,在文件中右键这个类或者方法,并且点击Run
    • 测试一个目录中的所有测试,右键这个目录,并且选择Run tests

Android的gradle插件编译的组件测试代码位于默认的目录src/androidTest/java/,构建一个测试的apk和production apk, 同时安装这个两个app在连接的设备或者模拟器上,运行测试.Android Studio会在Run window中展示这个组件测试的结果.

为了运行或者debug组件测试,android studio并没有给Instant Run额外需要的方法注入,这个特性是关闭的.

Run your tests with Firebase Test Lab

###

Firebase Test Lab 大家自己可以查一下哈

posted @ 2016-12-04 18:05  Panda Pan  阅读(2)  评论(0编辑  收藏  举报