简体   繁体   English

Robolectric和Gradle(Android工作室)的Android项目

[英]Android project with Robolectric and Gradle (Android studio)

I'm trying to use Robolectric in a project build with gradle inside the new Ide for android: Android studio, but I'm facing a strange problem, I've correctly imported all the libraries and created the "test" folder inside "src", the fact is that whenever I run the tests the ide keep saying "Class not found: "com.example.myandroidproject.test" what I'm doing wrong? i need to change something in the gradle.build? here's my directory structure: 我正在尝试使用Robolectric在一个项目构建中使用gradle在新的ide for android:Android studio中,但我面临一个奇怪的问题,我已经正确导入了所有库并在“src”中创建了“test”文件夹“,事实是每当我运行测试时,ide会继续说”Class not found:“com.example.myandroidproject.test”我做错了什么?我需要在gradle.build中更改一些内容吗?这是我的目录结构体:

在此输入图像描述

@Aldo Borrero, finally it seems that someone has found the way to test android projects under "Android Studio" using Robolectric and Gradle. @Aldo Borrero,最后似乎有人找到了使用Robolectric和Gradle在“Android Studio”下测试android项目的方法。 Please, take a look at this answer Robolectric with Gradle 请看看这个答案Robolectric with Gradle

Update: The guys from square have released a plugin to make Robolectric work out of the box with Gradle and Android Studio, this feature will be integrated with Robolectric in v2, meanwhile you can grab the plugin here: Gradle Android test Plugin 更新:来自广场的人们发布了一个插件,让Robolectric与Gradle和Android Studio一起开箱即用,这个功能将在v2中与Robolectric集成,同时你可以在这里获取插件: Gradle Android测试插件

I tried different appraoaches to combine android studio & robolectric & espresso. 我尝试了不同的appraoaches结合android studio&robolectric和espresso。 I ended with this example project setup https://github.com/nenick/android-gradle-template 我结束了这个示例项目设置https://github.com/nenick/android-gradle-template

Here some explanation for the different approaches: 这里有一些不同方法的解释:

application module + espresso + robolectric 应用模块+ espresso + robolectric

There is an example https://github.com/robolectric/deckard-gradle supported by robolectric maintainers. robolectric维护者支持一个示例https://github.com/robolectric/deckard-gradle This is based on the plugin https://github.com/robolectric/gradle-android-test-plugin . 这是基于插件https://github.com/robolectric/gradle-android-test-plugin But this have a drawback with dependency pollution reported at https://github.com/robolectric/gradle-android-test-plugin/issues/17 which results in slow esspresso tests compile time and execution time. 但这有一个缺点,在https://github.com/robolectric/gradle-android-test-plugin/issues/17上报告了依赖性污染,导致esspresso测试编译时间和执行时间变慢。

build.gradle snippet which combines all build.gradle片段,它结合了所有

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.10.+'
        classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
    }
}

apply plugin: 'android'
apply plugin: 'android-test'

android {
    defaultConfig {
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
}

androidTest {
    include '**/*Test.class'
    exclude '**/espresso/**/*.class'
}

dependencies {
    androidTestCompile('junit:junit:4.11')
    androidTestCompile('org.robolectric:robolectric:2.3-SNAPSHOT')
    androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}

seperate espresso 单独的浓咖啡

An example is shown by https://github.com/stephanenicolas/Quality-Tools-for-Android but it is much outdated and had also some drawbacks. 一个例子由https://github.com/stephanenicolas/Quality-Tools-for-Android显示,但它已经过时并且也有一些缺点。 It will recompile and makes android studio behave strange. 它将重新编译并使android studio表现得很奇怪。 It flags application module sources as (root source) of the espresso test module. 它将应用程序模块源标记为espresso测试模块的(根源)。 That works but not intuitive. 这有效但不直观。

build.gradle snippet for espresso module espresso模块的build.gradle片段

dependencies {
    androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}

android {
    sourceSets {
        main {
            manifest.srcFile '../AndroidSample/AndroidManifest.xml'
            java.srcDirs += ['../AndroidSample/src/main/java']
            resources.srcDirs = ['../AndroidSample/res']
            res.srcDirs = ['../AndroidSample/res']
        }
    }
    defaultConfig {
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }

}

spereate robolectric spereate robolectric

There exist a plugin https://github.com/novoda/gradle-android-test-plugin which enable us to put robolectric tests in a sperate package. 存在一个插件https://github.com/novoda/gradle-android-test-plugin ,它使我们能够将robolectric测试放入sperate包中。 This project setup works for me great: 这个项目设置对我很有用:

- MyProject 
|- app (with espresso tests)
|- - build.gradle (app)
|- robolectric (unit tests)
|- - build.gradle (robo)

build.gradle (app + espresso) snippet build.gradle(app + espresso)片段

dependencies {
    androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}            

android {
    defaultConfig {
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
}    

build.gradle (robo) snippet build.gradle(robo)片段

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath "com.novoda:gradle-android-test-plugin:0.9.8-SNAPSHOT"
    }
}

android {
    projectUnderTest ':AndroidSample'
}

apply plugin: 'java'
apply plugin: 'android-test'

dependencies {
    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-core:1.9.5'
    testCompile 'com.squareup:fest-android:1.0.+')
    testCompile ('org.robolectric:robolectric:2.3-SNAPSHOT')
}

There a some pitfall when you try setup this project setup, so just start with a working example: https://github.com/nenick/android-gradle-template 尝试设置此项目设置时会有一些陷阱,所以只需从一个工作示例开始: https//github.com/nenick/android-gradle-template

