Android 用の新しい Ide: Android スタジオ内で gradle を使用してプロジェクト ビルドで Robolectric を使用しようとしていますが、奇妙な問題に直面しています。すべてのライブラリを正しくインポートし、「src」内に「test」フォルダーを作成しました。 "、実際には、テストを実行するたびに、「クラスが見つかりません: "com.example.myandroidproject.test" 何が間違っているのですか? gradle.build で何かを変更する必要がありますか? これが私のディレクトリです。構造:
6 に答える
@Aldo Borrero、ついに誰かがRobolectricとGradleを使用して「Android Studio」の下でAndroidプロジェクトをテストする方法を見つけたようです。この回答を見てくださいRobolectric with Gradle
更新: Square の担当者が、Robolectric を Gradle と Android Studio ですぐに使えるようにするプラグインをリリースしました。この機能は、v2 で Robolectric と統合されます。その間、プラグインはここから取得できます: Gradle Android test Plugin
Androidスタジオとロボレクトリックとエスプレッソを組み合わせるために、さまざまなアプローチを試みました。このサンプルプロジェクトのセットアップで終了しましたhttps://github.com/nenick/android-gradle-template
ここで、さまざまなアプローチについて説明します。
アプリケーション モジュール + エスプレッソ + ロボレクトリック
Robolectric メンテナーによってサポートされているhttps://github.com/robolectric/deckard-gradleの例があります。これはプラグインhttps://github.com/robolectric/gradle-android-test-pluginに基づいています。しかし、これにはhttps://github.com/robolectric/gradle-android-test-plugin/issues/17で報告されている依存関係の汚染に関する欠点があり、espresso テストのコンパイル時間と実行時間が遅くなります。
すべてを組み合わせた 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'
}
別のエスプレッソ
例はhttps://github.com/stephanenicolas/Quality-Tools-for-Androidに示されていますが、かなり時代遅れで、いくつかの欠点もありました。再コンパイルされ、Androidスタジオの動作が奇妙になります。espresso テスト モジュールの (ルート ソース) としてアプリケーション モジュール ソースにフラグを立てます。それは機能しますが、直感的ではありません。
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"
}
}
ロボレクトリックを実行する
robolectric テストを個別のパッケージに入れることを可能にするプラグインhttps://github.com/novoda/gradle-android-test-pluginが存在します。このプロジェクトのセットアップは私にとって素晴らしいものです:
- MyProject
|- app (with espresso tests)
|- - build.gradle (app)
|- robolectric (unit tests)
|- - build.gradle (robo)
build.gradle (アプリ + エスプレッソ) スニペット
dependencies {
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}
android {
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
}
build.gradle (ロボ) スニペット
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')
}
このプロジェクトのセットアップをセットアップしようとすると、いくつかの落とし穴があるため、実際の例から始めてください: https://github.com/nenick/android-gradle-template
src/test は自動的に使用されないため、これはそのままでは機能しない可能性があります。このソース セットをコンパイルし、適切な依存関係を設定して実行するテスト タスクを自動的に作成する必要があります。
将来的にはこれをサポートする予定ですが、現時点では手動で行う必要があります。
ここに提示されているすべてのソリューションをテストしましたが、すべて何かが欠けていました (サポートされていないバージョンの gradle / gradle プラグイン、サポートされていないライブラリ プロジェクト、Android Studio との統合がないなど)。将来はそうではないかもしれませんが、今日はそうです。
私が見つけた最良の方法は、単体テストを自分で構成することです。build.gradle ファイルに数行の構成を追加する必要があります。説明は次の記事にあります: http://tryge.com/2013/02/28/android-gradle-build/。私は作者ではないので、ここの内容を直接コピペすることはできないと思います。
その記事に加えて、単体テスト フォルダーをソース フォルダー (オートコンプリートなど) として表示するように Android Studio を構成する場合は、次のちょっとした汚いハックを適用して、単体テストが instrumentationTest にあると IDE に認識させることができます。フォルダ。もちろん、それは実際のインストルメンテーション テストを台無しにするので、それらのいずれも持っていない場合にのみ機能します。
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
Android Studio 0.2.6 および Android gradle プラグイン 0.5 でテスト済み。
Gradle Android Unit Testing Pluginは、私にとって最良の選択肢です。
Jake Wharton によって開発されたもので、次の標準になると思います (おそらく、Google が Android Studio で Robolectric のすぐに使えるサポートをリリースするまで)。
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.+'
}
更新: このライブラリは、gradle プラグイン バージョン 0.8 以降、非推奨になっています。
私は多くのシナリオをテストしました ( http://tryge.com/2013/02/28/android-gradle-build/やhttp://www.peterfriese.de/android-testing-with-robolectric/など)しかし、Robolectric チームによって提供されたソリューションだけが私にとって役に立ちました。このセットアップでは、ビルド システムとして gradle を使用する 1 つの Android プロジェクトで、インストルメント化された robolectric テストを使用します。
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')
}
}