ybds4w
Last Updated: February 25, 2016
·
20.6K
· ravishanker
Af0e8c2dc298ef4e2edd5d86bbc0a658

Testing on Android Studio

After watching Google IO 2013 if you couldn't resist the temptation to try out the new svelte Android Studio. You would have soon hit a snag when it came to testing.

Unlike on Eclipse or ADT Bundle, The new Android Studio doesn't require a separate android testing project.

Good news, It's all integrated now within Studio.

There are usually the following options for testing for Android apps

  • Instrumentation Tests
  • Junit by extending AndroidTestCase (JUnit v3.7)
  • Junit (v3.8 or higher) separately
  • Popular testing frameworks like Robotium or Robolectric

For the first two scenarios you just need to create the following folder structure under src folder

  • src
  • - - - instrumentTest
  • - - - - Java
  • - - - - - com.example.spinner.test
  • - - - - - - SpinnerActivityTest.java

You can then run a particular test file or a package of instrumentation tests. The new build automatically generates required config and AndroidManifest.xml file automatically for you.

However, if you need to use JUnit for POJO testing exclusively or using popular frameworks then there is no integrated support yet in Android Studio. You would have to follow the steps below to configure gradle build system that is used in Android Studio.

In build.gradle file inside src folder create the following blocks

  • a SourceSet
  • a Configuration
  • and Dependencies
  • a test task
sourceSets {
    testLocal {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/resources')
    }
}

dependencies {
    compile 'org.roboguice:roboguice:2.0'
    compile 'com.google.android:support-v4:r6'
    testLocalCompile 'junit:junit:4.8.2'
    testLocalCompile 'org.robolectric:robolectric:2.1'
    testLocalCompile 'com.google.android:android:4.0.1.2'
    testLocalCompile 'com.google.android:support-v4:r6'
    testLocalCompile 'org.roboguice:roboguice:2.0'
}

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

It that sounds too much then do not despair there is a plugin in the making for robolectric-plugin

There is more at tools.android.com about the new gradle based build system in Android Studio.

I'm sure there will be better integrated features on the release candidate of Android Studio. Till then enjoy TDD with the sleek intelliJ based Android Studio through these hacks.

Say Thanks
Respond

2 Responses
Add your response

10801
360fe988e4bec9bb69e753b48162806d

Hi,

thank you for sharing! Unfortunately I stuck with AS 0.3.5. It says JUnit 3.8 error and I don't know how to put JUnit 4.0 dependency before Android dependency in running class path. Do you have any ideas?

Kind regards,
Eugen

over 1 year ago ·
13330
Memoafro2 normal

Eugen, it was hard but I think I got it working. I followed these instructions: http://blog.futurice.com/android_unit_testing_in_ides_and_ci_environments

over 1 year ago ·