Android,Gradle。如何从测试应用程序构建应用程序并运行测试

时间:2013-11-12 11:12:45

标签: android unit-testing gradle

我有android项目和android测试项目,位于文件夹测试下。这些项目的结构就像日食一样

+ - SRC
| - res
| - 图书馆 | - 测试
| .....

我使用gradle。我想要的只是构建应用程序,运行单元测试并获取它们的报告。但我不明白该怎么做。我在根文件夹中创建了build.gradle文件

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 18
        testPackageName "ua.cooperok.stringcalc.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        unitTest {
            java.srcDir file('tests/src')
            resources.srcDir file('tests/res')
        }
    }

    configurations {
        unitTestCompile.extendsFrom runtime
        unitTestRuntime.extendsFrom unitTestCompile
    }

    dependencies {
        unitTestCompile files("$project.buildDir/classes/release")
    }

    task unitTest(type:Test){
        description = "Run unit tests..."
        testClassesDir = android.sourceSets.unitTest.output.classesDir
        classpath = android.sourceSets.unitTest.runtimeClasspath
    }

    build.dependsOn unitTest

}

但是当我运行 gradle build 时,我收到了错误 无法在源设备单元测试中找到属性“输出”。

另外,据我了解,要获取报告我需要应用插件“android-reporting”,但是当我这样做时我得到了错误
无法添加名称为“android”的扩展程序,因为已经使用该名称注册了扩展程序。

我应该这样做,还是我必须在我的测试应用程序项目中创建新的gradle.build文件?

更新

我修复了输出错误,并使gradle构建成功,但我不明白是什么。我特意失败了我的测试,但是gradle build仍然是成功的 我做了什么:
 1.我将任务unitTest从android部分移动到root,之后,我的测试项目开始编译,但是有一个错误'packackage android.test not found'
 2.为了解决这个错误我添加到android源的类路径路径,所以在gradle构建成功之后,但我真的不明白为什么即使测试失败也总是成功

我仍然不明白如何从测试中获取报告

这是我的新build.gradle文件

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6+'
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 18
        testPackageName "ua.cooperok.stringcalc.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    sourceSets {
        main {
            manifest.srcFile file('AndroidManifest.xml')
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
    }
}

sourceSets {
    unitTest {
            java.srcDirs = ['tests/src']
            resources.srcDirs = ['tests/src']
    }
}

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}

dependencies {
    unitTestCompile files("$project.buildDir/classes/release")
    unitTestCompile 'junit:junit:4.11'
    compile fileTree(dir: 'libs', include: '*.jar')
}

task unitTest(type:Test, dependsOn: assemble) {
    description = "run unit tests"
    testClassesDir = sourceSets.unitTest.output.classesDir
    classpath = files("$System.env.ANDROID_HOME/sources/android-18")
    //actualy I didn't  get any report
    getReports().getJunitXml().setOutputPerTestCase(true)
}

build.dependsOn unitTest

1 个答案:

答案 0 :(得分:16)

我找到了解决方案。我对几个主题感到失望,并认为我需要为单元测试创​​建单独的任务。事实上它很简单 我没有改变我的项目结构,只是build.gradle文件。要构建应用程序我运行
gradle clean connectedCheck build
connectedCheck从我的测试项目运行单元测试,如果某些测试失败 - gradle build也会失败

这里的最终版本

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6+'
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 18
        testPackageName "ua.cooperok.stringcalc.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    sourceSets {
        main {
            manifest.srcFile file('AndroidManifest.xml')
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        instrumentTest {
            java.srcDirs = ['tests/src']
            manifest.srcFile file('tests/AndroidManifest.xml')
            resources.srcDirs = ['tests/src']
            res.srcDirs = ['tests/res']
            assets.srcDirs = ['tests/assets']
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
    }

    //Signing apk
    if(project.hasProperty("signingPropertiesPath")) {
        File propsFile = new File(System.getenv('HOME') +  "/" + project.property("signingPropertiesPath"))
        if(propsFile.exists()) {
            Properties props = new Properties()
            props.load(new FileInputStream(propsFile))

            //loading keystore properties
            signingConfigs {
                release {
                    storeFile file(propsFile.getParent() + "/" + props['keystore'])
                    storePassword props['keystore.password']
                    keyAlias props['keyAlias']
                    keyPassword props['keyPassword']
                }
            }
            buildTypes {
                release {
                    signingConfig signingConfigs.release
                }
            }
        }
    }
}