Gradle:如何为每个子项目运行特定的(体系结构)测试?

奥托

我有一个Gradle整体项目,大约有50个子项目。我想确保每个子项目中的代码都符合特定规则,例如,每个名为的类*Foo都用注释Bar

我已经编写了一个测试,可以成功扫描类路径并断言我感兴趣的属性。

我在哪里放置此代码,以便对整个项目执行该代码,或者对每个子项目分别执行,或者对整个代码库一次执行?

我了解测试装置,因此可以轻松地在所有子项目都可以访问的类中提供逻辑。但是,我仍然需要编写一个运行共享代码的测试类,这对于每个子项目都必须执行。

如何避免这种重复?我正在寻找这样的东西:

subprojects {
    additionalTestsFrom project(':architecture-tests')
}
奥托

编辑:使用Conventional插件的干净方法,请参见下面的旧(不完善的)方法。

主要思想:https : //docs.gradle.org/current/userguide/sharing_build_logic_between_subprojects.html#sec : convention_plugins

buildSrc/build.gradle

plugins {
    id 'groovy-gradle-plugin'
}

创建一个插件,定义我们进行任何测试所需的依赖项。buildSrc/src/main/groovy/playground.tests.gradle

plugins {
    id 'java'
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:latest.release'
    testImplementation 'org.assertj:assertj-core:latest.release'
    testImplementation 'nl.jqno.equalsverifier:equalsverifier:latest.release'

    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:latest.release'
}

tasks.withType(Test) {
    useJUnitPlatform()
}

利用此插件,并公开添加到中的测试类:architecture-testsarchitecture-tests/build.gradle

plugins {
    id 'playground.tests' // so that we have access to JUnit, AssertJ, ...
}

dependencies {
    // add whatever you need to write your architecture tests
}

// provide configurations that can be consumed by the other subprojects
configurations {
    exposedTestClasses {
        canBeConsumed = true
        canBeResolved = false
    }
    exposedTestRuntimeClasspath {
        canBeConsumed = true
        canBeResolved = false
    }
}

// wire the required data to the exposed configurations
artifacts {
    exposedTestClasses(sourceSets.test.output.classesDirs.files) {
        // make sure the compilation task is run first
        builtBy(compileTestJava)
    }
    exposedTestRuntimeClasspath sourceSets.test.runtimeClasspath.files
}

在中提供测试类architecture-tests/src/test/java/

现在定义一个可用于导入体系结构测试的插件。buildSrc/src/main/groovy/playground.architecture-tests.gradle

plugins {
    id 'playground.tests' // inherit the basic test dependencies
}

// define the configurations that connect the task (defined below) to the exposed configurations of `:architecture-tests`
configurations {
    sharedTestClasses {
        canBeConsumed = false
        canBeResolved = true
    }
    sharedTestRuntimeClasspath {
        canBeConsumed = false
        canBeResolved = true
        extendsFrom testRuntimeClasspath
    }
}

dependencies {
    sharedTestClasses project(path: ':architecture-tests', configuration: 'exposedTestClasses')
    sharedTestRuntimeClasspath project(path: ':architecture-tests', configuration: 'exposedTestRuntimeClasspath')
}

task sharedTest(type: Test) {
    description = 'Runs the tests shared by all subprojects.'
    group = 'verification'

    // use the configurations defined above, which depends on the configurations in `:architecture-tests`
    testClassesDirs = configurations.sharedTestClasses
    classpath = configurations.sharedTestRuntimeClasspath

    // optional - extend the classpath with the subproject's test and integration test classpath, so that I'm able to access these tests from my shared test
    classpath += sourceSets.test.runtimeClasspath
    classpath += sourceSets.integrationTest.runtimeClasspath
}
check.dependsOn sharedTest

要将架构测试作为子项目的一部分运行,请subproject/build.gradle添加:

plugins {
    id 'playground.architecture-tests'
}

