How to programmatically generate a trx file?

强颜欢笑 提交于 2019-12-22 04:32:06

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/21028962/how-to-programmatically-generate-a-trx-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!