Search code examples
c#tfsvisual-studio-2013testcasetest-suite

Add Test Case to ITestSuiteBase in TFS API


I'm working with the TFS API and have run into a problem with ITestSuiteBase and IRequirementTestSuite. I've mananged to easily create a new test case within a IStaticTestSuite:

IStaticTestSuite workingSuite = this.WorkingSuite as IStaticTestSuite;
testCase = CreateTestCase(this.TestProject, tci.Title, tci.Description);
workingSuite.Entries.Add(testCase);
this.Plan.Save();

However, this solution doesn't work for requirements test suites or ITestSuiteBase. The method that I would assume would work is:

ITestcase testCase = null;
testCase = CreateTestCase(this.TestProject, tci.Title, tci.Description);
this.WorkingSuite.AllTestCases.Add(testCase);
this.WorkingSuite.TestCases.Add(testCase);
this.Plan.Save();

But this method doesn't actually add the test case to the suite. It does, however, add the test case to the plan. I can query the created test case but it doesn't show up in the suite as expected - even immediately in the code afterwards. Refreshing the working suite has no benefit.

Additional code included below:

    public static ITestCase CreateTestCase(ITestManagementTeamProject project, string title, string desc = "", TeamFoundationIdentity owner = null)
    {
        // Create a test case.
        ITestCase testCase = project.TestCases.Create();
        testCase.Owner = owner;
        testCase.Title = title;
        testCase.Description = desc;
        testCase.Save();
        return testCase;
    }

Has anyone been able to successfully add a test case to a requirements test suite or a ITestSuiteBase?


Solution

  • Giulio's link proved to be the best way to do this

    testCase = CreateTestCase(this.TestProject, tci.Title, tci.Description);
    if (this.BaseWorkingSuite is IRequirementTestSuite)
        TFS_API.AddTestCaseToRequirementSuite(this.BaseWorkingSuite as IRequirementTestSuite, testCase);
    else if (this.BaseWorkingSuite is IStaticTestSuite)
        (this.BaseWorkingSuite as IStaticTestSuite).Entries.Add(testCase);
    this.Plan.Save();
    

    And the important method:

    public static void AddTestCaseToRequirementSuite(IRequirementTestSuite reqSuite, ITestCase testCase)
    {
        WorkItemStore store = reqSuite.Project.WitProject.Store;
        WorkItem tfsRequirement = store.GetWorkItem(reqSuite.RequirementId);
    
        tfsRequirement.Links.Add(new RelatedLink(store.WorkItemLinkTypes.LinkTypeEnds["Tested By"], testCase.WorkItem.Id));
        tfsRequirement.Save();
    
        reqSuite.Repopulate();
    }