原文:https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

Instrumented 单元测试是在真机和模拟器上运行的测试,它可以利用Android框架API和支持的API(如Android测试支持库)。如果你的测试需要访问工具信息(例如目标应用程序的Context),或者需要真正实现Android框架组件(如ParcelableSharedPreferences对象),则应该创建Instrumented 单元测试。

使用Instrumented单元测试还有助于减少编写和维护mock代码所需的工作量。 如果你愿意,仍然可以自由地使用一个mock框架模拟任何依赖关系。

### 设置测试环境

在你的Android Studio项目中,你必须将mock测试的源文件存储在module-name/src/androidTest/java/ 中。 创建新项目时该目录已经存在,并包含示例代码。

在开始之前,你应该下载Android测试支持库安装程序,该安装程序提供的API可让你快速构建和运行应用程序的检测代码。测试支持库包括用于功能性UI测试(Espresso和UI Automator)的JUnit 4测试运行器(AndroidJUnitRunner)和API。

还需要为项目配置Android测试依赖项,以使用测试运行程序和测试支持库提供的规则API。 为了简化测试开发,还应该包含Hamcrest库,它可以让你使用Hamcrest匹配器API创建更灵活的断言。

在你的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库的编译依赖项和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测试类,请确保将AndroidJUnitRunner指定为项目中的默认测试工具运行器,方法是在应用程序的模块级build.gradle文件中包含以下设置:

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

### 创建一个Instrumented的单元测试类

你的Instrumented单元测试类应该写成JUnit 4测试类。要了解有关创建JUnit 4测试类和使用JUnit 4断言和注释的更多信息,请参阅创建本地单元测试类。

要创建一个Instrumented的JUnit 4测试类,在测试类定义的开头添加@RunWith(AndroidJUnit4.class)注释。 还需要将Android测试支持库中提供的AndroidJUnitRunner类指定为默认测试运行器。测试入门中对此步骤进行了更详细的介绍。

以下示例显示如何编写一个Instrumented单元测试,以确保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));
    }
}

### 创建一个测试套件

要组织测试单元测试的执行,可以将一组测试集合在一个测试套件类中,并将这些测试一起运行。测试套件可以嵌套; 测试套件可以将其他测试套件分组,并将所有组件测试类一起运行。

测试套件包含在测试包中,类似于主应用程序包。按照惯例,测试套件包名通常以.suite后缀结尾(例如,com.example.android.testing.mysample.suite)。

以下示例显示了如何实现名为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 {}

运行Instrumented单元测试


要运行Instrumented测试,请遵循以下步骤:

1、通过单击工具栏中的“Sync Project”,确保您的项目与Gradle同步。。

2、以下列其中一种方式运行测试:

  • 要运行单个测试请打开Project窗口,然后单击“Run”。
  • 要测试类中的所有方法,请右键单击测试文件中的类或方法,然后单击“Run”。
  • 要在目录中运行所有测试,请右键单击该目录并选择“Run Tests”。

Gradle的Android插件编译位于默认目录(src/androidTest/java/)中的测试代码,构建测试APK和生产APK,在连接的真机或模拟器上安装两个APK,并运行测试。Android Studio然后在“Run”窗口中显示测试执行结果。

注意:在运行或调试测试工具时,Android Studio不会为即时运行注入所需的额外方法,并关闭该特性。

### 使用Firebase测试实验室运行测试

略....

(请查看原文)

### 额外的示例代码

要下载到示例应用程序,请参阅Android ActivityInstrumentation Sample

