Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 638
Direweasel Avatar asked Oct 13 '14 20:10

Direweasel


Video Answer


2 Answers

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();
}
like image 59
Direweasel Avatar answered Sep 26 '22 01:09

Direweasel


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.

like image 33
Giulio Vian Avatar answered Sep 26 '22 01:09

Giulio Vian