8

Now that Google has added experimental unit test support, how might one go about sharing resources across both unit tests and instrumentation test?

For example, say I have a TestUtils.java class that I want accessible in both my unit tests and my instrumentation tests. If I put it in my src/test/java folder, it will be accessible to my unit tests. If I put it in my src/androidTest/java folder, it will be accessible to my instrumentation tests. How do I make it accessible to both?

The only solution I see right now is putting it in src/debug/java, but is there a better way?

skoush
  • 380
  • 2
  • 10
  • You can always tell Gradle that there is more than one directory of source to pull from. I don't know if there's a cleaner answer, though. – CommonsWare Mar 24 '15 at 22:10
  • 2
    @CommonsWare good idea! Adding `android.sourceSets.androidTest.java.srcDirs = ['src/androidTest/java', 'src/test/java']` and vice-versa worked – skoush Mar 24 '15 at 23:01
  • This article covers that exact topic: http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/ – Jacob R Aug 26 '17 at 11:18

1 Answers1

4
android {  
  sourceSets {
    String sharedTestDir = 'src/sharedTest/java'
    test {
      java.srcDir sharedTestDir
    }
    androidTest {
      java.srcDir sharedTestDir
    }
  }
}
laomo
  • 436
  • 4
  • 12
  • 1
    While this code may answer the question, providing additional context regarding *why* and/or *how* this code answers the question improves its long-term value. – Benjamin W. Mar 26 '16 at 07:27
  • 1
    What's about doing this accross the modules? – X-HuMan Jan 15 '17 at 14:13