Unable to get the Test results in the correct order as listed in MTM

315 Views Asked by At

I am trying to create a report with the result of each step executed in a Test Case and I am able to retrieve the tests steps, expected result, step outcome, error message, attachments.

The tests steps and expected result are listed in the correct order as seen on MTM, but the step outcome and error message seem to be re-arranged for all test case that I retrieve. This is my code, please assist.

foreach (ITestSuiteEntry testcase in ts.TestCases)
{
    var testResults = testProject.TestResults.ByTestId(testcase.TestCase.Id);
    foreach (ITestCaseResult result in testResults)
    {
        for (int actionIndex = 0; actionIndex < testcase.TestCase.Actions.Count; actionIndex++)
        {
            resultData = new TestResultData();
            var actionStep = testcase.TestCase.Actions[actionIndex] as ITestStep;
            if (actionStep != null)
            {
                resultData.TestCaseName = result.TestCaseTitle;
                resultData.Step = Regex.Replace(actionStep.Title, @"<[^>]+>|&nbsp;", "").Trim();
                resultData.ExpectedResult = Regex.Replace(actionStep.ExpectedResult, @"<[^>]+>|&nbsp;", "").Trim();
            }
            var topIteration = result.Iterations.FirstOrDefault();
            if (topIteration != null && actionIndex < topIteration.Actions.Count)
            {
                var actionResult = topIteration.Actions[actionIndex];
                resultData.StepOutcome = actionResult.Outcome.ToString();
                resultData.Comment = actionResult.ErrorMessage;
                foreach (var attachment in actionResult.Attachments)
                {
                    resultData.AttachmentName = attachment.Name;
                    resultData.AttachmentUri = attachment.Uri.ToString();
                }
            }
            resultDataList.Add(resultData);
        }
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

The results are stored by ActivityId, so the easiest way to get the matching result for each action is to use:

topIteration.Actions.FirstOrDefault(result => result.ActionId == actionstep.Id)

To download attachments from TestCases, you'll need to use the WorkItemStore class to download the work item, then use the WebClient to download the individual files:

var workItemStore = teamProjectCollection.GetService<WorkitemStore>();
var workItem = workItemStore.GetWorkItem(testcaseId)

foreach (Attachment attachment in workItem.Attachments)
{
    var client = new WebClient();
    client.DownloadFile(attachment.Uri, attachment.Name);
}