Android Studio Robolectric测试环境配置
来源:互联网 发布:mysql with 编辑:程序博客网 时间:2024/05/22 02:17
Robolectric是一个高效但不完美的Android单元测试框架。其通过一系列对底层Android元素的替换来实现对原有元素调用的模拟,从而实现脱离模拟器的测 试。非常值得一提的是,在测试服务器请求时,Robolectric的数据模拟和延时发送模拟,给多线程状态下的测试提供了很好的解决方法
目前最新版本为3.0,可以支持API21,你可以在github上面找到他的实例和开源代码。
Roblectric的github地址
此处所实例的配置是之前2.4版本Roblectric的配置,目前使用最新的AS和3.0版的Robolectric更简单。
我的配置环境步骤
安装Android Studio Unit Test插件。
这个插件可以自动帮你关联测试所在目录以及维护插件测关系依赖,免去你很多需要配置的地方。
可以通过插件中心(settings-plubgins)在线安装(Browse repositories…),也可以先下载插件的zip包,离线安装(Install plugin from disk..)。(插件zip包下载位置:https://github.com/evant/android-studio-unit-test-plugin/blob/master/AndroidStudioUnitTestPlugin.zip?raw=true)添加测试module
下图是我的项目目录,测试类单独在一个module里面。这样我们项目里面是不需要任何改动的,配置都是在测试module里面配置。
下面是一些比较关键的文件:../robolectric-test/build.gradle
evaluationDependsOn(":app")apply plugin: 'java'dependencies { def androidModule = project(':app') testCompile project(path: ':app', configuration: 'debugCompile') def debugVariant = androidModule.android.applicationVariants.find({it.name == 'debug'}) testCompile debugVariant.javaCompile.classpath testCompile debugVariant.javaCompile.outputs.files testCompile files(androidModule.plugins.findPlugin("com.android.application").getBootClasspath()) testCompile 'junit:junit:4.12' testCompile 'org.robolectric:robolectric:2.4'}tasks.withType(Test) { scanForTestClasses = false include "**/*Should.class" include "**/*Test.class" include "**/*Tests.class"}
我这里使用了AndroidAnnotations所以需要自定义TestRunner类,让测试方法能够找到资源文件和清单文件位置。../robolectric-test/src/test/packagename/MyRobolectricTestRunner.java
package example.mislead.com.apptest;import org.junit.runners.model.InitializationError;import org.robolectric.AndroidManifest;import org.robolectric.RobolectricTestRunner;import org.robolectric.annotation.Config;import org.robolectric.res.Fs;public class MyRobolectricTestRunner extends RobolectricTestRunner { // 2.4版本只是支持到API18,最新的3.0可以支持API21了 private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18; protected static final String projectName = "appTest"; public MyRobolectricTestRunner(Class<?> testClass) throws InitializationError { super(testClass); } @Override protected AndroidManifest getAppManifest(Config config) { //set the project path as the root path int nameLength = projectName.length(); String rootPath = System.getProperty("user.dir", "./"); int index = rootPath.indexOf(projectName); //get the index of projectRootPath of user.dir if (index == -1) { throw new RuntimeException("project name not found in user.dir"); } rootPath = rootPath.substring(0, index + nameLength); String manifestProperty = rootPath + "/app/src/main/AndroidManifest.xml"; String resProperty = rootPath + "/app/src/main/res"; String assetsProperty = rootPath + "/app/src/main/assets"; return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty), Fs.fileFromPath(assetsProperty)) { @Override public int getTargetSdkVersion() { return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC; } }; }}
- Android Studio Robolectric测试环境配置
- Android Studio环境下Android单元测试(基于Robolectric+Mockito)
- 【Android】robolectric测试实践
- Android Robolectric 测试框架探究
- Android测试框架之Robolectric
- Robolectric测试环境移除stetho
- Robolectric 测试你的Android代码
- android studio环境配置
- Android Studio 环境配置
- android studio 环境配置
- android studio 环境配置
- Android Studio使用Robolectric对android程序实现单元测试
- 使用Robolectric对android程序实现自动化测试
- Android 系列 3.5使用Robolectric和JUnit测试4
- Robolectric 环境搭建(Eclipse)
- Robolectric环境搭建小记
- Android Studio Espresso测试配置
- android studio+Genymotion环境配置
- 国内下载android源码
- linux0.11源代码电梯算法解析
- 黑马程序员:3.面向对象(下)
- HDU 2089 不要62(数位DP)
- aapt常用命令
- Android Studio Robolectric测试环境配置
- js addEventListener和attachEvent
- android下使用HttpClient发起get和post请求
- SDUT 2158 Hello World!(第一届山东省赛)
- iOS-字体适应UIButton大小
- php和mysql连接的个人总结
- MFC 控件richedit
- 学生管理系统(c语言)
- 字符串和字符编码