12

Robotium を gradle ベースの Android Studio で動作させるのに苦労していますが、その方法が見つかりません。

これは私のbuild.gradleファイルです

buildscript {
    dependencies {
        repositories {
            mavenCentral()
            mavenLocal()
        }

        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

repositories {
    mavenCentral()
   /* maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }*/
}

sourceSets {
    testLocal {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/resources')
    }
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 17
    }

    sourceSets {
        instrumentTest.setRoot('src/test')
    }
}

dependencies {
    compile 'com.android.support:support-v4:13.0.+'

    // Dependencies for the `testLocal` task, make sure to list all your global dependencies here as well
    testLocalCompile 'junit:junit:4.11'
    testLocalCompile 'com.google.android:android:4.1.1.4'
    testLocalCompile 'com.android.support:support-v4:13.0.+'
    testLocalCompile 'org.robolectric:robolectric:2.1.+'
    testLocalCompile 'com.jayway.android.robotium:robotium-solo:4.2'

    // Android Studio doesn't recognize the `testLocal` task, so we define the same dependencies as above for instrumentTest
    // which is Android Studio's test task
    instrumentTestCompile 'junit:junit:4.11'
    instrumentTestCompile 'com.google.android:android:4.1.1.4'
    instrumentTestCompile 'com.android.support:support-v4:13.0.+'
    instrumentTestCompile 'org.robolectric:robolectric:2.1.+'
    instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:4.2'


}

task localTest(type: Test, dependsOn: assemble) {
    testClassesDir = sourceSets.testLocal.output.classesDir

    android.sourceSets.main.java.srcDirs.each { dir ->
        def buildDir = dir.getAbsolutePath().split('/')
        buildDir =  (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')

        sourceSets.testLocal.compileClasspath += files(buildDir)
        sourceSets.testLocal.runtimeClasspath += files(buildDir)
    }

    classpath = sourceSets.testLocal.runtimeClasspath
}




check.dependsOn localTest

ご覧のとおり、私は Robolectric と Robotium を使用しています。問題は、次のような Robotium テストを作成しようとするたびに発生することです。

import android.test.ActivityInstrumentationTestCase2;

import com.dlv.testing.MainActivity;
import com.jayway.android.robotium.solo.Solo;

public class MainActivityUITest extends
        ActivityInstrumentationTestCase2<MainActivity> {

    private Solo solo;

    public MainActivityUITest() {
        super(MainActivity.class);
    }

    public void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testStuff() throws Exception {
        solo.assertCurrentActivity("Check on first Activity", MainActivity.class);
        solo.sendKey(Solo.MENU);

    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
    }
}

インポートが見つからず、プロジェクトは Android Studio でコンパイルに失敗しません。テストを実行すると失敗するだけで、クラスと依存関係の参照を削除すると、Robolectric は問題なく動作します。

4

1 に答える 1

20

私が知る限り、まだ Android Studio からテストを正常に実行することはできません (Android Studio でテストを作成するにはどうすればよいですか? を参照してください)。コマンドラインから実行する必要があります (最初に clean が必要になる場合があることに注意してください./gradlew clean):

instrumentTests を実行するには、次を使用します。./gradlew connectedInstrumentTest

テスト タスクを実行するには、次を使用します。./gradlew localTest

Android Studio がテスト統合でより適切に動作するようになった場合の今後の参考のために、特定の Gradle タスクを実行するように設定できます。「構成の編集」に移動し、「+」ボタンをクリックして新しい構成を追加します。「Gradle」を選択し、正しい gradle.build ファイルと実行するタスクを指すように構成します。ただし、「ローンチ前」セクションに何を追加するかはわかりません。

そうは言っても、ロボティウムのテストを実行する方法は次のとおりです。

  1. localTest タスクを機能させることができなかったため、組み込みの Instrument Test 構成を使用しました (インポートが見つからなかった場合と同じエラーが発生しました)。フォルダーにはデフォルトのフォルダー構造を使用しましたinstrumentTestが、この行を使用するだけで、セットアップと同じように機能するように見えinstrumentTest.setRoot('src/test')ます。

  2. build.gradle ファイルを次のように構成しました。

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.5.+'
        }
    }
    apply plugin: 'android'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        // Included library modules
        ...
    
        // My regular dependencies
        compile 'com.android.support:support-v4:13.0.0'
        ...
    
        // Test dependencies. Notice that all I have to include is robotium
        // because the rest just works using the instrumentTest configuration
        instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:4.3'
    }
    
    android {
        compileSdkVersion 17
        buildToolsVersion "18.0.1"
    
        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 16
        }
    
    }
    
  3. command-;プロジェクト構造(私のMac上)にrobotiumライブラリをmavenライブラリとして追加しました。

  4. 次に、次のようなテストを書きました。

    import android.test.ActivityInstrumentationTestCase2;
    import com.example.activity.WelcomeScreen;
    import com.jayway.android.robotium.solo.Solo;
    
    public class WelcomeScreenTest extends ActivityInstrumentationTestCase2<WelcomeScreen> {
        private Solo solo;
    
        public WelcomeScreenTest() {
            super(WelcomeScreen.class);
        }
    
        protected void setUp() throws Exception {
            super.setUp();
    
            solo = new Solo(getInstrumentation(), getActivity());
        }
    
        public void testActivity() {
            // robotium assert
            solo.assertCurrentActivity("Welcome Screen", WelcomeScreen.class);
            // junit assert
            assertEquals(true, true);
        }
    }
    
  5. 次に、コマンドラインからテストを実行したところ、うまくいき./gradlew connectedInstrumentTestました。

于 2013-09-18T18:58:27.317 に答える