xunit test的Gradle脚本不执行任何操作或抛出execCommand == null

时间:2017-09-20 10:14:45

标签: .net gradle cmd xunit

我正在尝试创建一个gradle脚本来为我的.net项目运行xunit测试用例。该脚本如下所示:

task xunitTests {
   String contents = ""
   FileTree tree = fileTree(dir: 'Unit Test', 
   includes: ['**/bin/Debug/**/[Project]*UnitTest.dll'], exclude:['**/bin/Debug/**/*[Project].dll'])
   def reportDir = new File("${buildDir}",'report/xUnit')
   tree.each { path ->
      if (!executedDll.contains(path.name)) {
         def stdout = new ByteArrayOutputStream()
         exec {
            commandLine 'cmd', '/c', 'packages\\xunit.runner.console.2.2.0\\tools\\xunit.console.exe \"$path\" -xml xunit.xml'
            standardOutput = stdout
        }
        executedDll.add(path.name)
        println "Output:\n$stdout"
    } else {
       println "Excluded already executed dll $path.name"
    }
  }
}

运行xunitTests任务后,我得到的输出是任务是最新的,构建成功但我没有在控制台上看到任何执行。现在,当我执行以下代码时:

task xunitTests (type:Exec) {
   String contents = ""
   FileTree tree = fileTree(dir: 'Unit Test', 
includes: ['**/bin/Debug/**/[Project]*UnitTest.dll'], exclude:['**/bin/Debug/**/*[Project].dll'])
   def reportDir = new File("${buildDir}",'report/xUnit')
   tree.each { path ->
      if (!executedDll.contains(path.name)) {
        def stdout = new ByteArrayOutputStream()
        commandLine 'cmd', '/c', 'D:\\LIS\\LIS.Encompass\\packages\\xunit.runner.console.2.2.0\\tools\\xunit.console.exe \"$path\" -xml xunit.xml'
        standardOutput = stdout
        executedDll.add(path.name)
        println "Output:\n$stdout"
    } else {
        println "Excluded already executed dll $path.name"
    }
}

我收到错误" execCommand == null!"。我在这里错过了什么?我只需要执行测试dll列表来获取输出xml。

1 个答案:

答案 0 :(得分:0)

不幸的是,这完全不是它的工作方式 - read Exec类型的任务是如何工作的。可以在配置时配置commandLine一次,这里有一些示例(我假设您可以将多个路径传递给xunit):

task xunitTests (type:Exec) {
  def paths = []
  def tree = fileTree(dir: 'Unit Test', includes: ['**/bin/Debug/**/[Project]*UnitTest.dll'], exclude:['**/bin/Debug/**/*[Project].dll'])
  def paths = tree.files*.absolutePath
  commandLine 'cmd', '/c', "D:\\LIS\\LIS.Encompass\\packages\\xunit.runner.console.2.2.0\\tools\\xunit.console.exe ${paths.join(' ')} -xml xunit.xml"
}

你可以尝试一下,不幸的是我无法测试它。

如果可以将多个路径作为参数传递给xunit,则需要创建多个任务和一个根任务(这取决于所有子任务):

task testAll
def tree = fileTree(dir: 'Unit Test', includes: ['**/bin/Debug/**/[Project]*UnitTest.dll'], exclude:['**/bin/Debug/**/*[Project].dll'])
tree.eachWithIndex { f, idx ->
  task "test$idx"(type: Exec) {
    commandLine 'cmd', '/c', "packages\\xunit.runner.console.2.2.0\\tools\\xunit.console.exe ${f.absolutePath} -xml xunit.xml"
  } 
  testAll.dependsOn "test$idx"
}

现在没有输出重定向,首先是第一件事。

让我知道它是否对你有帮助。

相关问题