9

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

4

1 回答 1

8

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

请注意,由于“项目”字段的一些继承问题,例如 GenericTestType 和 PlainTextManualTestType(均派生自 BaseTestType),您可能会收到 InvalidOperationException。谷歌搜索应该有解决方案。基本上将所有“项目”定义放入 BaseTestType。这是链接:TestRunType 的序列化抛出异常

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

祝你好运!

于 2015-03-27T23:12:41.000 回答