12

I'm struggling to get Robotium to work on the gradle-based Android Studio and I can't find the way to do it

This is my build.gradle file

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

As you can see, I'm using Robolectric and Robotium The problem I've got is whenever I try to create a Robotium test, like this one:

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();
    }
}

it cannot find any import, the project does not fail to compile in Android Studio, it just fails when I run the tests and if I remove the class and the references in the dependences, Robolectric works just fine

4

1 回答 1

20

据我所知,您还无法从 Android Studio 成功运行任何测试(请参阅如何在 Android Studio 中创建测试?)。您需要从命令行执行此操作(请注意,您可能需要先清理./gradlew clean):

要运行 instrumentTests,请使用./gradlew connectedInstrumentTest

要运行您的测试任务,请使用./gradlew localTest

为了将来在 Android Studio 与测试集成更好地配合时参考,您可以将其设置为运行任何给定的 Gradle 任务。转到“编辑配置”并单击“+”按钮添加一个新配置。选择“Gradle”,然后将其配置为指向正确的 gradle.build 文件和您要运行的任务。但是,如果有的话,我不确定您会在“发布前”部分添加什么。

话虽如此,这就是我运行机器人测试的方式:

  1. 我使用了内置的 Instrument Test 配置,因为我无法让 localTest 任务工作(我遇到了与您在找不到任何导入时相同的错误)。我对文件夹使用了默认文件夹结构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. 我在项目结构(在我的mac上)中添加了robotium库command-;作为一个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 回答