Android测试(四):Instrumented 单元测试的更多相关文章

  1. Android测试:从零开始3—— Instrumented单元测试1

    Instrumented单元测试是指运行在物理机器或者模拟机上的测试,这样可以使用Android framework 的API和supporting API.这会在你需要使用设备信息,如app的Con ...

  2. 在Android Studio中进行单元测试和UI测试

    本篇教程翻译自Google I/O 2015中关于测试的codelab,掌握科学上网的同学请点击这里阅读:Unit and UI Testing in Android Studio.能力有限,如有翻译 ...

  3. Android测试:从零开始2——local单元测试

    上一篇分析了android项目的测试分类,这一篇讲local单元测试. 参考android官方文档. 测试前需要配置测试环境,新建项目后,目录下会出现app/src/test/java/文件夹,这个文 ...

  4. 【Android测试】【第十五节】Instrumentation——官方译文

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5482207.html 前言 前面介绍了不少Android ...

  5. 1、Android测试入门

    编写和运行测试时Android APP开发周期中的重要的一环.好的测试可以让你非常容易的在开发过程中发现bug,提升你对自己代码的自信.使用Android Studio,你可以在物理设备或者虚拟机中运 ...

  6. Android测试:从零开始1——简介

    参考文档:https://developer.android.com/training/testing/start/index.html 测试分类 使用android studio进行测试,首先需要先 ...

  7. [Android]Android MVP&依赖注入&单元测试

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5422443.html Android MVP&依赖注入 ...

  8. 【Android测试】【随笔】模拟长按电源键

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5195121.html 起因 昨天群里看到有人问如何实现一个 ...

  9. 【Android测试】【随笔】获得App的包名和启动页Activity

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5157308.html 前言 经常看到一些刚刚接触Andro ...

随机推荐

  1. [JavaScript] audio在浏览器中自动播放

    audio 在浏览器中自动播放 autoplay 属性 autoplay 属性规定一旦音频就绪马上开始播放. 如果设置了该属性,音频将自动播放. 使用 autoplay 属性进行播放 //使用auto ...

  2. Smobiler 4.0 正式发布

    l Smobiler4.0提供了三大技术亮点:第三方插件.JS.自定义控件等:   强大的插件移动应用引擎 Smobiler支持分插件打包功能和插件扩展机制,让应用开发更加灵活. 分插件打包是指Smo ...

  3. 从零开始学安全(三十四)●百度杯 ctf比赛 九月场 sqli

    先扫后台发现 两个可疑登录界面 l0gin.php login.php 猜测是第一个 用bp 抓包发现 index.php 中间有302 重定向 头文件 里面有一个 page=l0gin.php 应该 ...

  4. 程序员50题(JS版本)(一)

    程序1:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? for(var i=1,sum=0;i<=4;i++){ for(var j=1;j<=4;j++){ ...

  5. 配置ADB到Windows环境变量

    adb 命令可以帮我们快速的管理连接的手机设备,例如执行一些安装apk,卸载apk命令,对于熟悉linux系统的人,可以方便的管理手机目录操作手机文件,还可以通过adb命令查看手机的系统日志等操作. ...

  6. Tomcat 参数配置相关

    Tomcat参数配置相关 by:授客 QQ:1033553122 目的: 对Tomcat配置的点滴学习总结,主要目的在于分析Tomcat与性能相关的一些参数设置,以便性能调优时选择最优配置   环境: ...

  7. 如何开启红米手机4X的ROOT超级权限

    红米手机4X通过什么方法拥有了root权限?大家都清楚,Android机器有root权限,如果手机拥有了root相关权限,可以实现更强的功能,举个栗子大家公司的营销部门同事,使用大多数营销软件都需要在 ...

  8. 如何获得MIUI10系统的root超级权限

    MIUI10系统有没有办法拥有root超级权限?做开发的人都清楚,android手机有root超级权限,如果手机拥有root相关权限,能够实现更强的功能,举个栗子做开发的人企业的营销部门,使用某些营销 ...

  9. 共创力咨询推出《静态代码分析(PCLint)高级实务培训》课程!

    [课程背景] C/C++语言的语法非常灵活性,尤其是指针及内存使用,这种灵活性使代码效率比较高,但同时也使得代码编写具有较大的随意性,另外C/C++编译器不进行强制类型检查,也不对数据边界和有效性进行 ...

  10. 【笔记】两个根因分析方法:5WHY&10WHY

    什么是问题根因分析 根本原因分析(root cause analysis):通过调查和分析问题哪里出错.为什么出错,寻求防止差错事故再次发生的必要措施,从而提高服务安全和质量. 根因分析目标 问题(发 ...