Add Test Case to ITestSuiteBase in TFS API
Asked Answered
R

2

5

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?

Registrar answered 13/10, 2014 at 20:20 Comment(0)
R
5

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();
}
Registrar answered 22/10, 2014 at 18:18 Comment(0)
N
4

This is expected.

Static Test Suites are ... static while Requirement-based Test Suites are dynamic. The relationship between a Test Case and a Requirement is determined by the presence of a proper Tests/Tested By Work Item Link, so you need to add such a link.

For sample code see Not able to add test cases to type of IRequirementTestSuite.

Small note: you cannot duplicate links, so you may have to check for existence if the Test Case is not new.

Nievelt answered 14/10, 2014 at 5:30 Comment(1)
So basically I need to add links to the existing test cases in order to associate them with the requirement based test suite? I realize the intention is to be dynamic, but through MTM (or the web equivalent) you can add test cases directly to a Requirement Based Suite. I'll take a look at the solution you've linked as well!Registrar

© 2022 - 2024 — McMap. All rights reserved.