How to use Test Step using REST Client Helper?

Pankaj Khanzode

Test Case is the backbone for all manual testing scenarios. You can create test case using the web client from Test or Work hubs OR from Microsoft Test Manager (MTM), which then are stored in Team Foundation Server or Visual Studio Team Services. Using these clients you can create test artifacts such as test cases with test steps, test step attachments, shared steps, parameters, shared parameter. Test case is also a work item and using Work Item REST API support one can create a work item of type test case, see here: Create a work item.


Until this release, there is no support to modify/update test steps in a test case work item. Work item saves test steps, associated test step attachment, or expected results in custom XML document, and there is a need of helper to create that custom XML for test steps updates.


With the current deployment, we have added support to Create/Read/Update/Delete test step (action, and expected result) and test step attachments. ITestBase interface exposes required key method – loadActions and saveActions that provide helper methods for both in C# and JS to do above mentioned operations.


C# Client (Microsoft.TeamFoundationServer.Client) as released in previous deployment. OR JS Client (vss-sdk-extension) (Note: JS changes will be available only after current deployment completes.)

Walk through using new helper in C# client

Here, let’s walk through step-by-step on how to consume these newly added helper classes. We have also added GitHub sample for the same with some more operations (link given at the bottom of the post).

  1. Create an instance of TestBaseHelper class and generate ITestBase object using that.
    TestBaseHelper helper = new TestBaseHelper();
    ITestBase testBase = helper.Create();
  2. ITestBase exposes methods for create test step, generate xml, save actions and load actions. You can even assign title, set expected result and description with each test step and associate attachment using attachment URL. In the end, all test steps are added to actions associated with testBase object (see below).
    ITestStep testStep1 = testBase.CreateTestStep();
    testStep1.Title = "title1";
    testStep1.ExpectedResult = "expected1";
    testStep1.Description = "description1";
    testStep1.Attachments.Add(testStep1.CreateAttachment(attachmentObject.Url, "attachment1"));
  3. A call to SaveActions uses the helper classes and calls appropriate field setting of the test case – Test Steps to save newly added steps, expected result and attachment links. A JSON patch document created using “SaveActions” is used to createWorkItemAsync as shown below.
    JsonPatchDocument json = new JsonPatchDocument();
    // create a title field
    JsonPatchOperation patchDocument1 = new JsonPatchOperation();
    patchDocument1.Operation = Operation.Add;
    patchDocument1.Path = "/fields/System.Title";
    patchDocument1.Value = "New Test Case";
    // add test steps in json
    // it will update json document based on test steps and attachments
    json = testBase.SaveActions(json);
    // create a test case
    var testCaseObject = _witClient.CreateWorkItemAsync(json, projectName, "Test Case").Result;
  4. To modify a test case and its steps, you need to get the test case and just call “LoadAction” which internally uses helper class to parse the given xml and attachmentlinks as shown below. This will populate the testBase class with all details as appropriate.
    testCaseObject = _witClient.GetWorkItemAsync(testCaseId, null, null, WorkItemExpand.Relations).Result;
    // initiate testbase object again
    testBase = helper.Create();
    // fetch xml from testcase object
    var xml = testCaseObject.Fields["Microsoft.VSTS.TCM.Steps"].ToString();
    // create tcmattachemntlink object from workitem relation, teststep helper will use this
    IList tcmlinks= new List();
    foreach (WorkItemRelation rel in testCaseObject.Relations)
        TestAttachmentLink tcmlink = new TestAttachmentLink();
        tcmlink.Url = rel.Url;
        tcmlink.Attributes = rel.Attributes;
        tcmlink.Rel = rel.Rel;
    // load teststep xml and attachemnt links
    testBase.LoadActions(xml, tcmlinks);
  5. Once testBase object has been loaded with test case information, you can update test steps and attachments in the test case object.
    ITestStep testStep;
    //updating 1st test step
    testStep = (ITestStep)testBase.Actions[0];
    testStep.Title = "New Title";
    testStep.ExpectedResult = "New expected result";
    //removing 2nd test step
    //adding new test step
    ITestStep testStep3 = tb.CreateTestStep();
    testStep3.Title = "Title 3";
    testStep3.ExpectedResult = "Expected 3";
  6. Update test case object using new changes in the test steps and attachments.
    JsonPatchDocument json2 = new JsonPatchDocument();
    json2 = testBase.SaveActions(json2);
    // update testcase wit using new json
    testCaseObject = _witClient.UpdateWorkItemAsync(json2, testCaseId).Result;

As shown above, you can now use the helper classes provided to update test case steps, and still use the existing Work Item REST APIs for test case work item. You can find comprehensive samples for both C# and JS here on GitHub project: RESTApi-Sample.

  • Test Management Team


Discussion is closed.

Feedback usabilla icon