I have searched on this topic, didn't find any good info to do it step by step, so I studied it and shared it here. Here is an easy solution.
To generate a TRX file with SpecFlow+ Runner, you will need to start your tests from the command line using vstest. console.exe with the /logger:trx option to generate a TRX file. You have to execute it in the output folder of your test project ( bin\debug\ ).
Test results file created by Visual Studio, a Microsoft application used to develop Windows software; contains test results stored in an XML format; used to display test results in Visual Studio as well as save historical test case results.
Programmatically parse trx file The schema is available in the 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.
Find the vstst.xsd file in your VisualStudio installation, use xsd.exe to generate a .cs file:
xsd.exe /classes vstst.xsd
The resulted vstst.cs file contains all classes defining every fields/elements in a trx file.
you can use this link to learn some fields in a trx file: http://blogs.msdn.com/b/dhopton/archive/2008/06/12/helpful-internals-of-trx-and-vsmdi-files.aspx
you can also use an existing trx file generated from a mstest run to learn the field.
with the vstst.cs and your knowledge of trx file, you can write code like following to generate a trx file.
TestRunType testRun = new TestRunType();
ResultsType results = new ResultsType();
List<UnitTestResultType> unitResults = new List<UnitTestResultType>();
var unitTestResult = new UnitTestResultType();
unitTestResult.outcome = "passed";
unitResults.Add( unitTestResult );
unitTestResult = new UnitTestResultType();
unitTestResult.outcome = "failed";
unitResults.Add( unitTestResult );
results.Items = unitResults.ToArray();
results.ItemsElementName = new ItemsChoiceType3[2];
results.ItemsElementName[0] = ItemsChoiceType3.UnitTestResult;
results.ItemsElementName[1] = ItemsChoiceType3.UnitTestResult;
List<ResultsType> resultsList = new List<ResultsType>();
resultsList.Add( results );
testRun.Items = resultsList.ToArray();
XmlSerializer x = new XmlSerializer( testRun.GetType() );
x.Serialize( Console.Out, testRun );
note that you may get InvalidOperationException due to some inheritance issues on "Items" fields, like GenericTestType and PlainTextManualTestType (both derived from BaseTestType). There should be a solution by googling. Basically put all "Items" definition into BaseTestType. Here is the link: Serialization of TestRunType throwing an exception
to make the trx file be able to open in VS, there are some fields you need to put in, including TestLists, TestEntries, TestDefinitions and results. You need to link up some of the guids. By looking into an existing trx file, it's not hard to find out.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With