android studio 2.0创建一个空android moudle后,

会出现两个test目录,其中一个是Instrument Test,

另一个则是Unit Test

Unit Test 和 Instrument Test 的区别:

在 Android Developer 给出的 Instrument Test 解释如下:

Instrumented unit tests are unit tests that run on physical devices and emulators, instead of the Java Virtual Machine (JVM) on your local machine. You should create instrumented unit tests if your tests need access to instrumentation information (such as the target app’s Context) or if they require the real implementation of an Android framework component (such as a Parcelable or SharedPreferences object). Using instrumented unit tests also helps to reduce the effort required to write and maintain mock code. You are still free to use a mocking framework, if you choose, to simulate any dependency relationships. Instrumented unit tests can take advantage of the Android framework APIs and supporting APIs, such as the Android Testing Support Library。

简单的翻译: Instrumented Unit test 是允许在真机或者模拟器上的,而不是运行在本地环境下的虚拟机中。如果在测试时需要使用 instrumentation information(例如 app Context),或者你需要获取 Android 框架的组件(例如 SharedPrederences),这时候你就可以创建一个 instrumented unit test。使用 Instrumented unit test 可以帮助减少编写模拟环境的代码。当然你也可以使用一些 mock 框架。使用 Instrumented unit test 可以很好的利用 Android framework api 和 supporting API。

在 Android Developer 给出的 Local Unit Tests(Unit Test)解释如下:

If your unit test has no dependencies or only has simple dependencies on Android, you should run your test on a local development machine. This testing approach is efficient because it helps you avoid the overhead of loading the target app and unit test code onto a physical device or emulator every time your test is run. Consequently, the execution time for running your unit test is greatly reduced. With this approach, you normally use a mocking framework, like Mockito, to fulfill any dependency relationships.

简单的翻译:如果你的单元测试不依赖Android环境,那么你需要使用一个本地的单元测试,运行在本地的JVM上,这样的话,就有利于你的测试速度,因为加载物理设备是很耗费时间的。你可以使用类似于 Mockito 这种 Java 测试框架。

简单的总结一下是,这些情况适合用 Instrumented unit test :

  • 测试时需要使用 Android api支持
  • 测试时需要使用 Android 中的组件
  • 测试时需要访问 Android 中的特定环境元素(例如 Context)

其他情况优先考虑使用 Unit Test ,因为它的速度要快很多,效率也要高很多。其次,Instrumented unit test 是基于 Unit Test 的。

Unit Test 环境配置:

dependencies {

  testCompile ‘junit:junit:4.12’

  testCompile “org.mockito:mockito-core:1.9.5”

}

编写测试代码:

配置运行:

运行结果:

tip:需要在测试方法前需要添加 @Test 注解

两个知识点学习:

关于 Junit 4 中注解的使用:

  • @Before 被这个注解的方法会阻塞 @Test 注解方法,在 @Test 注解方法前执行,如果存在多个 @Before 方法,执行顺序随机。这个注解的方法主要用来执行一些测试的设置。
  • @After 被这个注解标志的方法,会在所有的 @Test 方法之后执行,主要用来释放资源的操作
  • @Test 被这个注解标志的方法,就是你的测试方法,一个单元测试类可以有多个测试方法。
  • @Test(timeout=) 带参数的 @Test 注解标签,如果方法在参数时间内没有成功完成,则返回失败。
  • @BeforeClass 被这个标签标注的方法,会在Class第一次加载调用,所以你可以在里面执行一个 static 方法,比如一些 static 方法是很费资源的,例如 JDBC 操作。
  • @AfterClass 对应着 @BeforeClass ,你可以进行一些 static 操作之后的资源释放。

Assert 系列方法的使用: 
Junit 提供了一系列的 Assert 重载方法,提供你把预期结果(Object,long,Arrays 等类型)和实际结果进行比较。同时还有一个 AssertThat()方法,提供了一个失败 Message 的输出

