Gradle - 配置测试包括属性文件

时间:2017-07-17 11:34:25

标签: gradle testng build.gradle

我有一个使用Gradle的Java项目构建和一个包含我的测试框架的自定义配置的属性文件(要使用的线程数量,测试环境URL,这些环境的自定义用户名和密码等等)。 )。

我面临与使用该文件中的属性相关的问题,我无法弄清楚:

  • 如果我的测试任务include '**/*Test.class',所有测试都按预期运行。
  • 如果我的测试任务include '**/MyTest.class',只有该测试按预期运行。
  • 如果我的测试任务include readProperty(),则任务被跳过为NO-SOURCE。 < - 这是我无法理解的部分 - 因为readProperty返回正确的值。

让我们详细了解一下:

这是在my.property文件中定义属性的方式:

testng.class.includes='**/MyTest.class'

这就是build.gradle文件的样子:

Properties props = new Properties()
props.load(new FileInputStream(projectDir.toString() + '/common.properties'))

def testsToRunWorking(p) {
  String t = 'MyTest.class'
  println "Tests = $t"
  return t ? t : '**/*Test.class'
}

def testsToRunNotWorking(p) {
  String t = getProperty(p, "testng.class.includes")
  println "Tests = $t"
  return t ? t : '**/*Test.class'
}

task testCustom(type: Test) {
  outputs.upToDateWhen { false }
  testLogging.showStandardStreams = true

  classpath = configurations.customTest + sourceSets.customTest.output
  include testsToRunNotWorking(props) ///< Does not work!
  // include testsToRunWorking(props) ///< Works!

  useTestNG()
}

在调试方面:

  • println正确返回我期望的值,即使testCustom任务没有达到我期望的效果。
  • 我尝试添加dependsOn任务,以打印testCustom.configure { println $includes }的内容,看起来也是正确的。
  • --info

    Tests = '**/MyTest.class'
    :clean
    :compileCustomTestJava - is not incremental (e.g. outputs have changed, no previous execution, etc.).
    Note: Some input files use or override a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: Some input files use unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    :processCustomTestResources
    :customTestClasses
    :testCustom NO-SOURCE
    

问题的核心似乎来自于我从财产中读取这一价值这一事实。我在build.gradle里面硬编码,一切按预期工作。如果从属性文件中读取 - 使用NO-SOURCE语句停止构建。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您在属性文件的值中使用引号。分配在属性文件中的所有内容都将用作值,因此引号保留在字符串中。它们打印在输出Tests = '**/MyTest.class'中。另一方面,如果在(Groovy)代码中使用引号定义字符串,则它们不包含在字符串中。因此,传递的字符串不一样。

从属性文件中删除引号,一切都应该有效,因为类文件将匹配不带引号的字符串。

相关问题