【问题标题】:Is it possible to configure system properties dynamically for a Gradle test task?是否可以为 Gradle 测试任务动态配置系统属性?
【发布时间】:2013-10-31 14:03:39
【问题描述】:

是否可以为 Gradle 测试任务动态配置系统属性?我还没有找到让它工作的方法。

我正在使用我自己的 Gradle 插件 GwtPluginapply() 方法如下所示:

/** 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()
       }
    }
}

我想从project.gwt.getServerWait() 获取integration.test.server.wait 系统属性的配置。我不知道如何做到这一点,我开始认为这是不可能的。

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

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

问题似乎是在定义任务时设置了系统属性,但我的扩展此时没有任何值。我不知道如何解决这个问题。

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

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

我还尝试将 systemProperty 行移动到 doFirst 块。在这种情况下,doFirst 块从我的扩展中获得了一个合理的值(我可以打印它),但分配显然为时已晚,无法影响测试运行器。

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

【问题讨论】:

    标签: unit-testing junit gradle


    【解决方案1】:

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

    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)
    ...
    

    这不是我所希望的优雅解决方案,但它确实有效,而且并不难遵循。

    【讨论】:

      【解决方案2】:

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

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

      在您的插件脚本中?

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

      【讨论】:

      • 我曾尝试使用 &lt;&lt; 而不是显式的 doLast 块,但没有成功。我只是重新测试以确认。 doLast 在套件完成运行后执行。
      • 为了澄清,我将doLast 块添加到我的插件脚本中integrationtest 任务的定义中。
      【解决方案3】:

      我做的是:

      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
      

      在多模块环境中,这可能会比较棘手。然后我使用来自test 任务的rootProject.ext.myProperty

      最好的问候, 达米安。

      【讨论】:

        【解决方案4】:

        不知道你用什么版本来做这个,但这对我来说似乎是最方便的方法:

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

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

        【讨论】:

        • 这个问题来自 2013 年,当时我使用的是 Gradle 2.5。这种结构似乎不适用于 2.5,但很高兴看到在 4.0 中发生了变化。
        猜你喜欢
        • 2016-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 2020-05-20
        • 2011-03-01
        相关资源
        最近更新 更多