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. PI校正环节的程序实现推导过程
  2. 泉州服务器维修,泉州云服务器
  3. python哨兵循环_Python:deadloop之非模态交互界面(模态循环)(哨兵循环)
  4. 机器人学习--卡尔曼滤波及各种滤波解析
  5. Android开发之蓝牙--扫描已经配对的蓝牙设备
  6. Hbase 协处理器之将数据保存到es (二级索引)
  7. db2与mysql语法区别,db2和mysql语法分析异同点
  8. Windows下Hadoop eclipse开发平台搭建
  9. Kd-tree的学习
  10. 拓端tecdat|R语言DTW(Dynamic Time Warping) 动态时间规整算法分析序列数据和可视化
  11. 鸿蒙音响怎么调,好音质是调出来的 大师教你咋调试音响
  12. 计算机中官方文档阅读方法,中国知网-帮助中心
  13. 安装Centos系统时选择安装的包
  14. 20210319_23期_心跳检测_Task02_数据探索性分析
  15. python调用photoshop_Python和Photoshop
  16. 环信php修改头像,环信客服 如何正确设置用户的头像和昵称?
  17. python库声纹_什么是声纹数据库?
  18. java中的字符串常量池_java字符串常量池
  19. Visual Studio 2017, 2019 官网下载地址
  20. ScrollView的属性(纵向的用HorizontalScrollView)

热门文章

  1. 【PHP基础】预定义超全局变量
  2. php学生老师,学生渴望的新课堂a href=/friend/list.php(教师中心专稿)/a
  3. java简单实现生日计算方法
  4. W3 D2 二维数组及案例
  5. 优惠券系统:优惠券结算微服务配置
  6. PHP 使用 PhpSpreadsheet 操作 excel 表格
  7. 【游戏开发】minecraft基于基础科技mod打造全自动化世界
  8. Linux运维02:top命令详解
  9. html5自适应 杂志模板,html5自适应,杂志模板.doc
  10. Flink cdc connectors RocketMQ