This is unlikely to work out of the box as src/test isn't used automatically. 这不太可能开箱即用,因为src / test没有自动使用。 You'd need to create a test task automatically that compiles this source sets, sets the right dependencies and run it. 您需要自动创建一个测试任务,编译此源集,设置正确的依赖项并运行它。

We intend to support this in the future but right now you'd need to do this manually. 我们打算在将来支持这一点,但现在您需要手动执行此操作。

I tested all the solutions presented here and they all lack of something (version of gradle / gradle plugin not supported, library project not supported, no integration with Android studio, etc). 我测试了这里提供的所有解决方案,它们都缺少某些东西(不支持gradle / gradle插件版本,不支持库项目,不与Android studio集成等)。 It may not be true in the future but it is today. 未来可能不是这样,但今天却是如此。

The best way I found is to configure the unit tests by yourself. 我发现的最好方法是自己配置单元测试。 You will need to add a few lines of config to your build.gradle file. 您需要在build.gradle文件中添加几行配置。 Explications are on the following article: http://tryge.com/2013/02/28/android-gradle-build/ . 有关以下文章的解释: http//tryge.com/2013/02/28/android-gradle-build/ Since I'm not the author, I don't think I can copy past the content here directly. 由于我不是作者,我认为我不能直接复制这里的内容。

In addition to that article, if you want configure Android Studio to see the unit test folder as a source folder (autocompletion and stuff), you can apply the following little dirty hack and let the IDE think that the unit tests are located in the instrumentationTest folder. 除了那篇文章之外,如果你想配置Android Studio将单元测试文件夹看作源文件夹(自动完成和东西),你可以应用下面的小脏攻,让IDE认为单元测试位于instrumentationTest中夹。 Of course, it will mess with your real instrumentation tests so it works only if you don't have any of those. 当然,它会弄乱您的真实仪器测试,所以只有当您没有任何这些测试时它才有效。

build.gradle 的build.gradle

// the unit test source set as described in the article
sourceSets {
    unitTest {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/resources')
    }
}

android {
    // tell Android studio that the instrumentTest source set is located in the unit test source set
    sourceSets {
        instrumentTest.setRoot('src/test')
    }
}

dependencies {
    // your unit test dependencies as described in the article
    unitTestCompile files("$project.buildDir/classes/release")
    unitTestCompile 'junit:junit:4.11'
    unitTestCompile 'com.google.android:android:4.1.1.4'
    unitTestCompile 'org.robolectric:robolectric:2.1.1'

    // duplicate these dependencies in the instrumentTestCompile scope 
    // in order to have the integration in Android Studio (autocompletion and stuff)
    instrumentTestCompile 'junit:junit:4.11'
    instrumentTestCompile 'org.robolectric:robolectric:2.1.1'
}

// the rest of the config as described in the article

Tested with Android Studio 0.2.6 and android gradle plugin 0.5. 测试Android Studio 0.2.6和android gradle插件0.5。

Gradle Android Unit Testing Plugin is the best option for me. Gradle Android Unit Testing Plugin对我来说是最好的选择。
Developed by Jake Wharton, I guess it is going to be the next standard (maybe until google releases an out of the box support for Robolectric in Android Studio). 由Jake Wharton开发,我想它将成为下一个标准(可能直到谷歌发布Android Studio中对Robolectric的开箱即用支持)。

You can import the library by adding in your build.gradle file: 您可以通过添加build.gradle文件来导入库:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.X.+'
        classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.+'
    }
}
...
apply plugin: 'android-test'
...

dependencies {
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.1.+'
testCompile 'com.squareup:fest-android:1.0.+'
}

Update: This library has been deprecated since gradle plugin version 0.8 更新:自gradle插件版本0.8以来,此库已被弃用

I've tested a lot of scenarios (like http://tryge.com/2013/02/28/android-gradle-build/ and http://www.peterfriese.de/android-testing-with-robolectric/ ) but only the solution provided by the Robolectric team worked for me. 我已经测试了很多场景(例如http://tryge.com/2013/02/28/android-gradle-build/http://www.peterfriese.de/android-testing-with-robolectric/ )但只有Robolectric团队提供的解决方案才能为我工作。 The setup uses instrumented and robolectric tests in one Android project with gradle as build system. 该设置在一个Android项目中使用了instrumented和robolectric测试,并将gradle作为构建系统。

See http://robolectric.org/getting_started/ and the sources on https://github.com/robolectric/deckard-gradle 请参阅http://robolectric.org/getting_started/以及https://github.com/robolectric/deckard-gradle上的来源

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.2'
        classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.4'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'android'
apply plugin: 'android-test'

android {
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
    }
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    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 {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            res.srcDirs = ['res']
        }
        androidTest {
            setRoot('src/test')
        }
    }
}

androidTest {
    include '**/*Test.class'
    exclude '**/espresso/**/*.class'
}

dependencies {
    repositories {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
    // Espresso
    androidTestCompile files('lib/espresso-1.1.jar', 'lib/testrunner-1.1.jar', 'lib/testrunner-runtime-1.1.jar')
    androidTestCompile 'com.google.guava:guava:14.0.1',
            'com.squareup.dagger:dagger:1.1.0',
            'org.hamcrest:hamcrest-integration:1.1',
            'org.hamcrest:hamcrest-core:1.1',
            'org.hamcrest:hamcrest-library:1.1'

    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile('org.robolectric:robolectric:2.3-SNAPSHOT') {
        exclude module: 'classworlds'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-plugin-registry'
        exclude module: 'maven-profile'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'nekohtml'
        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-http-shared'
        exclude module: 'wagon-provider-api'
    }
    androidTestCompile 'com.squareup:fest-android:1.0.+'
}

apply plugin: 'idea'

idea {
    module {
        testOutputDir = file('build/test-classes/debug')
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM