TFS API Part 51 – Adding Test Step & Shared Step

07/10/2013

Working TFS API for Test Cases is different from Work Items, the usage is different and this cause many of customer to send me questions regarding Test Case API.

This post will demonstrate how to create new Test Case, Adding Steps, Step Attachments, Shared Steps and more.

As always we need to start with connecting TFS Object Model:

TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
if (tpp.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

TfsTeamProjectCollection Tfs = tpp.SelectedTeamProjectCollection;

For Work Items we need to obtain WorkItemStore object, but for Test Cases we need to work with ITestManagmentTeamProject object.

ITestManagementServiceservice = (ITestManagementService)Tfs.GetService(typeof(ITestManagementService));
ITestManagementTeamProject TestProject = (ITestManagementTeamProject)service.GetTeamProject(tpp.SelectedProjects[0].Name);
The ITestManagmentTeamProject contains many helpers for Test API
  • Public propertyTestPlans – Gets a helper object that provides access to the test plans in this project.
  • Public propertyTestPoints – Contains methods to block and unblock test points.
  • Public propertyTestResolutionStates – Gets a helper object that provides access to the test resolution states in this project.
  • Public propertyTestResults – Gets a helper object that provides access to the test results that are shown in this project.
  • Public propertyTestRuns – Gets a helper object that provides access to the test runs in this project.
  • Public propertyTestSettings – Gets a helper object that provides access to the test settings in this project.
  • Public propertyTestSuites – Gets a helper object that provides access to the test suites in this project.
  • TestCases – Gets a helper object that provides access to the test cases in this project.
  • SharedSteps – Gets a helper object that provides access to the shared steps in this project.
  • Public propertyTestVariables – Gets a helper object that provides access to the test variables in this project.
For this post I’ll use TestCases helper to create and find new Test Case under my Team Project.
ITestCase testCase = TestProject.TestCases.Find(1);
    //  TestProject.TestCases.Query("QUery Text")
    //  TestProject.TestCases.InPlans("Query Text", isInPlans)
    //  TestProject.TestCases.Create()

Once you’ve created new Test Case you can start adding Steps and Shared Steps into the Test Case.

ITestStep newStep = testCase.CreateTestStep();
newStep.Title = "New Step Title";
newStep.ExpectedResult = "New Step Expected Result";
testCase.Actions.Add(newStep); //Don't forget adding it to the Test Actions List

ISharedStepReference sharedStepReference = testCase.CreateSharedStepReference();
sharedStepReference.SharedStepId = 2;
testCase.Actions.Add(sharedStepReference); //Don't forget adding it to the Test Actions List

//Create Test Step Attachment
ITestAttachment attachment = newStep.CreateAttachment(@"C:\Temp\Image.png");
newStep.Attachments.Add(attachment);

testCase.Save();

You can also add a bulk of test actions using ActionGroup object:

ITestStep newStep = testCase.CreateTestStep();
newStep.Title = "Step Number 1";
newStep.ExpectedResult = "New Step Expected Result";

ITestStep newStep2 = testCase.CreateTestStep();
newStep2.Title = "Step Number 2";
newStep2.ExpectedResult = "New Step Expected Result";

ITestStep newStep3 = testCase.CreateTestStep();
newStep3.Title = "Step Number 3";
newStep3.ExpectedResult = "New Step Expected Result";

ITestActionGroup group = testCase.CreateTestActionGroup();
group.Name = "Action Group";
group.Actions.Add(newStep);
group.Actions.Add(newStep2);
group.Actions.Add(newStep3);

testCase.Actions.Add(group);
testCase.Save();

Enjoy

Add comment
facebook linkedin twitter email