Android 单元测试只有放在 androidTest 文件夹中才能编译

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

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

Android Plug-in for Gradle version 1.1.0 and higher allows you to create a source directory (src/test/java) in your project to store JUnit tests that you want to run on a local machine.

我的 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

最佳答案

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

并且您需要更改构建变体。

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

关于Android 单元测试只有放在 androidTest 文件夹中才能编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31392601/

相关文章:

安卓工作室 : Error Expected Resources as string when using R. color.colorX

java - 如何监听或轮询事件

java - 一个应用程序显示无法执行 dex : GC overhead limit exceeded GC overhead limit exceeded in eclipse

java - 某些 api 调用在 Android 9 上失败

c# - 我如何模拟 FromSql() 方法?

android - 无法解决实现 'com.github.glomadrian:MaterialAnimatedSwitch:1.1@aar'

android - 如何为本地 .aar 库设置 transitive = true?

java - Junit集成测试

c# - 如何使用 Nunit 断言每个集合项

android - 为每种风格动态更改调试/发布 list key