编辑:较老,灰心,解决方案:

我想我知道了。我很高兴收到改进意见和建议!

我有一个子项目:architecture-tests,其中包含我想包含在其他所有子项目中的测试。为此,我需要一个专用的子项目,因为我正在更改常规子项目的subproject-xxx/build.gradle文件(子项目配置时间?)和subprojects {}build.gradle文件(项目配置时间?)的配置。到目前为止,这还不是问题,但是在任何公开/消耗性配置被另一个子项目解决之后,我无法重新配置子项目。这样,我需要:architecture-tests从该subprojects块和配置中排除该子项目,并将必要的配置复制到中architecture-tests/build.gradle如果我不这样做,则会得到:

解决后,无法更改依赖项配置':architecture-tests:testSharedRuntimeClasspath'的角色。”

这是我的解决方案:

将测试放在中architecture-tests/src/testShared/java/

architecture-tests/build.gradle

plugins {
    id 'java-library'
}

// I don't think it is necessary to have a special source set, though
sourceSets {
    testShared {
    }
}

// provide configurations that can be consumed by the other subprojects
configurations {
    exposedTestSharedClasses {
        canBeConsumed = true
        canBeResolved = false
    }
    exposedTestSharedRuntimeClasspath {
        canBeConsumed = true
        canBeResolved = false
    }
}

// just add some regular dependences for my shared tests
dependencies {
    // I use a platform which defines the versions/constraints. You can just add the versions below without using a platform.
    testSharedImplementation platform(project(':some-platform'))
    testSharedImplementation 'org.testng:testng'
    testSharedImplementation 'org.assertj:assertj-core'
    testSharedImplementation 'io.github.classgraph:classgraph'
}

// wire the required data to the exposed configurations
artifacts {
    // give access to the compiled (test) classes
    exposedTestSharedClasses(sourceSets.testShared.output.classesDirs.files) {
        // make sure the compilation task is run first
        builtBy(compileTestSharedJava)
    }
    // give access to the runtime classpath, containing the libraries defined above
    exposedTestSharedRuntimeClasspath sourceSets.testShared.runtimeClasspath.files
}

