I'm writing a console application in C# using the Microsoft.TeamFoundation
classes to connect to a Visual Studio Team Foundation Server 2015 on-premises instance.
My application needs to create/upload test cases and link them to existing user stories.
If I use the RelatedLink
class and add it to the ITestCase.Links
property, when I view the test case through the web portal, the links appear under the All Links tab, and not the Tested User Stories tab.
How can I go about linking test cases and stories so that they appear in the Tested User Stories tab instead?
You need to set the Link Type to "Tested By
".
Try below code sample to link a test case to an existing user story: (Install the Nuget package Microsoft.TeamFoundationServer.ExtendedClient)
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Linq;
namespace AssociateWorkitems
{
class Program
{
static void Main(string[] args)
{
int UserStoryID = 53;
int TestCaseID = 54;
TfsTeamProjectCollection tfs;
tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://ictfs2015:8080/tfs/DefaultCollection"));
tfs.Authenticate();
var workItemStore = new WorkItemStore(tfs);
WorkItem wit = workItemStore.GetWorkItem(UserStoryID);
//Set "Tested By" as the link type
var linkTypes = workItemStore.WorkItemLinkTypes;
WorkItemLinkType testedBy = linkTypes.FirstOrDefault(lt => lt.ForwardEnd.Name == "Tested By");
WorkItemLinkTypeEnd linkTypeEnd = testedBy.ForwardEnd;
//Add the link as related link.
try
{
wit.Links.Add(new RelatedLink(linkTypeEnd, TestCaseID));
wit.Save();
Console.WriteLine(string.Format("Linked TestCase {0} to UserStory {1}", TestCaseID, UserStoryID));
}
catch (Exception ex)
{
// ignore "duplicate link" errors
if (!ex.Message.StartsWith("TF26181"))
Console.WriteLine("ex: " + ex.Message);
}
Console.ReadLine();
}
}
}