Search code examples
azure-devopsazure-devops-rest-apiazure-devops-extensions

How to add test steps in a test case work item using Rest API


Image of Test Case I have created a custom work item in which i have successfully managed to call api to create work item but i have no idea how to create test steps for that work item.

 let body = [
      {
        "op": "add",
        "path": "/fields/System.Title",
        "from": null,
        "value": "Sample1"
      },
      {
        "op": "add",
        "path": "/fields/Microsoft.VSTS.TCM.Steps",
        "value":"<Steps id=\"0\">     <step id=\"2\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 1</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 1</parameterizedString><description/></step>     <step id=\"3\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 2</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 2</parameterizedString><description/></step></Steps>"
      },
    ]
    fetch(`https://dev.azure.com/${organizationName}/${projectId}/_apis/wit/workitems/$CUSTOM TASK 1?api-version=7.1-preview.3`, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json-patch+json',
        'Authorization': 'Basic ' + btoa("" + ":" + token)
      },
      body: JSON.stringify(body)
    })

i have tried the above code but it just creates a work item but the test steps are not visible in the work item This is what i get in response:

fields:
Microsoft.VSTS.TCM.Steps: "<steps id=\"0\">     <step id=\"1\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 1</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 1</parameterizedString><description/></step>     <step id=\"2\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 2</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 2</parameterizedString><description/></step></steps>"
System.AreaPath: "Azure devops test  demo"
System.ChangedBy: {displayName: 'sid vaidya', url: 'https://spsprodsin2.vssps.visualstudio.com/A120553…s/Identities/7131ed99-7bde-6f31-8161-653548dbc4fe', _links: {…}, id: '7131ed99-7bde-6f31-8161-653548dbc4fe', uniqueName: 'sidvaidya34@gmail.com', …}
System.ChangedDate: "2022-03-09T05:07:10.04Z"
System.CommentCount: 0
System.CreatedBy: {displayName: 'sid vaidya', url: 'https://spsprodsin2.vssps.visualstudio.com/A120553…s/Identities/7131ed99-7bde-6f31-8161-653548dbc4fe', _links: {…}, id: '7131ed99-7bde-6f31-8161-653548dbc4fe', uniqueName: 'sidvaidya34@gmail.com', …}
System.CreatedDate: "2022-03-09T05:07:10.04Z"
System.IterationPath: "Azure devops test  demo"
System.Reason: "Moved to state To do"
System.State: "To do"
System.TeamProject: "Azure devops test  demo"
System.Title: "Steps"
System.WorkItemType: "Custom Task 1"

Solution

  • Your XML is slightly off, that's what's causing the issue. There are two small issues I saw that when corrected, allow the steps to properly be interpreted by the work item:

    1. <Steps /> should be LOWERCASE <steps />
    2. Your step Ids start at 2 instead of 1

    Try this XML:

    <steps id=\"0\"> <step id=\"1\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 1</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 1</parameterizedString><description/></step> <step id=\"2\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 2</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 2</parameterizedString><description/></step></steps>