gpt4 book ai didi

gradle - 仅当此给定测试在 gradle 测试阶段激活时,如何运行特定的 gradle 任务?

转载 作者:行者123 更新时间:2023-12-03 03:17:21 25 4
gpt4 key购买 nike

我想确保只有在启用特定测试的情况下才会在测试之前运行特定的 gradle 任务。

例如,假设我有一个名为 TranslationTests 的测试,它看起来像这样:

@EnabledIfSystemProperty(named = "org.shabunc.tests.TranslationTests", matches = "true")
class TranslationTests {
...
}

通过以下方式激活:
test {
if (project.hasProperty(projectProperty)) {
systemProperty(projectProperty, "org.shabunc.tests.TranslationTests")
}
}

现在我想确定每次我运行时:
gradle test -Porg.shabunc.tests.TranslationTests

在测试一些特定的 gradle 任务之前,比如说 gradle prepareTranslationSetup被触发。严格来说,我希望每次我知道 TranslationTests 正在运行时触发此任务 - 否则不要触发。

最佳答案

您可以使用 onlyIf()任务 prepareTranslationSetup并制作 test依赖它。 onlyIf()定义如下:

You can use the onlyIf() method to attach a predicate to a task. The task’s actions are only executed if the predicate evaluates to true.



(来自: Authoring Tasks)

例子

假设您有以下任务:
task doBeforeTest {
onlyIf {
project.hasProperty("runit")
}

doLast {
println "doBeforeTest()"
}
}

task runTest {
dependsOn = [ doBeforeTest ]

doLast {
println "runTest()"
}
}
doBeforeTest的操作仅在项目具有指定属性时才会执行。 runTest配置为依赖 doBeforeTest .现在,当你这样做时
gradlew runTest --info

输出类似于
> Task :doBeforeTest SKIPPED
Skipping task ':doBeforeTest' as task onlyIf is false.

> Task :runTest
runTest()

如您所见 doBeforeTest由于未满足前提条件而被跳过。另一方面,运行
gradlew runTest -Prunit

执行 doBeforeTest正如预期的那样
> Task :doBeforeTest
doBeforeTest()

> Task :runTest
runTest()

关于gradle - 仅当此给定测试在 gradle 测试阶段激活时,如何运行特定的 gradle 任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59214601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com