Android 的单元测试有两种方式:JUnit test(本地虚拟机测试) 和 instrumented test(设备测试)。

a JUnit test that runs on the local JVM or an instrumented test that runs on a device

Local unit tests:Located at module-name/src/test/java/ 可以本地虚拟机测试,只能调用Java API ,不能调用 Android API 。不过,可以通过依赖 android.jar 或者 第三方库(如:Mockito)调用 Android API。

Instrumented tests:Located at module-name/src/androidTest/java/ 在设备上运行,可以调试Android API,并可自定义测试配置文件。通过依赖第三方库(如:Espresso)可以实现 UI testing.

二、环境配置

the test library dependencies in your app module's build.gradle file:

dependencies {

// Required for local unit tests (JUnit 4 framework)

testCompile 'junit:junit:4.12'

// Required for instrumented tests

androidTestCompile 'com.android.support:support-annotations:24.0.0'

androidTestCompile 'com.android.support.test:runner:0.5'

}

单元测试在项目中的目录结构:

Paste_Image.png

配置问题:

Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.3.0) and test app (23.1.1) differ

解决方案 :参考官网

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:

android {

defaultConfig {

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

}

上面是配置,然后添加下面这句:

android{

configurations.all {

resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'

}

}

三、创建 Test 文件

import org.junit.Test;

import java.util.regex.Pattern;

import static org.junit.Assert.assertFalse;

import static org.junit.Assert.assertTrue;

public class EmailValidatorTest {

@Test

public void emailValidator_CorrectEmailSimple_ReturnsTrue() {

assertThat(EmailValidator.isValidEmail("name@email.com"), is(true));

}

...}

import org.junit.Before;import org.junit.Test;

import org.junit.runner.RunWith;

/**

* RxJava simple test

* Created by ys on 2016/10/14 0014.

*/

@RunWith(AndroidJUnit4.class)

@SmallTest

public class RxJavaSimpleTest {

private RxJavaSimple mRxJavaSimple;

@Before

public void createRxJavaSimple(){

mRxJavaSimple = new RxJavaSimple();

}

@Test

public void testScheduler(){

//打印 Log

mRxJavaSimple.testScheduler();

}

}

四、Run Test

To run your instrumented tests, follow these steps:

Be sure your project is synchronized with Gradle by clicking Sync Project

in the toolbar.

Run your test in one of the following ways:

To run a single test, open the Project window, and then right-click a test and click Run

.

To test all methods in a class, right-click a class or method in the test file and click Run

.

To run all tests in a directory, right-click on the directory and select Run tests

三种方式:

运行单个 Test,右键 -> Run。

运行单个Test 文件,右键 class -> Run。

运行文件夹中所有Test,右键文件夹-> Run tests。

android 单元测试2016,Android 单元测试相关推荐

  1. android studio资产目录,在Android Studio中设置单元测试的自定义资产目录

    我们对使用仅需要进行单元测试的资产的Android应用程序执行单元测试是不常见的要求. 使用基于Eclipse的SDK这是简单明了的 – 测试项目是一个单独的项目,其自有资产文件夹.使用Android ...

  2. android studio异步单元测试,在Android Studio中可以进行单元测试

    写单元测试类 1.创建单元测试文件夹,即新建一个用于单元测试的包,存放单元测试的类. 2.创建一个类如 ExampleTest,注意要继承自InstrumentationTestCase类. 3.创建 ...

  3. 在Android Studio进行“简单配置”单元测试(Android Junit)

    起因 在Android studio 刚出.本人就想弄单元测试,可惜当时Android studio不知道抽什么风(准确来说,应该是我不会弄而已).无法执行到相应的代码. 后来今天突然自己又抽风.又想 ...

  4. Android单元测试一:单元测试入门

    1. 前言 在Android开发中,如果对一个简单的功能,每次修改代码都重新运行到设备中进行测试,会浪费大量时间,降低开发工作效率.如果使用单元测试,编写单元测试类,执行测试单元测试类就可以对某些功能 ...

  5. Android中的Junit单元测试

    Android中的Junit单元测试 在实际开发中,经常要对已经实现的功能进行单元测试,以保证当前单元没问题,尽可能的减少已有功能的bug 和Java中的开发一样,Android中对单元测试也可以采用 ...

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

    目录 在Android Studio中进行单元测试和UI测试 - 1.概述 在Android Studio中进行单元测试和UI测试 - 2.创建新的Android Studio工程 在Android ...

  7. Android学习笔记 75. 单元测试

    Android学习笔记 Android 开发者基础知识 (Java) -- Google Developers 培训团队 文章目录 Android学习笔记 Android 开发者基础知识 (Java) ...

  8. android单元测试demo,android单元测试AndroidTestCase

    在实际开发中,开发android软件的过程需要不断的进行测试.而是用Junit测试框架,则是正规android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 比如 ...

  9. Android ui 单元测试 覆盖率,Android单元测试/Ui测试+JaCoCo覆盖率统计

    Android单元测试/Ui测试+JaCoCo覆盖率统计 参考资料1 参考资料2 背景说明 单元测试 从源代码着手,对源码中的最小可测试单元进行检查和验证,在对源代码有较深的理解下,编写测试单元,工作 ...

最新文章

  1. Windows 2008 r2域更名
  2. Xampp安装PHPUnit
  3. Java项目中,如何限制每个用户访问接口的次数
  4. python操作文件的库_Python使用pyshp库读取shapefile信息的方法
  5. 如何实现android和服务器长连接呢?推送消息的原理
  6. 八大排序算法的Python实现
  7. python高阶函数(三分钟读懂)
  8. 支持统一码 10 的花园明朝字库终于更新出来了
  9. 如何阅读一本好书:APUE
  10. C语言实现贪吃蛇(四)----游戏存档读档(文件操作)
  11. 阿基米德螺旋线原理及代码
  12. 请问如何让路由器信号增强
  13. N-Tiers开发方式(COM+组件的注册、修改)
  14. 2023二建建筑施工备考第二天Day06水泥
  15. [转]孙悟空无姓无名的时候,阎王生死簿是怎么写的呢?
  16. 哪个软件可以制作GIF表情包
  17. 读书笔记-命令行总结
  18. java SE7 概要图
  19. Jenkins之用户管理
  20. 注意力水平的测试软件,中国儿童注意力水平测评量表儿童

热门文章

  1. 人工智能(一)概述(树状图)
  2. 使用多种方式实现输出1~100 内的奇数和
  3. Python模块---海龟(turtle)
  4. 浪潮思科交换机端口无法打开、光模块无光解决办法
  5. 【多重循环】【流程控制】abc组合(“百文白鸡”穷举法)
  6. 字节跳动员工不愿取消大小周?称每年少赚10万块
  7. acw周赛 - 点的赋值 —— 二分图染色
  8. 计算机二级函数教程,计算机二级 - Excel常用函数
  9. OPPO手机路迷5G
  10. 立体显示的原理-几种显示方式