【问题标题】:JUnit tests in Android from Gradle: "package android.test does not exist"来自 Gradle 的 Android 中的 JUnit 测试:“包 android.test 不存在”
【发布时间】:2014-02-18 22:03:49
【问题描述】:

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

package android.test does not exist

我按照另一篇文章中的说明将以下内容添加到我的 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 如下:

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 感谢您的 cmets。我确保我的目录名称与包名称匹配并更新了 sourceSets 中的 srcDir,但仍然出现相同的错误。目录结构包含为图像。
  • 在我看来你需要将 android-test 添加到你的 unitTestCompileDependencies

标签: java android unit-testing junit gradle


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2014-10-26
    相关资源
    最近更新 更多