2017-09-20 28 views
1

我想創建一個gradle腳本來爲我的.net項目運行xunit測試用例。該腳本看起來像:xunit測試的Gradle腳本什麼都不做或拋出execCommand == null

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。

回答

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" 
} 

無輸出重定向現在,首要的事情。

讓我知道它是否對你有幫助。

+0

我試過了你建議的第一個解決方案,但它不起作用。我收到的錯誤是「必須指定至少一個程序集」,並且例外情況是command cmd以非零的退出值3結束。您能幫助我嗎? – jugal

+0

那裏的消息:_必須指定至少一個assembly_完全來自?我想這不是來自'執行'任務。我可能會盡力幫助你,但無法測試我的建議。 – Opal

+0

@jugal這條消息來自這裏:https://github.com/xunit/xunit/blob/master/src/xunit.console/CommandLine.cs#L134,有一些配置丟失.. – Opal