问题
ITestCaseResult has a property "ErrorMessage"
The ErrorMessage property has a max size of 527bytes, which is not enough to store weighty Exception structures and unfortunately does not seem to have an appropriate Exception property to store one in.
My question is, when a test fails, how do you get the StackTrace? I know its available because the GUI can pull it up in Visual Studio
And as you can see the ErrorMessage associated with a ITestCaseResult is cut short. (If you zoom with your browser the image regains its clarity.)
I found a possible lead in the RunInfo object : http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.common.runinfo.aspx.
It has an proper exception structure, and seems to be associated with a run, but I can't connect the dots between a ITestCaseResult and a RunInfo Object. I might be barking up the wrong tree.
回答1:
I think Visual Studio gets this info from the TRX file which it stores as an attachment to the test run.
I got the attachment as follows:
TfsTeamProjectCollection tfsCollection = new TfsTeamProjectCollection(new Uri("http://tfsserver:8080/tfs/CTS"));
ITestManagementService tfsStore = tfsCollection.GetService<ITestManagementService>();
ITestManagementTeamProject tcmProject = tfsStore.GetTeamProject("MyProject");
// Get Test Run Data by Build
ITestRun thisTestRun = tcmProject.TestRuns.ByBuild(new Uri("vstfs:///Build/Build/" + buildnum)).First();
foreach (ITestAttachment attachment in thisTestRun.Attachments)
{
if (attachment.AttachmentType == AttachmentTypes.TrxTmiTestRunSummary)
{
WebRequest request = HttpWebRequest.Create(attachment.Uri);
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
WebResponse response = request.GetResponse();
using (System.IO.Stream responseStream = response.GetResponseStream())
{
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream);
trxFile.Load(reader);
}
break;
}
}
And then to get the stack trace for a named test I did the following:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(trxFile.NameTable);
nsmgr.AddNamespace("vstt", "http://microsoft.com/schemas/VisualStudio/TeamTest/2010");
// get result node for this testcase from trx file
XmlNode node =
trxFile.SelectSingleNode(
"//vstt:UnitTestResult[@testName=\"SourceWorkflowAsSvcWhenErrorConvertingFile\"]/vstt:Output/vstt:ErrorInfo",
nsmgr);
if (node != null)
{
Console.WriteLine("Stack Trace: " + node.InnerText);
}
I used this reference to get the code for getting attachment: http://social.msdn.microsoft.com/Forums/vstudio/en-US/7fecbac7-7c42-485f-9dd2-5bcb4e71fc39/retrieving-trx-file-stored-in-tfs-for-a-test
来源:https://stackoverflow.com/questions/24152415/getting-the-stacktrace-for-a-testcaseresult-that-failed-in-tfs-via-api