Today I would like to talk about sharing code between local and instrumentation tests on Android. As example, you have some classes which you would like to use for both types of tests (instrumentation and local), it can be test data, rules, dependency injection stuff, etc. Fortunately, you can configure your project with gradle and it allows you configure source sets for different parts of your project, I mean source code, local tests and instrumentation tests. We should update build.gradle file.

android {
    ...
    sourceSets {
        def commonTestDir = 'src/commonTest/java'
        test {
            java.srcDir commonTestDir
        }
        androidTest {
            java.srcDir commonTestDir
        }
    }
}

In my case this folder called common, however it can be called as you want. You can find a sample of using it in this small project.

result of sharing code between local and instrumentation tests on Android

P.S.: You can find local and instrumentation tests in project for testing SQLite database and ContentProviders. And use both type of tests for testing database, it’s overhead, but this project was created for talking about “Testing Database in Android”.

If this article was helpful to you, please share it. You can also subscribe to me on Twitter and get more useful information connected to Android development.

Share Share on Reddit0Share on VKTweet about this on TwitterShare on LinkedIn5Share on Google+0Share on Facebook3Flattr the authorEmail this to someoneShare on Tumblr0