2

我正在尝试创建一个包含在测试用例中执行的每个步骤的结果的报告,并且我能够检索测试步骤、预期结果、步骤结果、错误消息、附件。

测试步骤和预期结果按 MTM 上看到的正确顺序列出,但似乎为我检索的所有测试用例重新安排了步骤结果和错误消息。这是我的代码,请帮忙。

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

1 回答 1

2

结果由 ActivityId 存储,因此获取每个操作的匹配结果的最简单方法是使用:

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

要从 TestCases 下载附件,您需要使用 WorkItemStore 类下载工作项,然后使用 WebClient 下载各个文件:

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);
}
于 2014-12-18T18:45:28.563 回答