How to programmatically generate a trx file?

I was looking for this topic, I did not find any useful information to do it step by step, so I studied it and shared it. Here is a simple solution.

+6
source share
1 answer

Locate the vstst.xsd file in your VisualStudio installation, use xsd.exe to generate the .cs file:

xsd.exe / classes vstst.xsd

The above vstst.cs file contains all classes defining all fields / elements in the trx file.

you can use this link to examine some fields in the 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 the mstest run to find out the field.

with vstst.cs and your knowledge of the trx file, you can write code similar to the following to create the 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 an InvalidOperationException due to some inheritance issues in the "Items" fields, such as GenericTestType and PlainTextManualTestType (both derived from BaseTestType). There must be a googling solution. Essentially all definitions of β€œItems” in BaseTestType. Here is the link: TestRunType serialization throwing an exception

so that the trx file can open in VS, there are some fields that you need to add, including TestLists, TestEntries, TestDefinitions and results. You need to link some of them. If you look into an existing trx file, it’s not difficult to find out.

Good luck

+6
source

All Articles