Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating integration tests from unit tests in Android Studio

I'm trying to separate out integration tests in Android Studio 0.9.

I have added the following to the build file:

sourceSets {
    integrationTest {
        java.srcDir file('src/integrationTest/java')
    }
}

task integrationTest(type: Test) {
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
}

I've run into a couple of issues:

  1. The task will run but it doesn't have the rest of the project files available so I get errors about missing classes. There are some Java specific solutions I've found such as:

    • http://selimober.com/blog/2014/01/24/separate-unit-and-integration-tests-using-gradle/
    • https://blog.safaribooksonline.com/2013/08/22/gradle-test-organization/

    But I haven't been able to figure out how to get this to work with Android Studio. Various combinations of main and main.output and playing around with dependencies don't seem to work, I get errors like:

    Error:(33, 0) Could not find property 'main' on SourceSet container..
    

    Which makes sense as the android plugin defines its own source sets, but these don't work either.

  2. The IDE doesn't recognise the directory as a test source directory. For testing purposes I changed the source set name to androidTest and it correctly gets the green folder icon and the tests are run along with the existing unit tests that are already defined in androidTest.

like image 237
Philio Avatar asked Nov 01 '14 23:11

Philio


People also ask

Should you separate unit and integration tests?

In general: yes, you should put integration tests and unit tests into different folders. Often, programmers don't draw a clear line between these two kinds of tests and just write whatever kind of test is useful. But integration tests tend to be slower, because they often involve: Database queries.

Why do we separate units and integration tests?

Separating things Usually unit tests are fast and even if you run them by mistake, you won't feel uncomfortable. Integration, contract and acceptance tests can take several minutes if the project is huge. That's why it makes sense to separate integration and unit tests.

What is the difference between integration test and unit test?

Unit Testing is a kind of white box testing, whereas Integration Testing is a kind of black-box testing. For Unit Testing, accessibility of code is required, as it tests the written code, while for Integration Testing, access to code is not required, since it tests the interactions and interfaces between modules.

Can integration tests replace unit tests?

Unit testing and integration testing are both important parts of successful software development. Although they serve different yet related purposes, one cannot replace the other.


1 Answers

@sm4's answer works indeed for a Java module (with apply plugin: 'java'), but unfortunately not for Android application (apply plugin: 'com.android.application') nor Android library modules (apply plugin: com.android.library).

But I have found a workaround:
Create the folders for your integration tests:

src/integrationTest/java
src/integrationTest/res

Add the sourceSets for your new folders:

sourceSets {
    integrationTest {
        java {
            srcDir file('src/integrationTest/java')
        }
        res {
            srcDir file('src/integrationTest/res')
        }
    }
}

In a pure Java module the java folder would now turn green and the res folder icon would change. In an Android application/library module it does not.

Now create a product flavor identically named as the folder configured in the sourceSet, and it works!

productFlavors {
    integrationTest {
    }
}

And to put a cherry on top:

configurations {
    integrationTestCompile.extendsFrom testCompile
}
like image 136
Rule Avatar answered Oct 26 '22 21:10

Rule