在Android Studio中的Android Instrumentation测试和单元测试之间共享代码 [英] Sharing code between Android Instrumentation Tests and Unit Tests in Android Studio

查看:199
本文介绍了在Android Studio中的Android Instrumentation测试和单元测试之间共享代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在Android Studio中的这两种测试模式之间共享代码?
我在两种测试模式下都需要访问一组Mock Utils类。

It is possible to share code between this two test modes in Android Studio? I have a set of Mock Utils class's that I need to access in both of the test modes.

推荐答案

最后,我发现解决方案(解决方法)得益于Dan Lew的博客文章( http://blog.danlew.net/ 2015/11/02 / sharing-code-between-unit-tests-and-instrumentation-tests-on-android / )。

Finally I found the solution (workaround) thanks to a blog post from Dan Lew (http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/).


我提出的解决方案是利用源集来定义通用代码。首先,我将共享的测试代码放入src / sharedTest / java1中。

The solution I've come up with is to leverage source sets to define common code. First, I put my shared test code into src/sharedTest/java1 .



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




上面的操作是将我的共享代码目录添加到两个测试和androidTest源码集。现在,除了它们的默认Java源代码之外,它们还将包含共享代码。

What it's doing above is adding my shared code directory to both the test and androidTest source sets. Now, in addition to their default Java sources, they'll also include the shared code.

这篇关于在Android Studio中的Android Instrumentation测试和单元测试之间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