【问题标题】:Roboelectric and Android StudioRobolectric 和 Android Studio
【发布时间】:2014-10-21 10:54:41
【问题描述】:

我已经尝试了几天,但没有结果。我需要在 Android Studio(0.8.9,最新版本)中设置 Robolectric。 我遵循了不同的教程Android Unit and Integration testingRoboelectric installation for Unit testingAndroid Gradle app with RoboelectricHow to run Roboelectric JUnit tests,但总是出现某种错误。

所以我创建了专门用于测试的模块:

Kedzoh(项目)build.gradle

// Top-level build file where you can add configuration options common to all sub-    projects/modules.
buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.12.+'
    classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
}
}

allprojects {
repositories {
    jcenter()
}
}

app build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'android'

android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
    applicationId 'com.dev.kedzoh'
    minSdkVersion 9
    targetSdkVersion 19
    versionCode 14
    versionName '1.6.7'
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:19.+'
compile 'com.google.android.gms:play-services:4.2.42'
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:19.+'
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'
}

kedzoh-tests build.gradle:

apply plugin: 'java'

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'

testCompile 'junit:junit:4.+'
testCompile 'org.robolectric:robolectric:2.2'
}

此时我无法导入 Robolectric 类,它给了我错误。当我将 apply plugin: 'robolectric' 添加到 kedzoh-tests build.gradle 时,它会要求 'android' 插件。添加后,它抱怨没有 Manifest 并且无法构建。 我不确定这是正确的配置,因为它从未真正起作用。谁能给一些建议如何在 Android Studio 中设置 Robolectric?

编辑:

我尝试了下面的答案,但仍然遇到“找不到类”错误:

【问题讨论】:

    标签: android gradle android-studio robolectric


    【解决方案1】:

    robolectric 已有不同的项目模板。你试过https://github.com/nenick/android-gradle-template 吗?

    【讨论】:

      【解决方案2】:

      我认为您可能会通过在主“应用程序”模块之外创建测试模块来使您的生活变得更加困难和复杂。我见过的大多数教程在 app 模块中都有一个 androidTest 文件夹,可以包含你的 Robolectric 测试。我见过的所有基于 Eclipse 的旧教程似乎都在指导我们创建独立于主项目的测试模块。

      如果我使用的是 Robolectric,我会像为您创建 https://github.com/marcthomas2013/Kedzoh 一样设置项目。我会使用 androidTest 文件夹来放置我的测试并配置 gradle 文件,如下所示。

      请注意,测试的依赖项是使用 androidTestCompile 声明包含的,而应用程序依赖项是使用 compile 声明的。

      这个 gradle 文件中的另一个项目是 robolectric 部分,它包含和排除 androidTest 文件夹中的类。任何不在 espresso 文件夹中的内容都将使用 gradlew 测试目标运行,包括 espresso 文件夹在内的所有内容都将在调用 gradlew connectedAndroidTest 目标时运行。

      app gradle file
      apply plugin: 'com.android.application'
      apply plugin: 'robolectric'
      
      android {
          packagingOptions {
              exclude 'LICENSE.txt'
              exclude 'META-INF/LICENSE'
              exclude 'META-INF/LICENSE.txt'
              exclude 'META-INF/NOTICE'
          }
          compileSdkVersion 19
          buildToolsVersion "19.1.0"
      
          defaultConfig {
              minSdkVersion 18
              targetSdkVersion 18
              versionCode 2
              versionName "1.0.0-SNAPSHOT"
              testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
          }
      
          buildTypes {
              release {
                  runProguard false
              }
          }
      
          sourceSets {
              androidTest {
                  setRoot('src/androidTest')
              }
          }
      }
      
      robolectric {
          include '**/*Test.class'
          exclude '**/espresso/**/*.class'
      }
      
      dependencies {
          repositories {
              mavenCentral()
          }
          compile 'com.sothree.slidinguppanel:library:2.0.1'
      
          androidTestCompile('junit:junit:4.11') {
              exclude module: 'hamcrest-core'
          }
          // Espresso
          androidTestCompile files('libs/espresso-1.1.jar')
          androidTestCompile files('libs/testrunner-1.1.jar')
          androidTestCompile files('libs/testrunner-runtime-1.1.jar')
          androidTestCompile 'com.google.guava:guava:14.0.1'
          androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
          androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
          androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
          androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
          androidTestCompile('org.robolectric:robolectric:2.3') {
              exclude module: 'classworlds'
              exclude module: 'commons-logging'
              exclude module: 'httpclient'
              exclude module: 'maven-artifact'
              exclude module: 'maven-artifact-manager'
              exclude module: 'maven-error-diagnostics'
              exclude module: 'maven-model'
              exclude module: 'maven-project'
              exclude module: 'maven-settings'
              exclude module: 'plexus-container-default'
              exclude module: 'plexus-interpolation'
              exclude module: 'plexus-utils'
              exclude module: 'wagon-file'
              exclude module: 'wagon-http-lightweight'
              exclude module: 'wagon-provider-api'
          }
          androidTestCompile 'com.squareup:fest-android:1.0.+'
      }
      

      项目构建文件 这里我们包含了 robolectric 类路径,以便我们可以在上面的配置文件中使用 robolectric gradle 命令。

      // Top-level build file where you can add configuration options common to all sub-projects/modules.
      
      buildscript {
          repositories {
              jcenter()
              mavenCentral()
          }
          dependencies {
              classpath 'com.android.tools.build:gradle:0.12.2'
              classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
      
              // NOTE: Do not place your application dependencies here; they belong
              // in the individual module build.gradle files
          }
      }
      
      allprojects {
          repositories {
              jcenter()
              mavenCentral()
          }
      }
      

      您能否解释一下您遇到了什么错误,因为根据您的工作方式可能存在许多可能的错误,以及创建单独的测试模块是否有特殊原因。

      我提供的项目基于 Deck-gradle 设置 (https://github.com/robolectric/deckard-gradle),这里有许多关于 JUnit 冲突等的提示,您可能会遇到问题。

      一旦你做到了这一点,为了能够在应用程序中调试单元测试,你将不得不在启动测试时手动调整类路径(一点也不漂亮!),基于本文中的信息@ 987654323@

      你需要运行你的单元测试,等待它失败,复制 -classpath 参数和它的值(它会很长并且以“开始和结束”)并将它复制到你的运行配置中的 VM 选项中,然后在该类路径的末尾添加一个 ; 或一个 : 取决于您的操作系统路径分隔符,并将路径添加到您的测试类,这将类似于 <ABSOLUTE_PATH_TO_PROJECT>/build/test-classes 这会将您的测试类添加到类路径,然后是 Android Studio应该能够运行它们。

      这也是假设运行gradle testClasses的步骤已经从文章http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/中完成

      【讨论】:

      • 我更新了问题,请看一下。我听从了您的建议,但收到“找不到类”错误
      • 从命令行运行“gradlew clean test”是否有效?即你能让它在 AndroidStudio 之外工作吗?
      • 我已经更新了答案的结尾,包括如何让单元测试在 Android Studio 中运行。它并不漂亮,因为它基本上是一个类路径问题,由于某种原因,工作目录被忽略了。
      • 当我尝试 gradlew clean test 时它返回“java.lang.UnsupportedOperationException”,请参阅编辑中的屏幕截图
      • 我添加了构建测试类,但它从来没有工作过
      猜你喜欢
      • 2015-05-23
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多