Sunday, 17 January 2016

Convert Nunit Test Result XML to HTML


Here is the XSLT code to convert Nunit test result XML file to a HTML report:


style.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="test-results">
      <html>
         <head>
            <style>
               table,td{border:1px solid black;border-collapse:collapse; }
               th{border:1px solid black;border-collapse:collapse; color: #FFFFFF; background-color: #005387; }
               td{padding:5px; text-align:center; }
               table {width:700px; margin:0 auto;}
               h1,h2 { text-align: center; background-color: #BDBDBD; color: black; }
            </style>
         </head>
         <body>
            <span>
               <h1>AUTOMATION REPORT</h1>
            </span>
            <br></br>
            <table style="width: 400px; margin: 0px auto;"  >
               <tr style='font-weight:bold;'>
                  <th>Total Test Cases</th>
                  <th>Pass </th>
                  <th>Fail   </th>
                  <th>Success Rate </th>
               </tr>
               <tr style='font-weight:bold;'>
                  <td>
                     <xsl:value-of select="@total" />
                  </td>
                  <td>
                     <xsl:value-of select="@total - @errors - @failures" />
                  </td>
                  <td>
                     <xsl:value-of select="@errors + @failures" />
                  </td>
                  <td>
                     <xsl:value-of select='format-number(round(@total - @errors - @failures) div @total * 100,"##0.00" )' />
                  </td>
               </tr>
            </table>
            <br></br>
            <span>
               <h2>Test Run Summary</h2>
            </span>
            <table style="margin: 0px auto;">
               <tr>
                  <th>Test Class</th>
                  <th>Test cases</th>
                  <th>Passed</th>
                  <th>Failed</th>
               </tr>
               <xsl:for-each select="test-suite/results/test-suite/results/test-suite/results/test-suite/results/test-suite">
                  <tr style='font-weight:bold;'>
                     <td style = ' background-color: #F5EFFB;'>
                        <xsl:value-of select="@name" />
                     </td>
                     <td>
                        <xsl:value-of select="count(results/test-suite[@name])" />
                     </td>
                     <td >
                        <font color="#00df00">
                           <xsl:value-of select="count(results/test-suite[@result='Success'])" />
                        </font>
                     </td>
                     <td>
                        <font color="red">
                           <xsl:value-of select="count(results/test-suite[@result='Failure'])" />
                        </font>
                     </td>
                  </tr>
               </xsl:for-each>
            </table>
            <br></br><br></br><br></br>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Transformation command:

you should have msxsl.exe in y our system folder. copy it to your test results directory. Place your style.xslt too in the same directory. From the commnad directory, navigate to the folder in which you have placed all the files and run the below command.

msxsl.exe TestResult.xml style.xslt -o output.html

Output Html:

After the tranformation commands execued, you should have Output.html file in the same diretory. 
You should find the resultant html like the image shown below

Output.html


No comments:

Post a Comment