来自Gradle的Android中的JUnit测试:“程序包android.test不存在" [英] JUnit tests in Android from Gradle: "package android.test does not exist"

查看:906
本文介绍了来自Gradle的Android中的JUnit测试:“程序包android.test不存在"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何使我的单元测试通过Gradle运行.我创建了一个unitTest任务,设置了适当的源集和适当的依赖关系,但出现错误:

I'm not sure how to get my unit tests to run through Gradle. I created a unitTest task, set appropriate source sets, and appropriate dependencies, but I'm getting the error:

package android.test does not exist

我在另一篇文章中按照说明进行操作,并将以下内容添加到我的build.gradle中:

I followed directions in another post and added the following to my build.gradle:

// Unit tests
sourceSets {
    unitTest {
        java.srcDir file('test')
        resources.srcDir file('test/res')
    }
}

dependencies {
    unitTestCompile files("$project.buildDir/classes/debug")
    unitTestCompile 'junit:junit:4.11'
    unitTestCompile 'com.google.android:android:4.0.1.2'
}

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
}

完整的build.gradle如下:

Full build.gradle below:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}

configurations {
    apt
}

apply plugin: 'android'

dependencies {
    compile project(':PullToRefresh')
    compile project(':simple-crop-image-lib')
    compile project(':facebook')
    compile project(':gridlayout')
    compile project(':nineoldandroids')
    compile project(':ViewPagerIndicator')
    compile project(':volley')
    compile fileTree(dir: 'libs', include: '*.jar')
    apt files('compile-libs/androidannotations-2.7.1.jar')
}

android {
    buildToolsVersion '17.0'
    compileSdkVersion 17

    signingConfigs {
        debug {
            storeFile file('keystores/debug.keystore')
        }
    }

    buildTypes {
        debug {}

        ipconfigurable {
            debuggable true
            signingConfig signingConfigs.debug
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

// Unit tests
sourceSets {
    unitTest {
        java.srcDir file('src/test')
        resources.srcDir file('src/test/res')
    }
}

dependencies {
    unitTestCompile files("$project.buildDir/classes/debug")
    unitTestCompile 'junit:junit:4.11'
    unitTestCompile 'com.google.android:android:4.0.1.2'
}

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
}

目录结构:

推荐答案

感谢@PerrynFowler,解决方案是将以下行添加到我的unitTestCompile依赖项中:

Thanks to @PerrynFowler, the solution was to add the following line to my unitTestCompile dependencies:

unitTestCompile 'com.google.android:android-test:4.1.1.4'

这篇关于来自Gradle的Android中的JUnit测试:“程序包android.test不存在"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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