【问题标题】:Android unit test only compiles if placed in androidTest folder只有放在 androidTest 文件夹中的 Android 单元测试才会编译
【发布时间】:2015-07-13 20:15:51
【问题描述】:

我使用的是 Android Studio 1.1.0。我正在尝试将我的单元测试和集成测试分开。集成测试位于文件夹 src/androidTest/java 中。我正在尝试在src/test/java/ 中创建我的单元测试。根据documentation,这应该是可能的。

Android Plug-in for Gradle 版本 1.1.0 及更高版本允许您 在你的项目中创建一个源目录(src/test/java)来存储 您想在本地机器上运行的 JUnit 测试。

我的build.gradle 依赖项看起来有点像这样:

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
    }
}
...
dependencies {
    ...
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'org.mockito:mockito-all:1.9.5'
    androidTestCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile files('libs/dexmaker-mockito-1.0.jar')
    androidTestCompile files('libs/dexmaker-1.0.jar')

    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile ('com.android.support.test:testing-support-lib:0.1') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2') {
        exclude module: 'hamcrest-core'
    }
}

这应该适用于所有帐户,但是如果我将其放在 src/test/java 中,我编写的示例测试根本无法编译。如果我将它放在src/androidTest/java 中,它可以编译并运行良好。我错过了什么?我目前无法升级到 Android Studio 1.2。我已将我的Test Artifact 设置为Unit Test

【问题讨论】:

    标签: android unit-testing android-gradle-plugin android-testing


    【解决方案1】:

    如果您正在使用单元测试,请将您的依赖项添加为 testCompile ...

    您需要更改构建变体。

    查看http://tools.android.com/tech-docs/unit-testing-support

    【讨论】:

      猜你喜欢
      • 2016-05-04
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 2011-12-28
      相关资源
      最近更新 更多