模块化 Android 测试 - 使用基本模块测试进行模块测试

Dmi*_*din 5 android gradle android-studio build.gradle android-gradle-plugin

我有一个包含多个模块的 android 项目,模块 B 依赖于模块 A

dependencies {
    implementation project(':features:A')
}
Run Code Online (Sandbox Code Playgroud)

我希望模块 B 中的单元测试扩展模块 A 的基础测试。IDE 成功解析了所有依赖项,但在构建时,只有模块 A 的生产代码可在测试中访问。

我尝试专门为测试添加依赖项:

dependencies {
    testCompile project(':features:A').sourceSets.test.output
}
Run Code Online (Sandbox Code Playgroud)

这会导致错误:

Could not get unknown property 'test' for SourceSet container of type org.gradle.api.internal.tasks.DefaultSourceSetContainer.
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用测试创建自定义工件,也是如此。

如何从依赖模块访问基本模块中特定于测试的代码?

AS 3.1,gradle 4.1

Dmi*_*din 1

我发现的最好的解决方案是创建第三个模块仅用于测试,将我的基本测试类和测试依赖项放在那里。并在模块 A 和模块 B(以及所有其他模块 - 只有一个模块用于所有单元测试依赖项)中使用它

testImplementation project(':features:basetests')
Run Code Online (Sandbox Code Playgroud)

缺点 - 可以将其导入非测试工件(eq实现项目(':features:basetests'))。良好的代码审查可以防止此类错误。看起来没有方便的方法来定义基本共享测试代码。