Instrumented unit 环境配置:

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}
dependencies {
    androidTestCompile 'com.android.support:support-annotations:23.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    // 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.1'
    // Optional -- UI testing with UI Automator
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
运行配置:



运行结果:


官方链接:

http://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html#setup

 

Demo地址:

https://github.com/googlesamples/android-testing/tree/master/unit/BasicUnitAndroidTest

android 单元测试的更多相关文章

  1. Android随笔之——Android单元测试

    在实际开发中,开发android软件的过程需要不断地进行测试.所以掌握Android的单元测试是极其重要的.您应该把单元测试作为Android应用开发周期的一部分,精心编写的测试可以在开发早起帮你发现 ...

  2. Android单元测试实践

    为什么要写单元测试 首先要介绍为什么蘑菇街支付金融这边会采用单元测试的实践.说起来比较巧,刚开始的时候,只是我一个人会写单元测试.后来老板们知道了,觉得这是件 很有价值的事情,于是就叫我负责我们组的单 ...

  3. Android单元测试

    安卓单元测试总结文章,目测主要会cover以下的主题: 什么是单元测试 为什么要做单元测试 JUnit Mockito Robolectric Dagger2 一个具体的app例子实践 神秘的bonu ...

  4. Android单元测试: 首先,从是什么开始

    Android单元测试: 首先,从是什么开始 http://chriszou.com/2016/04/13/android-unit-testing-start-from-what.html 这是一系 ...

  5. Android单元测试之四:仪器化测试

    Android单元测试之四:仪器化测试 仪器化测试 在某些情况下,虽然可以通过模拟的手段来隔离 Android 依赖,但代价很大,这种情况下可以考虑仪器化的单元测试,有助于减少编写和维护模拟代码所需的 ...

  6. Android单元测试之三:使用模拟框架模拟依赖

    Android单元测试之三:使用模拟框架模拟依赖 基本描述 如果是一些工具类方法的测试,如计算两数之和的方法,本地 JVM 虚拟机就能提供足够的运行环境,但如果要测试的单元依赖了 Android 框架 ...

  7. Android单元测试之一:基本概念

    Android单元测试之一:基本概念 简单介绍 单元测试是应用程序测试策略中的基本测试,通过对代码进行单元测试,一方面可以轻松地验证单个单元的逻辑是否正确,另一方面在每次构建之后运行单元测试,可以快读 ...

  8. android单元测试 activity跳转 以及 input 输入后 测试

    Android junit实现多个Activity跳转测试 分类: Android Junit测试2011-11-14 16:49 1601人阅读 评论(2) 收藏 举报 androidjunitla ...

  9. Android 单元测试学习计划

    网上查了一下Android单元测试相关的知识点,总结了一个学习步骤: 1. 什么是单元测试2. 单元测试正反面: 2.1. 重要性 2.2. 缺陷 2.3. 策略3. 单元测试的基础知识: 3.1. ...

  10. 阿里知识储备之二——junit学习以及android单元测试

    一,junit框架 http://blog.csdn.net/afeilxc/article/details/6218908 详细见这篇博客 juit目前已经可以和maven项目进行集成和测试,而且貌 ...

随机推荐

  1. link和@import的区别、及各自的应用

    面试的过程中遇到的问题,当时自己回答的感觉自己心里还是很满意的,但是回来百度查看后才知道自己回答的有多么的糟糕: 下面我这这个知识点做一些总结的书面说明,为了少走点弯路,多涨点见识吧. 首先我们要了解 ...

  2. UVA 10054 the necklace 欧拉回路

    有n个珠子,每颗珠子有左右两边两种颜色,颜色有1~50种,问你能不能把这些珠子按照相接的地方颜色相同串成一个环. 可以认为有50个点,用n条边它们相连,问你能不能找出包含所有边的欧拉回路 首先判断是否 ...

  3. MaxScale:实现MySQL读写分离与负载均衡的中间件利器

    1. MaxScale 是干什么的? 配置好了 MySQL 的主从复制结构后,我们希望实现读写分离,把读操作分散到从服务器中,并且对多个从服务器能实现负载均衡. 读写分离和负载均衡 是MySQL集群的 ...

  4. 微信H5页面内实现一键关注公众号

    H5页面内实现关注公众号的微信JSSDK没有相关接口开放,因此就得动点脑筋来实现该功能了.下面的方法就是通过一种非常蹊跷的方式实现的. 首先,需要在公众号内发表一篇原创文章,注意是原创文章,然后由另一 ...

  5. Innodb 表空间传输迁移数据

    在mysql5.5之前,mysql实例中innodb引擎表的迁移是个头疼的问题,要么使用mysqldump导出,要么使用物理备份的方法,但是在mysql5.6之后的版本中,可以使用一个新特性,方便地迁 ...

  6. [OSG]OpenSceneGraph FAQ 以及OSG资源

    1.地球背面的一个点,计算它在屏幕上的坐标,能得到吗? 不是被挡住了吗? 答:计算一个空间点的屏幕坐标,使用osgAPEx::GetScreenPosition函数.当空间点处于相机视空间内(不管它是 ...

  7. JavaScript基本数据类型(较易混淆的几个概念)

    1. var a; typeof a;  控制台输出结果是什么? 解答:a只被声明,而未被初始化,在javascript中,未初始化的变量默认值都为undefined; 2. var s=" ...

  8. Linux术语全称

    bin = BINaries (binary) /dev = devices /etc = ETCetera etcetera附加的人, 附加物, 以及其它, 等等 /lib = LIBrary /p ...

  9. android dialog

    /** * @Title MenuTest.java * @package com.example.standardview * @since * @version 1.0.0 * @author V ...

  10. tomcat 7+ 启动慢 熵池阻塞变慢详解

    原因: Tomcat 7/8都使用org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom类产生安全随机类SecureRan ...