mercredi 16 janvier 2019

Check whether a test case already exists before creating it or get id from test case after you create it - TFS / Python

I am creating a test case in TFS 2018 using postman like this:

curl -X POST \
  'https://TFSURL:443/DefaultCollection/PROJECT/_apis/wit/workitems/$Test%20Case?api-version=4.1' \
  -H 'Authorization: Basic MYKEY' \
  -H 'Content-Type: application/json-patch+json' \
  -d '[
    {
        "op": "add",
        "path": "/fields/System.AreaPath",
        "from": null,
        "value": "TEST\\Automation"
    },
    {
        "op": "add",
        "path": "/fields/System.IterationPath",
        "from": null,
        "value": "TEST\\Sprint 8"
    },
    {
        "op": "add",
        "path": "/fields/System.Title",
        "from": null,
        "value": "Sample task"
    },
    { 
        "op": "add", 
        "path": "/fields/Microsoft.VSTS.TCM.Steps",
        "value": "<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>
        <step id=\"3\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 3</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 3</parameterizedString><description/></step>
        <step id=\"4\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">Input step 4</parameterizedString><parameterizedString isformatted=\"true\">Expectation step 4</parameterizedString><description/></step></steps>"
    } 
]'

How can I check before sending this request that this test exist so that I can update it instead of creating a new one every time?

I was expecting to do it by either:

  • searching the name of the automation which should be unique in tfs (method name from automation script) and see if it returns something
  • or by getting the key of the test case right after i have created it if this is possible

It seems like in tfs there is a way to do a request to get all work items but I am not sure if you can return based on the title field of the item. (Help page on how to do this request)

I tried to use the search api to return the results if anything with that name exists in tfs but I am getting an error message which I don't know how to resolve

curl -X POST \
  'https://TFSURL:443/DefaultCollection/PROJECT/_apis/search/workitemsearchresults?api-version=4.1-preview' \
  -H 'Authorization: Basic MYKEY' \
  -H 'Content-Type: application/json' \
  -d '{
  "searchText": "Test_Signin_Signout - test_signin"
}
'

Response:

{
    "$id": "1",
    "innerException": null,
    "message": "TakeResults parameter should be in the range [1, 1000]. The parameter supplied is [0].",
    "typeName": "Microsoft.VisualStudio.Services.Search.Shared.WebApi.Exceptions.InvalidQueryException, Microsoft.VisualStudio.Services.Search.Shared.WebApi",
    "typeKey": "InvalidQueryException",
    "errorCode": 0,
    "eventId": 3000

Aucun commentaire:

Enregistrer un commentaire