Adding attachment to test case in MTM

785 views Asked by At

I am trying to add an attachment to a test case In MTM using TFS Api in C#. I am not getting any exception thrown, but still I cant see the uploaded file in TFS. I am adding the relevant code that I am trying to add the attachment for your reference. Please help me out.

foreach (ITestCaseResult result in results)
{
   result.Attachments.Add(result.CreateAttachment(@"ThePath"));    
   result.Outcome = TestOutcome.Failed;                    
   result.State = TestResultState.Completed;
   result.Save();
} 
results.Save(true);
testRun.Save();
testRun.Refresh();
2

There are 2 answers

1
Tingting0929 On BEST ANSWER

The code you posted above is correct. It could add an attachments to your test results.

Pay attention that the code above is not used to add attachments to test run or test case, it's used to add attachments to test results. Please make sure you refer to the test result page to check attachments in TFS.

Find the test run, go to Test result, double click the test results to check if there any attachments.

enter image description here

0
Kritz On

This worked for me:-

                String path= @"path";
                var store = tfsCollection.GetService<WorkItemStore>();
                Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem wi = store.GetWorkItem(testCaseId);
                wi.Attachments.Add(new Attachment(path));
                wi.Save();