对于每个消耗(其他)子项目(我build.gradle在一个subprojects{}的根目录中执行此操作

// define the configurations that connect the task (defined below) to the exposed configurations of `:architecture-tests`
configurations {
    testSharedClasses {
        canBeConsumed = false
        canBeResolved = true
    }
    testSharedRuntimeClasspath {
        canBeConsumed = false
        canBeResolved = true
        extendsFrom testRuntimeClasspath
    }
}

dependencies {
    testSharedClasses project(path: ':architecture-tests', configuration: 'exposedTestSharedClasses')
    testSharedRuntimeClasspath project(path: ':architecture-tests', configuration: 'exposedTestSharedRuntimeClasspath')
}

task testShared(type: Test) {
    useTestNG()

    description = 'Runs the tests shared by all subprojects.'
    group = 'verification'

    // use the configurations defined above, which depends on the configurations in `:architecture-tests`
    testClassesDirs = configurations.testSharedClasses
    classpath = configurations.testSharedRuntimeClasspath

    // optional - extend the classpath with the subproject's test and integration test classpath, so that I'm able to access these tests from my shared test
    classpath += sourceSets.test.runtimeClasspath
    classpath += sourceSets.integrationTest.runtimeClasspath
}
// run the shared tests as part of the regular `build` (which depends on `check`)
check.dependsOn testShared

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

执行单元测试时,Gradle会构建每个子项目

来自分类Dev

如何使用Gradle向每个子项目注入Android配置?

来自分类Dev

如何为不同的体系结构构建Visual Studio项目

来自分类Dev

如何为不同的体系结构构建Visual Studio项目

来自分类Dev

如何配置一个子项目取决于主项目的Gradle项目?

来自分类Dev

在多项目设置中,Gradle会评估每个子项目。我可以忽略一些子项目吗?

来自分类Dev

如何在另一个子项目的 gradle 任务中从一个子项目调用代码?

来自分类Dev

在gradle根项目上运行测试任务不会在子项目上运行测试任务

来自分类Dev

gradle kotlin dsl-如何为子项目配置依赖项

来自分类Dev

如何配置一个子项目依赖于主项目的Gradle项目?

来自分类Dev

测试时如何顺序运行子项目测试(包括设置方法)

来自分类Dev

仅在特定子项目上运行Gradle ShadowJar插件

来自分类Dev

从子项目运行Gradle根项目任务

来自分类Dev

Jenkins无法识别gradle子项目的测试

来自分类Dev

gradle 排除子项目的运行任务

来自分类Dev

如何使用drifferent gradle多项目的子项目

来自分类Dev

如何为我的所有 gradle 项目(不仅仅是子项目)共享构建代码脚本

来自分类Dev

Gradle 禁用特定任务的自动子项目执行

来自分类Dev

如何在具有多个子项目的SBT项目中覆盖子项目中的设置

来自分类Dev

Gradle-构建子项目子项目

来自分类Dev

如何指定 Gradle 子项目之间的配置顺序?

来自分类Dev

在所有子项目中的所有测试之前在子项目中运行 checkstyles

来自分类Dev

如何在Android上运行离子项目?

来自分类Dev

如何在Gradle中将其他子项目中选择的类用作测试依赖项

来自分类Dev

如何在Gradle中将其他子项目中选择的类用作测试依赖项

来自分类Dev

Gradle-递归子项目

来自分类Dev

Gradle:复制子项目资源

来自分类Dev

在子项目上执行gradle任务

来自分类Dev

Gradle获取子项目的版本

Related 相关文章

  1. 1

    执行单元测试时,Gradle会构建每个子项目

  2. 2

    如何使用Gradle向每个子项目注入Android配置?

  3. 3

    如何为不同的体系结构构建Visual Studio项目

  4. 4

    如何为不同的体系结构构建Visual Studio项目

  5. 5

    如何配置一个子项目取决于主项目的Gradle项目?

  6. 6

    在多项目设置中,Gradle会评估每个子项目。我可以忽略一些子项目吗?

  7. 7

    如何在另一个子项目的 gradle 任务中从一个子项目调用代码?

  8. 8

    在gradle根项目上运行测试任务不会在子项目上运行测试任务

  9. 9

    gradle kotlin dsl-如何为子项目配置依赖项

  10. 10

    如何配置一个子项目依赖于主项目的Gradle项目?

  11. 11

    测试时如何顺序运行子项目测试(包括设置方法)

  12. 12

    仅在特定子项目上运行Gradle ShadowJar插件

  13. 13

    从子项目运行Gradle根项目任务

  14. 14

    Jenkins无法识别gradle子项目的测试

  15. 15

    gradle 排除子项目的运行任务

  16. 16

    如何使用drifferent gradle多项目的子项目

  17. 17

    如何为我的所有 gradle 项目(不仅仅是子项目)共享构建代码脚本

  18. 18

    Gradle 禁用特定任务的自动子项目执行

  19. 19

    如何在具有多个子项目的SBT项目中覆盖子项目中的设置

  20. 20

    Gradle-构建子项目子项目

  21. 21

    如何指定 Gradle 子项目之间的配置顺序?

  22. 22

    在所有子项目中的所有测试之前在子项目中运行 checkstyles

  23. 23

    如何在Android上运行离子项目?

  24. 24

    如何在Gradle中将其他子项目中选择的类用作测试依赖项

  25. 25

    如何在Gradle中将其他子项目中选择的类用作测试依赖项

  26. 26

    Gradle-递归子项目

  27. 27

    Gradle:复制子项目资源

  28. 28

    在子项目上执行gradle任务

  29. 29

    Gradle获取子项目的版本

热门标签

归档