2017-08-29 49 views
-1

我們目前正在使用FitNesse進行自動化測試。我正在使用VSTS將其集成到我們的構建中,並需要發佈測試結果。將FitNesse XML測試結果轉換爲JUnit格式

FitNesse測試結果保存在xml中,需要轉換爲VSTS可以理解的JUnit格式。理想情況下,我想使用一個PowerShell腳本,這將使轉換爲JUnit成爲可能。

有沒有人以前做過或有一個示例腳本轉換爲JUnit格式。

非常感謝

+2

你嘗試過什麼? XSLT是一個選項嗎? – MartinByers

+0

我有一個XSLT文件,我很遺憾無法共享......棘手的部分是在'FitNesseRoot/files/testResults'內的測試套件中打開每個單獨測試的結果(使用XSLT'document'函數) ,然後提取您想要查看的詳細程度。 – legoscia

+0

感謝@legoscia,這聽起來像是一個不錯的選擇。您是否使用XSLT函數轉換爲JUnit?我的目標是希望將它變成PowerShell腳本。這是你做的事嗎?或者有一個例子可以幫助我開始嗎? – Sej

回答

0

感謝您的幫助球員。我們最終創建了自己的PowerShell腳本,以獲取類名,套件和失敗類型。如果有人願意使用,請在下面發佈。

param([string]$outpath, [string]$testserver, [string]$suitename) 
 

 
$uri = "http://" + $testserver + "/" + $suitename + "?suite&format=xml" 
 
$resultString = Invoke-WebRequest -Uri $uri 
 
$xml = New-Object xml 
 
$xml.LoadXml($resultString) 
 

 
$tests = @{} 
 

 
foreach($result in $xml.testResults.result) 
 
{ 
 
    $pageName = $result.pageHistoryLink.Substring(0, $result.pageHistoryLink.IndexOf("?")) 
 
    foreach ($ir in $result.instructions.instructionResult) 
 
    { 
 
     $i =$ir.instruction.Replace("=",":") 
 
     $argsloc = $i.LastIndexOf(', args:') 
 
     if ($argsloc -gt 0) 
 
     { 
 
      $i=$i.Substring(0, $argsloc) + "}" 
 
     } 
 

 
     $instr =convertFrom-Json $i 
 
     if ($instr.instruction -eq "import") 
 
     { 
 
      continue 
 

 
     } 
 

 
     if ($instr.instruction -eq "make") 
 
     { 
 
      $t=New-Object psobject 
 
      $classname = $pagename + "." + $instr.className 
 
      Add-Member -InputObject $t -MemberType NoteProperty -Name className -Value $classname 
 
      Add-Member -InputObject $t -MemberType NoteProperty -Name scenarios -Value @{} 
 
      $tests.add($pagename + "." + $instr.instanceName, $t)  
 
     } 
 

 
     if ($instr.instruction -eq "call") 
 
     { 
 
     
 
      $t=$tests[$pagename + "." + $instr.instanceName] 
 
      if (-not($t.scenarios.ContainsKey($ir.expectation.row))) 
 
      { 
 
      $s=New-Object psobject 
 
      Add-Member -InputObject $s -MemberType NoteProperty -Name pass -Value $true 
 
      Add-Member -InputObject $s -MemberType NoteProperty -Name failures -Value @() 
 
      $t.scenarios.add($ir.expectation.row, $s)  
 
      } 
 

 
      if ($ir.expectation.status -eq $null) 
 
      { 
 
      continue 
 
      } 
 

 
      if ($ir.expectation.status -eq "pass") 
 
      { 
 
      continue 
 
      } 
 

 
      $LASTEXITCODE = 1 
 
      $scenario = $t.scenarios[$ir.expectation.row] 
 
      $scenario.pass =$false 
 
      $scenario.failures += ("Method Name: "+ $instr.methodName + " - Expected: " + $ir.expectation.expected + ", Actual: " + $ir.expectation.actual) 
 

 
     } 
 
    } 
 
} 
 

 
$doc = New-Object System.Xml.XmlDocument 
 
    
 
$suites = $doc.CreateElement("testsuites") 
 
$res=$doc.AppendChild($suites) 
 

 
$ts = $doc.CreateElement("testsuite") 
 
$res=$suites.AppendChild($ts) 
 

 
foreach ($testkey in $tests.Keys) 
 
{ 
 
    $test = $tests[$testkey] 
 
    $test 
 
    foreach ($scenariokey in $test.scenarios.Keys) 
 
    { 
 
     $scenario =$test.scenarios[$scenariokey] 
 
     $tc = $doc.CreateElement("testcase") 
 

 
     $attr=$doc.CreateAttribute("classname") 
 
     $res=$attr.Value= $test.className 
 
     $res=$tc.Attributes.Append($attr) 
 
     
 
     $attr=$doc.CreateAttribute("name") 
 
     $res=$attr.Value= $test.className 
 
     $res=$tc.Attributes.Append($attr) 
 

 
     if (-not($scenario.pass)) 
 
     { 
 
      $errString = [string]::Join(": ", $scenario.failures) 
 
      $scenario.failures.GetType() 
 
      $fail = $doc.CreateElement("failure") 
 
      $attr=$doc.CreateAttribute("message") 
 
      $res=$attr.Value= $errString 
 
      $res=$fail.Attributes.Append($attr) 
 
      #$fail.innertext = $errString 
 
      $res=$tc.AppendChild($fail) 
 
     } 
 

 
     $res=$ts.AppendChild($tc) 
 
    } 
 
} 
 

 
$doc.Save($outpath)