简体   繁体   中英

How to add test steps in a test case work item using Rest API - TFS2018 / Python

I am using Microsoft's TFS 2018 and I have started writing some Selenium test cases using Python 3.7 in Visual Studio 2018.

I have managed to use the REST API of TFS to return my TFS projects and create new test cases.

What I couldn't find is how to use this API to pass a list with all the test steps of this test case. I am not sure how and if you can add them in the body of the request as a string or array.

At the moment I am trying to make this work on Postman first and then I am going to try in python as well.

This is the request:

curl -X POST \
  'https://TFSLINK:443/DefaultCollection/TFS/_apis/wit/workitems/$Test%20Case?api-version=4.1' \
  -H 'Authorization: Basic MYKEY' \
  -H 'Content-Type: application/json-patch+json' \
  -H 'cache-control: no-cache' \
  -d '[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample task 2"
  }
]'

Is there a way to achieve adding steps ? The API didn't mention anything about this.

In the response I get after creating a test case I get a section called 'fields' which should have included the steps but I can't see them in my response.

{
    "id": 731,
    "rev": 1,
    "fields": {
        "System.AreaPath": "TFS",
        "System.TeamProject": "TFS",
        "System.IterationPath": "TFS",
        "System.WorkItemType": "Test Case",
        "System.State": "Design",
        "System.Reason": "New",
        "System.AssignedTo": "Marialena <TFS\\marialena>",
        "System.CreatedDate": "2019-01-09T08:00:50.51Z",
        "System.CreatedBy": "Marialena <TFS\\marialena>",
        "System.ChangedDate": "2019-01-09T08:00:50.51Z",
        "System.ChangedBy": "Marialena <TFS\\marialena>",
        "System.Title": "Sample task 2",
        "Microsoft.VSTS.Common.StateChangeDate": "2019-01-09T08:00:50.51Z",
        "Microsoft.VSTS.Common.ActivatedDate": "2019-01-09T08:00:50.51Z",
        "Microsoft.VSTS.Common.ActivatedBy": "Marialena <TFS\\marialena>",
        "Microsoft.VSTS.Common.Priority": 2,
        "Microsoft.VSTS.TCM.AutomationStatus": "Not Automated"
    },
    "_links": {
        "self": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731"
        },
        "workItemUpdates": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731/updates"
        },
        "workItemRevisions": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731/revisions"
        },
        "workItemHistory": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731/history"
        },
        "html": {
            "href": "https://TFSLINK/web/wi.aspx?pcguid=07b658c4-97e5-416f-b32d-3dd48d7f56cc&id=731"
        },
        "workItemType": {
            "href": "https://TFSLINK/DefaultCollection/18ca0a74-cf78-45bf-b163-d8dd4345b418/_apis/wit/workItemTypes/Test%20Case"
        },
        "fields": {
            "href": "https://TFSLINK/DefaultCollection/_apis/wit/fields"
        }
    },
    "url": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/731"
}

I have tried creating this PATCH request to update the steps but it didn't work

curl -X PATCH \
  'https://TFSLINK:443/DefaultCollection/TFS/_apis/wit/workItems/730?api-version=4.1' \
  -H 'Authorization: Basic MYKEY' \
  -H 'Content-Type: application/json-patch+json' 
  -d '[
  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.TCM.Steps",
    "from": null,
    "value": "Test"
  },
  {
    "op": "add",
    "path": "/fields/Steps",
    "from": null,
    "value": "Test"
  }
]'

And maybe this is a another topic but if the above is achievable, can you also pass the results after you run the test and update the test plan perhaps ? If this is unrelated please help me only with the test steps and ignore this question.

Many thanks.

This is the way to add test steps in Test Case with Rest API:

{ 
    "op": "add", 
    "path": "/fields/Microsoft.VSTS.TCM.Steps",
    "value": "<steps id=\"0\" last=\"1\"><step id=\"2\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 1</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 1</parameterizedString><description/></step></steps>"  
} 

For a few steps (3 on this example):

{ 
    "op": "add", 
    "path": "/fields/Microsoft.VSTS.TCM.Steps",
    "value": "<steps id=\"0\" last=\"4\"><step id=\"2\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\"><P>step 1 \"Action\"</P></parameterizedString><parameterizedString isformatted=\"true\"><P>step 1 \"Expected\"<BR/></P></parameterizedString><description/></step><step id=\"3\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\"><P>step 2 \"Action\"<BR/></P></parameterizedString><parameterizedString isformatted=\"true\"><P>step 2 \"Expected\"<BR/></P></parameterizedString><description/></step><step id=\"4\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\"><P>step 3 \"Action\"<BR/></P></parameterizedString><parameterizedString isformatted=\"true\"><P>step 3 \"Expected\"<BR/></P></parameterizedString><description/></step></steps>"  
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM