无法使用 TFS 将附件上传到测试结果 API

Unable to upload attachments to testresult using TFS API

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

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

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

代码没有抛出任何错误。我还看到 IAttachmentOwner.AttachmentUploadCompleted 事件已引发,表明它已完成。 但是我无法在我的 TFSWebPortal 上看到上传的附件。 我在这里遗漏了什么吗?

P.S :这里是第一个问题。请随时指正。

您可以通过以下代码获得它:

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[=10=]0.jpg"));
                result.Outcome = TestOutcome.Warning;
                result.State = TestResultState.Completed; 
                results.Save(true);
            }

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

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