Trx file is visual studio unit test result file extension in form of XML file.
Introduction
Trx file is visual studio unit test result file extension in XML
format. Result of unit test is kept in TestResult folder in base directory. We can open these files in Visual Studio to see result.
Objective
To
understand parsing of trx file through C#.NET.
Programmatically
parse trx file
Microsoft
Visual Studio provides schema to read trx file. Schema is available in Visual
Studio installation folder. It is named vstst.xsd
and can be found under your Visual Studio installation directory. You can
easily run the xsd.exe tool on this schema and generate the C# classes to parse
the trx files.
Use command
line to generate C# class from schema
Run below
command in command prompt to generate C# class file:
"C:\Program
Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64\xsd.exe" "C:\Program
Files (x86)\Microsoft Visual Studio 9.0\Xml\Schemas\vstst.xsd" /c
Using Code
UI

Code File
We can use ASP.NET GridView to display result. Create an ASP.NET Web Application and include vstst.cs file (we got after running xsd command mentioned above).
In the code snippet below, we are accessing base directory and iterating through all the files
having trx extension. While iterating “TestRunType” is used which is defined inside vstst.cs file. Here, a
datatable is created at run time and retrieving all important information from UnitTestResultType. This datatable then used as a source
for gridview. You can refer attached document to see full code.
public DataTable GetTestResultData()
{
string fileName;
DataTable testResultTable = null;
try
{
//Construct DirectoryInfo for the folder path passed in as an argument
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
DirectoryInfo di = new DirectoryInfo(baseDirectory);
ResultTable resultTable = new ResultTable();
testResultTable = resultTable.CreateTestResultTable();
// For each .trx file in the given folder process it
foreach (FileInfo file in di.GetFiles("*.trx"))
{
fileName = file.Name;
//Deserialize TestRunType object from the trx file
StreamReader fileStreamReader = new StreamReader(file.FullName);
XmlSerializer xmlSer = new XmlSerializer(typeof(TestRunType));
TestRunType testRunType = (TestRunType)xmlSer.Deserialize(fileStreamReader);
//Navigate to UnitTestResultType object and update the sheet with test result
//information
foreach(object itob1 in testRunType.Items)
{
ResultsType resultsType = itob1 as ResultsType;
if (resultsType != null)
{
foreach (object itob2 in resultsType.Items)
{
UnitTestResultType unitTestResultType = itob2 as UnitTestResultType;
if (unitTestResultType != null)
{
DataRow row = testResultTable.NewRow();
row[Constant.PROCESSEDFILENAME] = fileName;
row[Constant.TESTID] = unitTestResultType.testId;
row[Constant.TESTNAME] = unitTestResultType.testName;
row[Constant.TESTOUTCOME] = unitTestResultType.outcome;
row[Constant.ERRORMESSAGE] = ((System.Xml.XmlNode[])(((OutputType)(((TestResultType)(unitTestResultType)).Items[0])).ErrorInfo.Message))[0].Value;
testResultTable.Rows.Add(row);
}
}
}
}
}
}
catch
(Exception ex)
{
}
return
testResultTable;
}
Design
GridView is used to bind all the columns for display. You can refer attached document
to see full code.
<asp:GridView ID="grdTestResult" runat="server" CssClass="text"
AutoGenerateColumns="false" CellPadding="5"
GridLines="Both" SelectedIndex="0">
<Columns>
<asp:BoundField DataField="ProcessedFileName" HeaderText="Processed File Name" />
<asp:BoundField DataField="TestID" HeaderText="Test Run ID" />
<asp:BoundField DataField="TestName" HeaderText="Test Name" />
<asp:BoundField DataField="TestOutcome" HeaderText="Test Outcome" />
<asp:BoundField DataField="ErrorMessage" HeaderText="Error Message" />
</Columns>
</asp:GridView>
Point to Note
If you have
generated a fresh c# class file from schema, don’t forget to comment Items
property of these two class:
1. GenericTestType
2.
CodedWebTestElementType
Conclusion
With schema
provided by the Microsoft it’s easy to parse trx file.