Android studio下gradle Robolectric单元测试配置
2014-10-11 14:11
246 查看
android studio下gradle Robolectric单元测试配置
1.Robolectric
Robolectric是一个基于junit之上的单元测试框架。它并不依赖于Android提供的测试功能,它使用了shadow objects并且运行测试于普通的工作站/服务器JVM,不像模拟器或设备需要dexing(Android dex编译器将类文件编译成Android设备上的Dalvik VM使用的格式),打包,部署和运行的过程,大大减少了测试执行的时间。
参考:安卓单元测试相关概述http://www.cnblogs.com/droidpilot/archive/2012/04/27/2473291.html
2.下载as插件
如果是android studio 0.8.9以下的版本,需要按照指示添加额外配置
https://github.com/evant/android-studio-unit-test-plugin
3.编写gradle 配置
项目地址:https://github.com/JCAndKSolutions/android-unit-test
以下是配置的详细说明
4.运行测试
直接在as的终端里面执行:gradle test 或者./gradlew test 即可
1.Robolectric
Robolectric是一个基于junit之上的单元测试框架。它并不依赖于Android提供的测试功能,它使用了shadow objects并且运行测试于普通的工作站/服务器JVM,不像模拟器或设备需要dexing(Android dex编译器将类文件编译成Android设备上的Dalvik VM使用的格式),打包,部署和运行的过程,大大减少了测试执行的时间。
参考:安卓单元测试相关概述http://www.cnblogs.com/droidpilot/archive/2012/04/27/2473291.html
2.下载as插件
如果是android studio 0.8.9以下的版本,需要按照指示添加额外配置
https://github.com/evant/android-studio-unit-test-plugin
3.编写gradle 配置
项目地址:https://github.com/JCAndKSolutions/android-unit-test
以下是配置的详细说明
buildscript { repositories { mavenCentral() maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { classpath 'com.android.tools.build:gradle:0.12.+' // classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+’ //引用相关gradle插件 classpath 'com.github.jcandksolutions.gradle:android-unit-test:1.6.2' } } apply plugin: 'android-library' apply plugin: 'idea' idea { module { //设置测试类的输出目录 testOutputDir = file('build/test-classes') } } repositories { mavenCentral() maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } //由于android studiode 一个bug,必须把module的iml文件中的android sdk引用放到最下面 task pushDownJdkDependency { //这里是待测试项目的iml文件名 def imlFile = file("library.iml") doLast { try { def parsedXml = (new XmlParser()).parse(imlFile) def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' } parsedXml.component[1].remove(jdkNode) //这里是target sdk版本,只需要改数字就行 new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': "Android API 19 Platform", 'jdkType': 'Android SDK']) def writer = new StringWriter() new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml) imlFile.text = writer.toString() } catch (FileNotFoundException e) { // nop, iml not found } } } //在build之前修改iml文件 gradle.projectsEvaluated { preBuild.dependsOn(pushDownJdkDependency) } android { compileSdkVersion 19 buildToolsVersion '19.1.0' sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src/main/java'] res.srcDirs = ['res’] //指定测试文件所在目录 androidTest.setRoot('src/test') } } defaultConfig { minSdkVersion 10 targetSdkVersion 19 versionCode 2 versionName "2.0.0" testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" } } //应用插件 apply plugin: 'android-unit-test' dependencies { compile 'com.android.support:support-v4:19.1.0’ //注意,如果https://github.com/evant/android-studio-unit-test-plugin,此插件没有安装,则可能无法识别testCompile语义 //junit:junit和org.robolectric:robolectric是必须项,其他的项目根据实际引用添加 testCompile 'junit:junit:4.10' testCompile 'org.robolectric:robolectric:2.3' testDebugCompile 'org.debugonly.dependency' testFreeCompile 'Admob.jar' testCompile 'org.mockito:mockito-all:1.9.5' testCompile('com.squareup:fest-android:1.0.+') { exclude module: 'support-v4' } }
4.运行测试
直接在as的终端里面执行:gradle test 或者./gradlew test 即可
相关文章推荐
- 在Android Studio进行“简单配置”单元测试(Android Junit)
- 在Android Studio中进行单元测试和UI测试配置支持单元测试的工程
- 解耦配置文件依赖,让单元测试跑起来
- Hibernate配置文件在单元测试中的灵活运用
- VS2005配置CPPUnit进行单元测试
- [转]VS2008下.NET 单元测试工具 NUnit2.5 配置与集成方法
- Android:单元测试Junit的配置
- [转]VS2008下.NET 单元测试工具 NUnit2.5 配置与集成方法
- C#中单元测试如何部署配置文件
- visual studio 2010单元测试--代码覆盖率配置(code coverage)
- VS2008下.NET 单元测试工具 NUnit2.5 配置与集成方法
- Hibernate配置文件在单元测试中的应用
- VS2008下.NET 单元测试工具 NUnit2.5 配置与集成方法
- VS2008下.NET 单元测试工具 NUnit2.5 配置与集成方法
- VS2005配置CPPUnit进行单元测试
- VS2008下.NET 单元测试工具 TestDriven.NET 配置与集成方法
- Hibernate配置文件在单元测试中的灵活运用
- Hibernate配置文件在单元测试中的应用
- 关于VS2008单元测试中加载配置文件的问题
- NHibernate配置,单元测试总结