android 单元测试2016,Android 单元测试
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 单元测试相关推荐
- android studio资产目录,在Android Studio中设置单元测试的自定义资产目录
我们对使用仅需要进行单元测试的资产的Android应用程序执行单元测试是不常见的要求. 使用基于Eclipse的SDK这是简单明了的 – 测试项目是一个单独的项目,其自有资产文件夹.使用Android ...
- android studio异步单元测试,在Android Studio中可以进行单元测试
写单元测试类 1.创建单元测试文件夹,即新建一个用于单元测试的包,存放单元测试的类. 2.创建一个类如 ExampleTest,注意要继承自InstrumentationTestCase类. 3.创建 ...
- 在Android Studio进行“简单配置”单元测试(Android Junit)
起因 在Android studio 刚出.本人就想弄单元测试,可惜当时Android studio不知道抽什么风(准确来说,应该是我不会弄而已).无法执行到相应的代码. 后来今天突然自己又抽风.又想 ...
- Android单元测试一:单元测试入门
1. 前言 在Android开发中,如果对一个简单的功能,每次修改代码都重新运行到设备中进行测试,会浪费大量时间,降低开发工作效率.如果使用单元测试,编写单元测试类,执行测试单元测试类就可以对某些功能 ...
- Android中的Junit单元测试
Android中的Junit单元测试 在实际开发中,经常要对已经实现的功能进行单元测试,以保证当前单元没问题,尽可能的减少已有功能的bug 和Java中的开发一样,Android中对单元测试也可以采用 ...
- Android Studio中进行单元测试和UI测试
目录 在Android Studio中进行单元测试和UI测试 - 1.概述 在Android Studio中进行单元测试和UI测试 - 2.创建新的Android Studio工程 在Android ...
- Android学习笔记 75. 单元测试
Android学习笔记 Android 开发者基础知识 (Java) -- Google Developers 培训团队 文章目录 Android学习笔记 Android 开发者基础知识 (Java) ...
- android单元测试demo,android单元测试AndroidTestCase
在实际开发中,开发android软件的过程需要不断的进行测试.而是用Junit测试框架,则是正规android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 比如 ...
- Android ui 单元测试 覆盖率,Android单元测试/Ui测试+JaCoCo覆盖率统计
Android单元测试/Ui测试+JaCoCo覆盖率统计 参考资料1 参考资料2 背景说明 单元测试 从源代码着手,对源码中的最小可测试单元进行检查和验证,在对源代码有较深的理解下,编写测试单元,工作 ...
最新文章
- Windows 2008 r2域更名
- Xampp安装PHPUnit
- Java项目中,如何限制每个用户访问接口的次数
- python操作文件的库_Python使用pyshp库读取shapefile信息的方法
- 如何实现android和服务器长连接呢?推送消息的原理
- 八大排序算法的Python实现
- python高阶函数(三分钟读懂)
- 支持统一码 10 的花园明朝字库终于更新出来了
- 如何阅读一本好书:APUE
- C语言实现贪吃蛇(四)----游戏存档读档(文件操作)
- 阿基米德螺旋线原理及代码
- 请问如何让路由器信号增强
- N-Tiers开发方式(COM+组件的注册、修改)
- 2023二建建筑施工备考第二天Day06水泥
- [转]孙悟空无姓无名的时候,阎王生死簿是怎么写的呢?
- 哪个软件可以制作GIF表情包
- 读书笔记-命令行总结
- java SE7 概要图
- Jenkins之用户管理
- 注意力水平的测试软件,中国儿童注意力水平测评量表儿童