Tuesday, 13 May 2008

Decoding the MS Test TRX file

I got fed up with loading a command line MS Test output data (.trx file) into visual studio in order to see the the failed tests and their messages so with the help of a colleague we came up with a simple XSL transformation that turns the output of the MS test session into a piece of text or html. We use the text version to print out a summary, and the html version to show the errors (if any :-)

In order to incorporate it into the command line desktop build I used nxslt2 from xmllab.net. Which takes the trx file, the xsl stylesheet and the output on its command line e.g.

nxslt2.exe  testrun.trx trxformatter.xsl -o testrun.results.htm




The summary stylesheet just outputs how many test found, passed and failed, and the name of the trx file if any did fail (so you can interrogate easily in vs if you want).







   1: <xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >


   2:     <xsl:output method="text" />


   3:     <xsl:template match="/">


   4:         Test Results Summary : Codebase: <xsl:value-of select="Tests/TestRun/tests/value/codeBase"/>


   5:         <xsl:if test="(Tests/TestRun/result/executedTestCount - Tests/TestRun/result/passedTestCount) > 0">


   6:             Summary : Failed <xsl:value-of select="Tests/TestRun/result/executedTestCount - Tests/TestRun/result/passedTestCount"/> (Executed [<xsl:value-of select="Tests/TestRun/result/executedTestCount"/>], Found [<xsl:value-of select="Tests/TestRun/result/totalTestCount"/>], Passed [<xsl:value-of select="Tests/TestRun/result/passedTestCount"/>])


   7:             Results : <xsl:value-of select="Tests/TestRun/runConfig/runDeploymentRoot"/>.trx


   8:         </xsl:if> <xsl:if test="(Tests/TestRun/result/executedTestCount - Tests/TestRun/result/passedTestCount) = 0">


   9:             Summary : Passed (Executed [<xsl:value-of select="Tests/TestRun/result/executedTestCount"/>], Found [<xsl:value-of select="Tests/TestRun/result/totalTestCount"/>], Passed [<xsl:value-of select="Tests/TestRun/result/passedTestCount"/>])


  10:         </xsl:if>


  11:  


  12:     </xsl:template>


  13: </xsl:stylesheet>







and outputs a summary similar to :



























 



The error details stylesheet :



<?xml version="1.0" ?>
<
xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<
xsl:template match="/">
<
html>
<
head>
<
style type="text/css">
h2 {color: sienna}
p {margin-left: 20px}
.resultsHdrRow { font-face: arial; padding: 5px }
.resultsRow { font-face: arial; padding: 5px }
</style>
</
head>
<
body>
<
h2>Test Results</h2>
<
xsl:for-each select="Tests/TestRun/result">
<
h3>Summary</h3>
<
ul>
<
li>
Tests found: <xsl:value-of select="totalTestCount"/>
</
li>
<
li>
Tests executed: <xsl:value-of select="executedTestCount"/>
</
li>
<
li>
Tests passed: <xsl:value-of select="passedTestCount"/>
</
li>
</
ul>
</
xsl:for-each>
<
table border="1" width="80%" >
<
tr class="resultsHdrRow">
<
th align="left">Test</th>
<
th align="left">Outcome</th>
</
tr>
<
xsl:for-each select="Tests/UnitTestResult">
<
tr valign="top" class="resultsRow">
<
td width='30%'>
<
xsl:value-of select="testName"/>
</
td>
<
td width='70%'>
<
Div>
Message: <xsl:value-of select="errorInfo/message"/>
</
Div>
<
br/>
<
Div>
Stack: <xsl:value-of select="errorInfo/stackTrace"/>
</
Div>
</
td>
</
tr>
</
xsl:for-each>
</
table>
</
body>
</
html>
</
xsl:template>

</
xsl:stylesheet>



creates the following HTML output



image



Note that the test name will of course be yours, and any messages and stack trace will appear in the



I'm aware that you can optimise the xsl but it works for me as is.