Roboelectric和Android Studio

8
我已经尝试了几天,但没有结果。我需要在Android Studio(0.8.9,最新版本)中设置Robolectric。 我按照不同的教程Android单元和集成测试Roboelectric安装用于单元测试使用Roboelectric的Android Gradle应用程序如何运行Roboelectric JUnit测试,但总是会出现某种错误。
因此,我创建了专门用于测试的模块: enter image description here enter image description here 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类,它会给我报错。当我在kedzoh-tests build.gradle中添加apply plugin: 'robolectric'时,它要求添加'android' plugin。添加后,它抱怨没有清单文件并且构建失败。
我不确定这是否是正确的配置,因为它从未真正起作用过。请问有人能给出如何在Android Studio中设置Robolectric的建议吗?
编辑:
我尝试了下面的答案,但仍然卡在“找不到类”的错误上:
2个回答

2

0

我认为,如果你将测试模块创建在主要的“app”模块之外,可能会让你的生活变得更加困难和复杂。我看过的大多数教程都有一个名为androidTest的文件夹,可以包含你的Robolectric测试。所有基于Eclipse的旧教程似乎都指示我们创建独立于主项目的测试模块。

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

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

此gradle文件中的另一项内容是robolectric部分,在其中包括和排除androidTest文件夹中的类。任何不在espresso文件夹中的内容都将使用gradlew test目标运行,而包括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冲突等问题的提示,你可能会遇到麻烦。

一旦你完成了这一步,为了能够在应用程序中调试单元测试,你将不得不在启动测试时手动调整类路径(非常不美观!),根据这篇文章中的信息 https://github.com/codepath/android_guides/wiki/Robolectric-Installation-for-Unit-Testing

你需要运行单元测试,等待它失败,复制-classpath参数及其值(它会很长,以"开头和结尾),并将其复制到运行配置的VM选项中,然后在类路径的末尾加上一个;或:取决于您的OS路径分隔符,并添加路径到您的测试类,路径可能是这样的<ABSOLUTE_PATH_TO_PROJECT>/build/test-classes 这将把您的测试类添加到类路径中,然后Android Studio就可以运行它们了。

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


我更新了问题,请看一下。我按照你的建议操作,但是出现了“找不到类”的错误。 - Roman
如果你在命令行中运行“gradlew clean test”,它是否有效?也就是说,你能否在AndroidStudio之外使其工作? - Marc Thomas
我已经更新了答案的结尾,包括如何从Android Studio中运行单元测试。这并不美观,因为它基本上是一个类路径问题,由于某种原因,工作目录被忽略了。 - Marc Thomas
当我尝试运行gradlew clean test时,它会返回“java.lang.UnsupportedOperationException”的错误提示,详见编辑中的截图。 - Roman
我已经有了我的工作仓库,所以我尝试将Robolectrics集成到其中。但是它非常容易出错,所以我现在放弃了。无论如何,还是谢谢你的帮助! - Roman
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接