Android测试官方教程翻译(三)--构建设备单元测试

  • 翻译难有谬误,错误之处敬请指出。遇到不理解之处请对照官方英文资料。
  • mock object,mocking framework在前两篇博文中也多次出现,不知如何翻译,就翻译为模仿对象和模仿框架
  • 本篇中test suite有的地方翻译为测试套件,有的地方直接使用英文
  • 官方原文链接 Building Instrumented Unit Tests

设备单元测试

设备单元测试是运行在物理设备和模拟器上的测试,他们能够利用Android Framework APIs和supporting APIs,比如Android Testing Support Library。如果你的测试需要获取instrumentation信息(比如目标app的context)或者需要Android框架组件(比如Parcelable o或者SharedPreferences 对象)的实际实现,你应当创建设备单元测试。

使用设备单元测试也能够减少编写和维护模仿代码的工作量。你仍然能够使用模仿框架,如果你选择如此,来模拟任何依赖关系。

设置你的测试环境

在你的AndroidStudio工程中,你必须存储设备测试的源文件在路径 module-name/src/androidTest/java/ 下。在你创建新的工程时这个路径已经存在,并且包含了一个设备测试的例子。

在你开始之前,你应该下载 Android Testing Support Library Setup ,它提供了允许你对app快速构建和运行设备测试代码的APIs。Testing Support Library包含了一个JUnit 4 test runner 和功能UI测试的APIs(Espresso 和 UI Automator,链接见译文一)。

你还需要给你的工程配置Android测试依赖,以使用Testing Support Library提供的 test runner 和 规则(rule)。为了简化测试环境,你应当引入 Hamcrest 库,它能让你使用Hamcrest matcher APIs创建更加灵活的assertion。

在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 依赖和espresso-core 库的androidTestCompile 依赖,你的构建由于依赖冲突可能失败。为了解决问题,像如下方法更新espresso-core依赖。

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

创建设备单元测试类

你的设备单元测试类应当编写成如同JUnit 4测试类。学习更多创建JUnit 4测试类和使用assertion以及annotation,查看”创建本地单元测试类”(译文第二篇)。

为了创建一个设备JUnit 4测试类,在测试类定义起始处添加注释@RunWith(AndroidJUnit4.class)。你还需要指定Android Testing Support Library 提供的 AndroidJUnitRunner为默认test runner。这一步骤在Getting Started with Testing(译文第一篇)中有详尽描述。

下面示例展示了你如何编写一个设备单元测试来测试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));
    }
}

创建测试套件

为了组织设备单元测试的执行,你可以在一个test suite类中分组测试类,一起运行这些测试类。

一个test suite包含在一个测试包中,类似于主应用包。按照惯例,test suite的包名经常以 .suite的后缀结尾(比如com.example.android.testing.mysample.suite)。
为单元测试创建test suite,import JUnit的 RunWith和Suite类。在你的test suite中,添加 @RunWith(Suite.class)@Suite.SuitClasses() 注释。在@Suite.SuitClasses() 中,列出单独的测试类或者test suite 作为参数。

以下示例展示了你如何实现一个分组并且CalculatorInstrumentationTestCalculatorAddParameterizedTest 测试类一起运行的类名为UnitTestSuite的test suite。

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

运行设备单元测试

运行设备单元测试,遵循以下步骤。

  1. 点击工具栏的Sync Project 这里写图片描述 ,确保工程和gradle同步。
  2. 用以下方式之一运行测试
    • 运行单一测试,打开Project 窗口,然后右键点击一个测试并且点击Run这里写图片描述
    • 测试一个类中的所有方法,右键点击测试文件的一个类或者方法,然后点击Run 这里写图片描述
    • 运行一个路径下的所有测试,右键点击该路径,然后选择Run Tests 这里写图片描述

Android的gradle插件编译位于缺省路径(src/androidTest/java/)的设备测试代码,构建一个测试APK和产物APK(production APK),将两个APK安装到连接的设备或者模拟器上,然后运行测试。Android Studio 然后在Run 窗口显示设备测试执行的结果。

当运行或者debugging设备测试,Android Studio 并不注入 Instant Run需要的额外方法和关闭特性。

使用Firebase Test Lab运行测试

使用Firebase Test Lab ,你可以在许多流行设备和设备配置(地区、屏幕方向、屏幕尺寸和平台版本)上同时测试App。这些测试运行在远端google数据中心的物理和虚拟设备上。你可以通过Android Studio 或者命令行直接部署app到Test Lab。测试结果提供了测试日志,并且包括任何App失败的详情。

在你开始使用 Firebase Test Lab之前,你需要依照如下操作,除非你已经有了Google账户和一个Firebase 工程。

  1. 如果你没有账户,创建一个Google账户
  2. Firebase控制台 ,点击Create new project。
    Spark计划:每日免费配额 之内,使用Test Lab测试App免费。

PS: 这一部分没翻译完,所剩不多,考虑到国内无法使用google服务/收费/我没这个需求,不在翻译剩余部分。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值