II. Android自带基本的单元测试control + shift + R (Android Studio 默认执行单元测试快捷键)。 1. 本地单元测试直接在开发机上面进行运行测试。在没有依赖或者仅仅只需要简单的Android库依赖的情况下,有限考虑使用该类单元测试。 代码存储如果是对应不同的flavor或者是build type,直接在test后面加上对应后缀(如对应名为 src/test/java 激活测试在一个功能测试或验证的测试方法前面添加 Google官方推荐引用dependencies {
// Required -- JUnit 4 framework,用于单元测试,google官方推荐
testCompile 'junit:junit:4.12'
// Optional -- Mockito framework,用于模拟架构,google官方推荐
testCompile 'org.mockito:mockito-core:1.10.19'
}
2. 模拟测试运行在Android设备或者虚拟机上的测试 主要用于测试: 单元(Android架构引用相关的单元测试)、UI、应用组件集成测试(Service、Content Provider、etc.) 代码存储:src/androidTest/java Google官方推荐引用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'
}
常见的UI测试需要模拟Android系统环境 主要三点:
III. 拓展工具1. AssertJ Android极大的提高可读性。 // 一般的JUnit assertEquals(View.GONE, view.getVisibility()); // AssertJ Android assertThat(view).isGone(); 2. Robolectric让模拟测试直接在开发机上完成,而不需要在Android系统上。 主要是解决模拟测试中耗时的缺陷,模拟测试需要安装以及跑在Android系统上,也就是需要在Android虚拟机或者设备上面,所以十分的耗时。基本上每次来来回回都需要几分钟时间。针对这类问题,业界其实已经有了一个现成的解决方案: Pivotal实验室推出的 Robolectric 。通过使用Robolectrict模拟Android系统核心库的 3. Mockito快速模拟控制系统架构返回参数。 不同于Roblectric,Mockito可以通过模拟并控制或修改一些方法的行为。 // 无论什么时候调用 myQueryObject.getCurrentTime,返回值都会是 1363027600 Mockito.doReturn((long) 1363027600).when(myQueryObject).getCurrentTime(); 4. Robotium(Integration Tests)模拟用户操作,事件流测试。 通过模拟用户的操作的行为事件流进行测试,这类测试无法避免需要在虚拟机或者设备上面运行的。是一些用户操作流程与视觉显示强相关的很好的选择。 |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|