无法获取Robotium在Android的工作室工作工作室、工作、Robotium、Android

2023-09-05 00:19:46 作者:官方小可爱√

我努力让Robotium工作,对摇篮为基础的Andr​​oid Studio和我找不到这样做的方式。

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

这是我的build.gradle文件

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

正如你所看到的,我使用Robolectric和Robotium 我有现在的问题是,每当我试图创建一个Robotium测试,像这样的:

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

无法找到任何进口,该项目并没有失败,在Android的工作室编译,它只是失败,当我运行测试,如果我删除类和依赖关系的引用,Robolectric工作得很好

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

推荐答案

据我所知,你不能成功运行Android的工作室所有测试过(见的如何在Android的Studio中创建测试?)。您需要在命令行中做到这一点(请注意,您可能需要清洁第一 ./ gradlew干净):

As far as I can tell, you can't successfully run any tests from Android Studio yet (see How can I create tests in Android Studio?). You need to do it from the command line (note that you may need to clean first ./gradlew clean):

要运行instrumentTests,使用 ./ gradlew connectedInstrumentTest

To run the instrumentTests, use ./gradlew connectedInstrumentTest

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

To run your test task, use ./gradlew localTest

对于未来的参考,当机器人工作室的作品与集成测试更好,你可以将它设置为运行任何给定的摇篮任务。进入编辑配置,然后点击+按钮添加一个新的。选择摇篮,然后将其配置为指向正确的gradle.build文件,要运行的任务。我不知道你会添加'发射前部分中的东西,但是,如果任何事情。

For future reference when Android Studio works better with test integration, you can set it up to run any given Gradle task. Go to 'Edit Configurations' and click the '+' button to add a new one. Choose 'Gradle' and then configure it to point to the correct gradle.build file and the task you want to run. I'm not sure what you would add in the 'Before launch' section, however, if anything.

之所以这么说,这里就是我得到了我的robotium测试运行:

All that being said, here's how I got my robotium tests to run:

我用内置的仪器测试的配置,因为我无法让localTest任务工作(像你一样在那里找不到任何进口,我得到了同样的错误)。我使用的是默认的文件夹结构与 instrumentTest 文件夹,但它看起来像你可以只用一行 instrumentTest.setRoot(SRC /测试) ,使其与您的安装工作是一样的。

I used the built-in Instrument Test configuration because I couldn't get the localTest task to work (I got the same errors as you did where it couldn't find any imports). I used the default folder structure with the instrumentTest folder, but it looks like you can just use the line instrumentTest.setRoot('src/test') to make it work the same with your setup.

我可以这样来配置我的build.gradle文件:

I configured my build.gradle file like this:

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
    }

}

我添加了robotium库项目结构(命令 - ; 在我的Mac)。作为一个maven libary

I added the robotium library in Project Structure (command-; on my mac) as a maven libary.

然后我写了这样一个测试:

I then wrote a test like this:

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

然后我跑了通过命令行 ./ gradlew connectedInstrumentTest 的考验,它为我工作。

Android常用的5款自动化测试工具

I then ran the test via the command line ./gradlew connectedInstrumentTest and it worked for me.