Visual studio Team System 2008 can give you all kinds of reports for different kinds of tests you ran. This is possible only when you have a TFS - Team Foundation Server installed and configured. What if you do not have a TFS set up? In that case, you have to have VSTS in order to be able to open the test result files. VSTS uses a propritory format for results and it has extension .trx. If you look inside any .trx, you will find it to be an XML file. If you have VSTS on your computer, you can easily open this .trx file and do all sorts of filtering to analyze the results. But not all on your team would have VSTS installed, most of them would be your bosses who would be keep to see these results.
There are several ways to solve this -
1. Using XSLT, which I am going to explain in this article
2. Using ASP.NET,
3. Using C#
I will talk about last 2 methods in other articles.
To keep this article close to the subject, I am skipping explaination of what is XSLT and how to use it? You can find detailed help and instructions at http://www.w3schools.com/xsl (I used this site to learn it myself, and found extremely helpful). I am also skipping what is XML, you can always study it at http://www.w3schools.com/xml
Lets take a look inside a .trx file generated after a sample test run of 2 units tests and 2 manual tests.
I know, it is a lot of data, just after execution of 4 test cases. Just imagine going through this file after execution of hundreds if not thousands of test cases.
But we do not need all the data given in trx file to generate our simple report which can at least show us what happened, how many tests pass, how many failed, which passed, which failed and a summary of the overall run. We will need to work with 2 main nodes of the file.
1. ResultSummary (found near the top of the file) and
2. Results (found near the end of the file)
Other nodes can be used based on your reporting requirements.
Here is how the XSLT is layed out
Lets understand what I am doing here,
I am creating an HTML page which will have a Header as "VSTS Test Results", then a sub-heading as "Summary". Then I am laying out the summary table which will have 4 columns - Tests, Executed, Failed and Time(s). Now, the real deal, getting values from XML and transforming them into using this XSL. As I know that all my summary values are going to come from node "Counters", I am using XPATH expression to read the corresponding values of attributes and filling them out in the summary table. Example, <xsl:value-of select="//Counters/@total"/> statement is going to jump to the "Counters node" by //Counters, then it is going to look for an attirbute called "total" and xsl:value-of command is going to read the corresponding value of that attribute. In the same way, I read all the required values. My summary table is done.
Next, I create a test result details table with columns - Test Name, Description and Result. Now, the problem here is, name and description of the test case is under "TestDefinitions" node and result is under "Results" node. Much like, two tables in a database. If I jump to "ManualTest" I will get name and description for each test case easily and I can populate the whole table using a loop. But, how am I going to fetch the result??
I start easy, I put a loop around fetching statements of name and description of each test case. When it comes to fill the third column of result, I should find out the corresponding test case in the "Results" node and then fetch the "outcome" attribute. That is exactly what - <xsl:value-of select='//ManualTestResult[@testId=current()/@id]/@outcome'/> is doing. My loop is using node //ManualTest to get the name and description, so to get the corresponding result from //ManualTest Result, I am comparing/matching the "testid" attributes (much like a unique key between these 2 tables). When the testid from //ManualTest matches testid of //ManualTestResult, I am fetching the "outcome" attribute, which is nothing but the result of that test case.
The beauty of this is, I do not have to write 2 cascading loops to go through 2 nodes and their child node and attributes to find the correct result value of that test case, XSL takes care of the inner loop for me (this is more like jumping through nodes and attributes than looping through, so it is also much faster).
To give some final touches, I use a CSS stylesheet to make my results look good!
How to I use it?
Once I have my trx generated after a test pass, I open it in notepad and insert below line just below the XML declare. I am calling above shown XSL transform file "XSLT.xslt"
This statement tell the browser that, when you are opening the xml, look for that particular XSL transform file and apply it before rendering the output. Also, I remove any attributes from the first node of the trx file, it should look just as <TestRun>. If you keep the attributes from <TestRun>, your browser does not treat is as XML format.
Here is how the above TRX will look usning above method of XSL transformation -
( 1 Vote )