Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically generate a trx file?

Tags:

xml

xsd

trx

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.

like image 748
kongkongt Avatar asked Jan 09 '14 19:01

kongkongt


People also ask

How do I create a TRX file?

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\ ).

What is a TRX file?

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.

How to parse a trx file?

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.


1 Answers

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!

like image 100
kongkongt Avatar answered Oct 09 '22 02:10

kongkongt