【问题标题】:Android build tools 1.1.0, unit test folder?Android build tools 1.1.0,单元测试文件夹?
【发布时间】:2015-02-22 18:31:48
【问题描述】:

我最近将 google 的最新工具安装到我的 android 项目中:

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

allprojects {
    repositories {
        jcenter()
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "com.xxx"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 200
        versionName "2.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        debug {
            ...
        }
        release {
            ...
        }
    }
    buildTypes {
        release {
            ...
        }
        debug {
            ...
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    // ---- Tests with robolectric
    testCompile 'com.google.guava:guava:14.0.1'
    testCompile 'junit:junit:4.+'
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile 'org.mockito:mockito-all:2.0.2-beta'

    // ---- Tests with Espresso
    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
        exclude module: 'hamcrest-core'
    }
    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:testing-support-lib:0.1') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
    androidTestCompile('junit:junit-dep:4.10') {
        exclude module: 'hamcrest-core'
    }
}

在此之前,我曾经使用com.github.jcandksolutions.gradle:android-unit-test:2.1.1 在 jvm 中运行我的 robolectric 测试。 正如谷歌对他们的新构建工具所说:“新的源文件夹被识别为单元测试:src/test/java、src/testDebug/java、src/testMyFlavor/java 等。” 但正如您在下面看到的,我的测试文件夹未被识别为源文件夹。它适用于 com.github.jcandksolutions.gradle:android-unit-test:2.1.1,但不再适用于新的构建工具:

我在这里缺少什么?谢谢

【问题讨论】:

    标签: android android-gradle-plugin robolectric android-build build-tools


    【解决方案1】:

    我找到了在 IDE 左角的 Test Artifacts 之间切换的解决方案。在这个屏幕上只有“Android Instrumentation Tests”可用,因为我降级了我的 android 工具,但使用 1.1.0+ 的工具,您应该看到不同类型的测试,让 IDE 将它们识别为源文件夹。

    【讨论】:

      【解决方案2】:

      前几天我遇到了类似的问题,除了我能够在 Android Studio 中正常运行 Robolectric 测试,但不能从命令行运行。对我有用的是以下内容。

      1) 运行 ./gradlew clean assembleDebug test(而不仅仅是 clean test

      (现在,它会从主包中找到源代码,但我会得到this problem

      2) 将此添加到 build.gradle 文件中:android.sourceSets.test.java.srcDirs += "build/generated/source/r/debug"

      【讨论】:

      • 现在我收到类似com.xxx.network.requests.AuthenticateTest > callbackForNewUser FAILED java.lang.NullPointerException 的错误。我想我现在会坚持使用旧的 android-test-plugin 并等待更稳定的 gradle 构建工具版本。
      【解决方案3】:

      只需关注http://tools.android.com/tech-docs/unit-testing-support 即可启用实验性单元测试并将测试工件切换到单元测试。然后您的单元测试文件夹将被识别。

      当您遇到更多问题时,这可能会有所帮助http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html

      【讨论】:

      • 感谢您提供这些资源,但我仍然无法进行测试。我想出了如何在 IDE 中将测试文件夹“标记”为源文件夹,但是当我运行时:./gradlew clean test 我收到如下错误:error: cannot find symbol User user = UserBuilder.fromJSON(null);。我的测试类在我的主文件夹中找不到包的地方。
      • 你能更新完整的错误吗?您是否将@Config 添加到您的测试或实现了自定义运行程序?
      【解决方案4】:

      如果有任何用途,我会设置一个样板项目,允许通过使用切换构建变体来使用单元测试和 Espresso 测试。您不需要使用任何第三方插件。

      https://github.com/hitherejoe/Android-Boilerplate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-11
        • 2019-02-28
        • 1970-01-01
        相关资源
        最近更新 更多