编写Gradle脚本以运行Eclipse Android测试项目的单元测试用例 [英] Writing Gradle script to run unit test cases for Eclipse Android Test project

查看:160
本文介绍了编写Gradle脚本以运行Eclipse Android测试项目的单元测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的HelloWorld Android项目(内置于Eclipse IDE中),我可以在该项目的cmd提示符下成功执行gradle build。

I have a simple HelloWorld Android project (built in Eclipse IDE), I am able to do "gradle build" successfully in cmd prompt for this project.

另外我已经为它编写了一个简单的JUnit Android测试项目,它在Eclipse中运行正常。

Also I have written a simple JUnit Android Test Project for it, and it runs fine in Eclipse.

现在我想运行这个测试项目或单元测试用例(如果我的理解是错误的!)使用Gradle脚本。
我该怎么做?

Now I want to run this Test Project or Unit Test Cases (if my understanding is wrong!) using Gradle script. How do I do that?

以下是我正在使用的build.gradle文件。我想知道如何编写脚本代码来自动执行测试用例。

Following is the build.gradle file I am using. I want to know how to write script code to automate the test case execution.

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')
    }
}


推荐答案

此解决方案尚未实施和测试。正在进行中,意见受到欢迎。

阅读全部用户指南,很显然,没有直接支持Eclipse Android测试项目。原因是新建系统采用Gradle(和Maven)风格在模块/项目中进行单元测试。

After reading all user guide carefully, it is clear that there is no direct support for Eclipse Android Test projects. The reason is that new build system adopts Gradle(and Maven) style to have Unit test inside module/project.

但是由于Eclipse Android测试项目仍然是Android项目,所以是尝试

But because Eclipse Android Test projects is still Android project, there is way to try


  1. 添加 build.gradle Android测试项目。

    应该在依赖关系中有编译项目(':app')

  1. Add build.gradle see below to Eclipse Android Test project.
    It should have compile project(':app') in dependencies

/*      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"

$ C> settings.gradle 。最简单的方法是将其降低1级(两个项目的文件夹)

  • Because app and its test now make multi-module project add settings.gradle. The easiest way to put it 1 level down (folder where both project are)

    include ':app'  
    include ':apptest'
    

    其他方法是放入兄弟文件夹,例如称为 parent

    Other way is to put in sibling folder, e.g. called parent

    includeFlat 'app'
    includeFlat 'apptest'
    

    在稍后的情况下,您需要从该文件夹运行build(或每次 settings.gradle 位置与 -c CLI选项

    In later case you would need to run build from that folder (or specify each time settings.gradle location with -c CLI option

    更新关于纯粹的研究JUnit测试与gradle(这个问题有点偏离),似乎Android工具小组忽略了这个问题,打破了其他开发人员试图用新的android毕业插件,即使在Android Studio它也不是甚至如何在Android Studio中创建测试?。关心的唯一方法是具有框架的Robolectric贡献者准备进行纯JUnit测试 https://github.com/robolectric/gradle-android-test-plugin

    UPDATE Doing research about pure JUnit tests with gradle (a bit offtopic for this question), it seems like Android Tooling team overlooks this question, breaking what others developers are trying to do with newer android gradle plugin. Even in Android Studio it is not so even How can I create tests in Android Studio? . The only orgnization that cares is Robolectric contributors that have its framework ready for pure JUnit testing https://github.com/robolectric/gradle-android-test-plugin

    这篇关于编写Gradle脚本以运行Eclipse Android测试项目的单元测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