无法使用TFS API将附件上载到testresult

时间:2015-08-12 19:12:59

标签: tfs tfs-sdk

我正在尝试使用TFS API上传附件 以下是代码段:

    result.State = TestResultState.Completed;
    result.RunBy = identity;

    var attachment = result.CreateAttachment(logFilePath);
    run.Attachments.Add(attachment);

代码不会抛出任何错误。此外,我看到IAttachmentOwner.AttachmentUploadCompleted事件已经引发,表明它已完成。 但我无法在TFSWebPortal上看到上传的附件。 我在这里错过了什么吗?

P.S:第一个问题在这里。请随时纠正我。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfsservername:8080/tfs/DefaultCollection"));
        ITestManagementTeamProject project = tfs.GetService<ITestManagementService>().GetTeamProject("projectName");

        foreach (ITestPlan p in project.TestPlans.Query("Select * From TestPlan"))
        {
            ITestRun testRun = p.CreateTestRun(false);
            var testPoints = p.QueryTestPoints("SELECT * from TestPoint");
            foreach (ITestPoint testPoint in testPoints)
            {
                testRun.AddTestPoint(testPoint, null);
            }
            testRun.Save();

            ITestCaseResultCollection results = testRun.QueryResults();

            foreach (ITestCaseResult result in results)
            {
                result.Attachments.Add(result.CreateAttachment(@"C:\Users\visong\Pictures\000.jpg"));
                result.Outcome = TestOutcome.Warning;
                result.State = TestResultState.Completed; 
                results.Save(true);
            }

            testRun.Save();
            testRun.Refresh();
        }

然后您应该能够在MTM中使用的测试结果中找到附件。