如何以编程方式生成trx文件?

时间:2014-01-09 19:05:35

标签: xml xsd trx

我搜索过这个主题,没有找到任何好的信息,一步一步地做,所以我研究并在这里分享。这是一个简单的解决方案。

1 个答案:

答案 0 :(得分:6)

在VisualStudio安装中找到vstst.xsd文件,使用xsd.exe生成.cs文件:

xsd.exe / classes vstst.xsd

生成的vstst.cs文件包含定义trx文件中每个字段/元素的所有类。

您可以使用此链接了解trx文件中的某些字段:http://blogs.msdn.com/b/dhopton/archive/2008/06/12/helpful-internals-of-trx-and-vsmdi-files.aspx

您还可以使用从mstest运行生成的现有trx文件来学习该字段。

使用vstst.cs和您对trx文件的了解,您可以编写如下代码来生成trx文件。

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 );

请注意,由于&#34;项目&#34;上的某些继承问题,您可能会收到InvalidOperationException。字段,如GenericTestType和PlainTextManualTestType(均源自BaseTestType)。应该通过谷歌搜索解决方案。基本上把所有&#34;项目&#34;定义为BaseTestType。这是链接:抛出异常的TestRunType的序列化

要使trx文件能够在VS中打开,您需要输入一些字段,包括TestLists,TestEntries,TestDefinitions和结果。你需要链接一些guid。通过查看现有的trx文件,不难发现。

祝你好运!

相关问题