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



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

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

请注意,由于"Items"字段的一些继承问题,如GenericTestType和PlainTextManualTestType(均源自BaseTestType),您可能会得到InvalidOperationException。应该有一个谷歌搜索的解决方案。基本上将所有"项"定义放入BaseTestType中。以下是链接:TestRunType的序列化引发异常

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

祝你好运!

最新更新