编写 Gradle 脚本以运行 Eclipse Android 测试项目的单元测试用例
Posted
技术标签:
【中文标题】编写 Gradle 脚本以运行 Eclipse Android 测试项目的单元测试用例【英文标题】:Writing Gradle script to run unit test cases for Eclipse Android Test project 【发布时间】:2014-06-26 19:12:12 【问题描述】:我有一个简单的 HelloWorld android 项目(内置在 Eclipse IDE 中),我可以在 cmd 提示符下成功地为这个项目做“gradle build”。
我还为它编写了一个简单的 JUnit Android 测试项目,它在 Eclipse 中运行良好。
现在我想使用 Gradle 脚本运行这个测试项目或单元测试用例(如果我的理解有误!)。 我该怎么做?
以下是我正在使用的 build.gradle 文件。我想知道如何编写脚本代码来自动执行测试用例。
buildscript
repositories
mavenCentral()
dependencies
classpath 'com.android.tools.build:gradle:0.10.+'
apply plugin: 'android'
dependencies
compile fileTree(dir: 'libs', include: '*.jar')
android
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets
main
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
test
java.srcDirs = ['src/test/java']
resources.srcDirs = ['src/test/resources']
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
androidTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
【问题讨论】:
instrumentTest
已重命名为 androidTest
,请参阅 github.com/Nodeclipse/nodeclipse-1/blob/master/… 的历史记录
【参考方案1】:
这是我使用的:
android
// ...
// your stuff
// ...
// So Android Studio can nicely edit the test files
sourceSets
androidTest
java.srcDir file('src/test/java')
// <Task to run tests>
sourceSets
unitTest
java.srcDir file('src/test/java')
configurations
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
task unitTest(type: Test, dependsOn: assemble)
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
check.dependsOn unitTest
// </Task to run tests>
dependencies
// ...
// compile stuff
// ....
unitTestCompile files("$project.buildDir/classes/release")
unitTestCompile 'junit:junit:4.11'
运行你的测试:
gradlew unitTest
【讨论】:
问题是关于 Eclipse ADT(带有经典源文件夹)和如何运行 Android 测试项目(在 Eclipse 中创建) 虽然我投了反对票,但仍然给予赏金作为最佳答案(我无法判断)【参考方案2】:此解决方案尚未实施和测试。工作正在进行中,欢迎 cmets。
仔细阅读所有user guide后,很明显没有直接支持Eclipse Android Test项目。原因是新的构建系统采用 Gradle(和 Maven)风格在模块/项目中进行单元测试。
但是因为Eclipse Android Test项目还是Android项目,所以有办法尝试
将build.gradle
添加到Eclipse Android 测试项目中。
在dependencies
中应该有compile project(':app')
/* Android
* http://www.nodeclipse.org/projects/gradle
* Nodeclipse/Enide build.gradle template for classic Android project
* https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.enide.editors.gradle/docs/android/build.gradle
* Gradle Plugin User Guide:
* http://tools.android.com/tech-docs/new-build-system/user-guide
* Usage
* 1. put in project root
* 2. check version numbers
* 3. use from command line or with Gradle for Android http://marketplace.eclipse.org/content/gradle
* Support for this template
* https://github.com/nodeclipse/nodeclipse-1/issues/
* History
* 2014-03-13 android plugin updated to 0.9, see http://tools.android.com/tech-docs/new-build-system/migrating_to_09
* 2014-04-01 check for gradle version
* 2014-04-10 wrapper and runAndroidApplication tasks
* 2014-04-25 rename to run, add <<
* 2014-05-23 defaut plugin version 0.10.x
* 2014-06-06 show "boilerplate part"
* @author Paul Verest
*/
println GradleVersion.current().prettyPrint()
assert gradle.gradleVersion >= "1.10" // android plugin 0.10 requires Gradle 1.10, 1.11, 1.12
// after `gradle wrapper` it is possible to use './gradlew build' with Gradle version specified
task wrapper(type: Wrapper)
gradleVersion = '1.12'
// "boilerplate part"
buildscript
repositories
mavenCentral()
//jcenter()
dependencies
classpath 'com.android.tools.build:gradle:0.10.+'
apply plugin: 'android'
dependencies
compile fileTree(dir: 'libs', include: '*.jar')
//androidTestCompile 'com.jayway.android.robotium:robotium-solo:4.3.1'
// for multi-module project build (needs `settings.gradle`):
// reference needed modules or App under test (for Eclipse Android Test project)
compile project(':app')
android
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets
main
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
// Move the tests to tests/java, tests/res, etc...
androidTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
// "boilerplate part"
因为应用程序及其测试现在使多模块项目添加settings.gradle
。将其降低 1 级的最简单方法(两个项目所在的文件夹)
include ':app'
include ':apptest'
其他方法是放入同级文件夹,例如叫parent
includeFlat 'app'
includeFlat 'apptest'
在以后的情况下,您需要从该文件夹运行构建(或每次使用-c
指定settings.gradle
位置@CLI option
更新 用 gradle 研究纯 JUnit 测试(这个问题有点离题),似乎 Android Tooling 团队忽略了这个问题,打破了其他开发人员试图用更新的 android gradle 插件做的事情。即使在 Android Studio 中,How can I create tests in Android Studio? 也并非如此。唯一关心的组织是 Robolectric 贡献者,他们的框架已准备好进行纯 JUnit 测试https://github.com/robolectric/gradle-android-test-plugin
【讨论】:
以上是关于编写 Gradle 脚本以运行 Eclipse Android 测试项目的单元测试用例的主要内容,如果未能解决你的问题,请参考以下文章
编写 Gradle 插件时的 Gradle Api Sources 和 Doc