Search code examples
tfstfs-sdk

Unable to upload attachments to testresult using TFS API


I am trying to upload attachments using TFS API Following is the code snippet :

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

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

The code doesn't throw any error. Also i see that IAttachmentOwner.AttachmentUploadCompleted Event has been raised indicating it is completed. Yet I am not able to see the uploaded attachments on my TFSWebPortal. Am I missing something here ?

P.S : First question here. Please feel free to correct me.


Solution

  • You can get it works with the following code:

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

    Then you should be able to find the attachement in the test result you're working with in MTM.