10

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.

kongkongt
  • 301
  • 3
  • 9
  • It's perfectly OK to answer your own questions here on SO (and even to accept you own answer) but please respect the standard question-answer format, ie, question above and formulate you answer in an answer block below. – fvu Jan 09 '14 at 19:12
  • Thanks for reminding. With one new account, I can't answer my own questions in 8-hour interval. Will come back fix it later. – kongkongt Jan 09 '14 at 19:23
  • Could you add a sample of a generated TestResults file to this question? – oɔɯǝɹ Apr 09 '14 at 17:53

1 Answers1

10

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!

kongkongt
  • 301
  • 3
  • 9
  • I never could get this to completely work for making an XML file that Azure Devops liked, but +1 for the xsd.exe tool tip, I found some schema for JUnit, and used that instead with the xsd.exe tool, and it works well. – Lavamantis Jun 08 '23 at 22:12