testing - 反馈 `test-support`代码的gradle配置

标签 testing gradle multi-project

我最近一直在思考这个问题,想就我几天前的想法获得一些反馈。

问题:

在典型的代码库中,每个模块都有一个 main和一个 test源集。这在一段时间内可以很好地工作,但迟早我总是会遇到这样的情况,我想将一堆类组合在一起,这样可以更轻松地测试涉及某个模块的代码。一个很好的例子是一组 hamcrest给定模块的匹配器类。

  • 假设 1:
    作为hamcrest是测试代码的库,这些匹配器不应该进入 main源集。

  • 假设 2: 这些类也不应该进入 test源集,作为对 test 的依赖source 只是这些类可用的一种解决方法。人们通常不希望依赖于实际测试。 也是not recommended (由 Netflix)定义对 test 的依赖项目的源集。

解决方案 1:

main 中创建包含这些类的专用模块source-set 并在任何需要的地方简单地定义这个模块的测试依赖。

这是我使用了很长一段时间的方法,但我不太喜欢它。

  • 首先,除了附加 testSupport 之外,我从来没有想出一个好听的名字。到原始模块的名称,导致名称类似于 core-testSupport , persistence-testSupport等等。

  • 其次,它创建了大量模块,项目树被这些模块污染了。

解决方案 2:(我希望得到反馈的那个)

configurations {
    testSupportCompile.extendsFrom compile
    testSupportRuntime.extendsFrom runtime
}

sourceSets {
    testSupport {
        compileClasspath += sourceSets.main.output + configurations.testSupportCompile
        runtimeClasspath += compileClasspath + configurations.testSupportRuntime
    }
}

task testSupportJar(type: Jar) {
    from sourceSets.testSupport.output
    classifier 'testSupport'
}

artifacts {
    testSupportCompile testSupportJar
}

上面的 gradle 配置可以放在名为 testSupport.gradle 的文件中并应用于需要此专用源集的任何模块,以提供可在测试中重用的类。

定义一个依赖会像这样工作:

testCompile project(path: ':core', configuration: 'testSupportCompile')

我对 gradle 还是个新手,研究了很多,但我仍然有几个问题。

  1. 我知道声明一个新的源集会自动创建两个配置:<sourceSet>Compile<sourceSet>Runtime .我不太喜欢这种方法的是,在声明依赖项时必须使用 testSupportCompile 配置。有没有办法将其别名为 testSupport还是类似的东西?

  2. 我的项目目前可以正常编译。但是,我不确定我是否以正确的方式做事。如何改进此配置?

  3. 还有其他方法可以实现所需的功能吗?在研究过程中,我并没有真正找到关于这个主题的太多信息,这让我觉得我要么使用了错误的搜索词,要么做了一些根本不应该做的愚蠢事情。

我知道这是一个宽泛的问题,但我不确定除了此处之外,在哪里可以获得关于此类问题的适当反馈。

最佳答案

我有类似的情况,我已经推迟了一段时间的解决方案,使用了各种 hack 和变通方法。您的问题是调查它的最终动机。

这就是我最终得到的 -- EDIT 与 Thomas 合作完成的:

configurations {
    // create a new configuration and inherit everything from compile
    testlib.extendsFrom compile
}

sourceSets {
    testlib {
        // We will at least need access to our main sourceSet and all dependencies that are declared for our configuration.
        compileClasspath += sourceSets.main.output + configurations.testlib
    }
}

task testlibJar(type: Jar) {
    from sourceSets.testlib.output
    classifier 'testlib'
}

artifacts {
    testlib testlibJar    // include the classes into the new configuration
    archives testlibJar    // optional: include the support JAR into "uploadArchives", so it may also be used in other projects
}

然后,在依赖模块中,只需使用:

dependencies {
    testCompile project(path: ':otherproject', configuration: 'testlib')
}

请注意,(空的)teSTLibCompileteSTLibRuntime 配置仍会创建(作为引入新的 teSTLib 源集的结果),但我相信忽略它们是安全的。

另外,经常会遇到项目自己的test配置需要使用teSTLib(项目测试依赖通用测试支持)。在这种情况下,您可以在同一项目的两个配置之间添加依赖关系:

testCompile project(path: ':myproject', configuration: 'testlib')

或者单独增强编译和运行时类路径:

configurations {
    testlib.extendsFrom compile
    testCompile.extendsFrom testlib
}
sourceSets {
    test {
        compileClasspath += sourceSets.testlib.output
        runtimeClasspath += sourceSets.testlib.output
    }
}

关于testing - 反馈 `test-support`代码的gradle配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40600392/

相关文章:

testing - Web 测试记录器不允许我记录测试 IE11 windows 7

android - 我该如何解决这个问题?我在用gradle构建android项目时收到此错误

java - 如何在java中将源代码附加为依赖项

gradle - 子项目与Gradle的依赖项失败

java - 有没有人使用 SIKULI 来测试他们基于 GUI 的应用程序?

node.js - 如何确保测试对象属性的更改不会传播到其他测试用例?

spring - Spring Cloud 合约gradle每次都运行测试

Android studio gradle脚本找不到Pattern类

gradle - gradle 构建脚本应该如何模块化此应用程序?

c# - 如何使用 C# 制作几个不同的列表?