我们如何跨Android库模块继承测试类? [英] How Do We Inherit Test Classes Across Android Library Modules?
问题描述
我有一个带有两个库模块的Android Studio项目:foo-module
和bar-module
.每个实现一个库,其中foo-module
定义策略接口,而bar-module
取决于foo-module
并实现此类策略. foo-module
具有工具测试(foo-module/src/androidTest/
),用于使用存根策略实现来测试其核心代码,并且bar-module
应该具有自己的工具测试.
I have an Android Studio project with two library modules: foo-module
and bar-module
. Each implements a library, with foo-module
defining a strategy interface and bar-module
depending upon foo-module
and implementing such a strategy. foo-module
has instrumentation tests (foo-module/src/androidTest/
) to test its core code, using a stub strategy implementation, and bar-module
should have its own instrumentation tests.
我在foo-module/src/androidTest/
中定义了一个AbstractTests
类,该类可以进行大多数实际测试.我在foo-module/src/androidTest/
中还有一个StubTests
类,该类扩展了AbstractTests
并实现了必要的abstract
方法以完成测试用例(提供了策略的实现,等等).这一切都很好.
I defined an AbstractTests
class in foo-module/src/androidTest/
that does most of the actual testing. I also have a StubTests
class in foo-module/src/androidTest/
that extends AbstractTests
and implements the necessary abstract
methods to complete the test case (providing an implementation of the strategy, etc.). This all works great.
在bar-module/src/androidTest/
中,我创建了一个BarStrategyTests
类,该类旨在镜像StubTests
,但提供了在bar-module
中实现的策略.但是,即使我的build.gradle
文件中包含compile project(':foo-module')
,BarStrategyTests
也看不到AbstractTests
,并且bar-module
中的主(非测试)类可以与主(非测试)类正常工作在foo-module
中. IOW,虽然project()
依赖项处理常规代码,但不处理androidTest/
代码.我收到错误:包com.commonsware.foo.test不存在".
In bar-module/src/androidTest/
, I created a BarStrategyTests
class, designed to mirror StubTests
, but provide the strategy implemented in bar-module
. However, BarStrategyTests
cannot see AbstractTests
, even though I have compile project(':foo-module')
in my build.gradle
file, and the main (non-test) classes in bar-module
can work fine with the main (non-test) classes in foo-module
. IOW, while the project()
dependency handles regular code, it does not handle androidTest/
code. I get "error: package com.commonsware.foo.test does not exist".
我也尝试添加androidTestCompile project(':foo-module')
,结果相同.
I tried also adding androidTestCompile project(':foo-module')
, with the same result.
在模块之间共享仪器测试代码的秘诀是什么?
What is the recipe for sharing instrumentation test code between modules?
暂时,我可以克隆AbstractTests
,但这不是一个很好的长期解决方案.
Temporarily, I can clone AbstractTests
, but that's not a great long-term solution.
此SO问题涵盖了普通Java的类似情况.有没有人尝试过一个答案中的选项,并使其适用于Android工具测试?第一种选择(将通用测试代码作为常规的非测试代码移动到另一个模块中)似乎是合理的,但是我不知道其他两个选项是否可以与com.android.library
插件而不是java
插件一起很好地工作./p>
This SO question covers similar ground for ordinary Java. Has anyone tried the options in the one answer and gotten them to work for Android instrumentation tests? The first option (move the common testing code into yet another module as regular non-test code) seems plausible, but I have no idea if the other two will work well with the com.android.library
plugin instead of the java
plugin.
推荐答案
由于所有测试类(单元和仪器)都不是任何模块(包括aar)的一部分,因此无法通过对它们的依赖来获得它们模块.我也面临这个问题,并通过创建test-module
并将其放入所有必需的类(src/main/java
)来解决.
因此,根据您的情况,您可以在此模块中移动AbstractTests
并将该模块用作androidTestCompile
依赖项.
Due to the fact that all the test classes (unit and instrumentation) are not a part of any module, including aar, they are not available via dependency to that module. I faced to this issue as well and solved it by creating test-module
and put all the required classes in it (src/main/java
).
So, in your case you can move AbstractTests
in this module and use this module as a androidTestCompile
dependency.
这篇关于我们如何跨Android库模块继承测试类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!