在Android中的单元测试和仪器测试之间共享资源

在Android中的单元测试和仪器测试之间共享资源

问题描述:

现在Google已添加实验单元测试支持,如何共享资源跨单元测试和仪器测试?

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

例如,假设我有一个TestUtils.java类,我希望在我的单元测试和仪器测试中都可以访问该类.如果将其放在src/test/java文件夹中,则单元测试可以访问它.如果将它放在我的src/androidTest/java文件夹中,我的仪器测试将可以使用它.如何使两者都可以访问?

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?

我现在看到的唯一解决方案是将其放在src/debug/java中,但是有更好的方法吗?

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

android {  
  sourceSets {
    String sharedTestDir = 'src/sharedTest/java'
    test {
      java.srcDir sharedTestDir
    }
    androidTest {
      java.srcDir sharedTestDir
    }
  }
}