是否可以为Gradle测试任务动态配置系统属性?

时间:2013-10-22 15:36:54

标签: unit-testing junit gradle

是否可以为Gradle测试任务动态配置系统属性?我还没有办法让它发挥作用。

我在自己的Gradle插件GwtPlugin内工作。 apply()方法如下所示:

/** Apply the plugin. */
void apply(Project project) {
   project.plugins.apply(JavaPlugin)
   project.plugins.apply(WarPlugin)

   project.extensions.create("gwt", GwtPluginExtension, project)
   project.extensions.create("testSuite", TestSuitePluginExtension, project)
   project.convention.plugins.gwt = new GwtPluginConvention(project)

   applyGwt(project)
   applyTestSuite(project)
}

applyTestSuite()方法中,我为我的测试套件创建了一个任务。 integrationtest任务的定义如下所示:

// Run integration tests, assumed to be found in a class suites/IntegrationTestSuite.
project.task("integrationtest", type: org.gradle.api.tasks.testing.Test, dependsOn: project.tasks.buildApplication) {
    workingDir = { project.testSuite.getWorkingDir() == null ? project.projectDir : project.testSuite.getWorkingDir() }
    scanForTestClasses = false
    scanForTestClasses = false
    enableAssertions = false
    outputs.upToDateWhen { false }

    include "suites/IntegrationTestSuite.class"
    systemProperty "integration.test.server.wait", project.gwt.getServerWait()

    beforeSuite { descriptor ->
       if (descriptor.className == "suites.IntegrationTestSuite") {
          project.convention.plugins.gwt.rebootDevmode()
       }
    }

    afterSuite { descriptor ->
       if (descriptor.className == "suites.IntegrationTestSuite") {
          project.convention.plugins.gwt.killDevmode()
       }
    }
}

我想从integration.test.server.wait获取project.gwt.getServerWait()系统属性的配置。我无法弄清楚如何使这项工作,我开始认为这是不可能的。

如果我对系统属性进行硬编码,一切都按预期工作:

systemProperty "integration.test.server.wait", 10

问题似乎是在定义任务时设置了系统属性,但是我的扩展在那时没有任何值。我无法弄清楚如何解决这个问题。

例如,我尝试将project.gwt.getServerWait()调用放在闭包中,但在这种情况下,系统属性设置为如下字符串:

com.me.gradle.GwtPlugin$_applyTestSuite_closure10_closure42@320de756

我还尝试将systemProperty行移至doFirst块。在这种情况下,doFirst块从我的扩展中获得一个合理的值(我可以打印它),但是分配对于影响测试运行者来说显然为时已晚。

我有什么方法可以做到这一点吗?如果没有,是否有另一种方法可以将动态配置传递给我的测试运行器?

4 个答案:

答案 0 :(得分:2)

我找到了一种方法来完成这项工作。诀窍是稍后在某个属性确定可用时在测试任务上设置系统属性。实现这一目标的最简单方法似乎是通过虚拟依赖:

project.task("integrationtestconfig") << {
   outputs.upToDateWhen { false }
   project.tasks.integrationtest.systemProperty("integration.test.server.wait", project.gwt.getServerWait())
}

project.task("integrationtest", type: org.gradle.api.tasks.testing.Test, 
             dependsOn: project.tasks.buildApplication, project.tasks.integrationtestconfig)
...

这不是我希望的优雅解决方案,但它确实有效,并且不太难以理解。

答案 1 :(得分:1)

我的工作是:

ext {
    // Sets a sensible default
    myProperty project.properties.myProperty
}
task preTest << {
    // Computes the property...
    project.ext.myProperty = ...
}
task myTest {
    ...
    doFirst {
        systemProperty 'myProperty', project.ext.myProperty
    }
}

我在gradle.properties文件中定义了合理的默认值:

myProperty = A sensible default value

在多模块环境中,它可能更棘手。然后我使用rootProject.ext.myProperty任务中的test

祝你好运, 达明。

答案 2 :(得分:1)

Dunno你用什么版本来做这件事,但这对我来说似乎是最方便的方式:

task doSomethingBeforeTest {
    doLast {
      // some stuff to do
        test {
            systemProperties['some.property'] = 'prop'
        }
    }
}

基本上,只需将test块放在您的任务中并设置属性即可。 (这与gradle 4.0一样 - 不确定以前的版本,但我想它会。)

答案 3 :(得分:0)

我不确定这是否有效,但你是否尝试过这样做

doLast {
    systemProperty "integration.test.server.wait", project.gwt.getServerWait()
}

在你的插件脚本中?

这可能与在gradle中解决问题时的阶段(配置,...)有关。