How to do Integration Testing on Android with the new Gradle Build System?
Asked Answered
A

1

11

Our Android app needs automated testing, and our group is using Robotium to handle that for us. This is no problem for unit tests, but we're also writing a set of end-to-end integration tests to exercise not only the client by the back-end servers as well. I've got some tests that do this, but if possible, I'd like to break them out separately from the unit tests so that our continuous integration builds don't require a live server to be running in order to complete.

We're using the shiny new Gradle build system. I'm wondering if I could do something like a test-only flavor or a subproject that depends on the parent APK to make it go. I tried making this work with a separate project altogether using the Robotium instructions for testing a source-less debug APK, but it didn't work. Maybe because I was on real hardware and not an emulator. I've had poor luck with the emulator, even with the hardware acceleration installed.

Any advice, or should I just hold my breath and roll with my builds requiring the integration server to be available when builds are happening?

Arsenate answered 4/6, 2013 at 2:25 Comment(0)
H
11

According to their Maven instructions all you need to do is add robotium-solo as a compile dependency.

repositories {
    mavenCentral()
}

dependencies {
    instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:4.2'
}

This will ensure you have the robotium-solo.jar file in your classpath. Then define your tests in the src/instrumentTest directory and run gradle build. See if that works?

I'll help where I can, as we converted from maven to gradle about a year ago.

*EDIT OP wanted the tests to run separately from a gradle build, so the solution is to specify a custom source set like so:

sourceSets {
    integrationTest {
        // Gives you access to the compiled classes in your tests
        compileClasspath += main.output
        runtimeClasspath += main.output
    }
}

dependencies {
    integrationTestCompile 'com.jayway.android.robotium:robotium-solo:4.2'
}

// To run the tests: ./gradlew integrationTest
task integrationTest(type: Test) {
    testClassesDir = sourceSests.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
}

Note: I don't have the android SDK installed on this computer. If main.output does not work try it with andriod.sourceSets.main.output and see if that works.

Haemophilia answered 17/7, 2013 at 18:10 Comment(4)
I've got that working. What I want is a way to segregate my server-dependent integration tests from my non-server-dependent test so that Jenkins doesn't require a running product server to work.Arsenate
Ah, I see. In that case look into making a custom sourceSet in build.gradle called integrationTest. You'll then have compileIntegrationTest available to you in your dependencies closure where you can add robotium. Add a task to run those integration tests. By specifying a custom sourceSet gradle build won't run them by default. To run them you'll need to add a custom task. I'll revise my answer with some source.Haemophilia
Wouldn't it be instrumentTestCompile?Zetes
Peter is right, it is instrumentTestCompile, not compileInstrumentTestPhotoactive

© 2022 - 2024 — McMap. All rights reserved.