Search code examples
azure-devopsazure-pipelinesazure-devops-rest-apiazure-devops-extensionsazure-devops-server-2019

How to dynamically add certain TestCases to TestSuites by providing the TestCase name in AzureDevops pipeline


We are trying to use AzureDevops pipeline to automate the integration of test automation framework with Azure Test Plan as described in this article.

We converted below tasks to AzureDevops yaml pipelines bash scripts and put "planName","suitName" and "caseName" as input parameters. We were able to get TestplanID (step1), TestSuiteID(step2) dynamically by passing their names as input parameters. But for the step3, in order to get the testcaseID from the testcase name, first We need to manually add\import the testcase under this testsuit to dynamically get the TestcaseID from the input parameter of testcasename.

its blocking our fully automation of below steps because of this manual import/adding of testcase to the testsuite.

So we are looking for an automated azuredevops task after step2, and before step3, which will dynamically add the given input parameter value of "testcasename" to the testsuite and from there we will be able to get the testcaseid using step3 and can proceed other steps..

Get Test Plan ID
Endpoint : 
https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/plans?api-version=5.0
Method :
GET
JsonPath to get Test Plan ID :
$.value.[?(@.name == 'yourPlanName')].id

2. Get Test Suite ID

Endpoint : 
https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/plans/planID/suites?api-version=5.0
Method :
GET
JsonPath to get Test Suite ID :
$.value.[?(@.name == 'yourSuiteName')].id
planID-is available from step1

3. Get Test Case ID

Endpoint : 
https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/plans/planID/suites/suiteID/points?api-version=5.0
Method :
GET
JsonPath to get Test Case ID :
$..[?(@.name == 'yourTestCaseName')].id
planID-is available from step1
suiteID-is available from step2

4. Get Test Point ID

Endpoint : 
https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/plans/planID/suites/suiteID/points?testCaseId=tcID&api-version=5.0
Method :
GET
JsonPath to get Test Point ID :
$.value.[0].id
planID-is available from step1
suiteID-is available from step2
tcID-is available from step3

5. Create Test Run

Endpoint : 
https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/runs?api-version=5.0
Method :
POST
Content-Type : 
application/json
Sample Payload:
{"name":"runName","plan":{"id":planID},"pointIds":[pointID]}
JsonPath to get Test Run ID :
$.id
planID-is available from step1
pointID-is available from step4

6. Get Test Result ID

Endpoint : 
https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/runs/runID/results?api-version=6.0-preview.6
Method :
GET
JsonPath to get Test Result ID :
$.value.[0].id
runID-is available from step5

8. Update Results in Test Run

Endpoint : 
https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/runs/runID/results?api-version=6.0-preview.6
Method :
PATCH
Content-Type : 
application/json
Sample Payload if Passed:
[{ "id": resultID ,  "outcome": "PASSED" ,"state": "Completed",    "comment": "Execution Successful"  }]
Sample Payload if Failed:
[{ "id": resultID ,  "outcome": "FAILED" ,"state": "Completed",    "comment": "Execution Failed", "associatedBugs": [{"id":bugID}]}]

Solution

  • Please follow below steps to add dynamically the given input parameter value of "testcasename" to the testsuite.

    1 use Query by Wiql REST API to get Test Case Id by adding query condition Test Case Name into query.Refer to doc:Query by Wiql

    for example :

    POST https://dev.azure.com/{organization}/{project}/{team}/_apis/wit/wiql?api-version=7.0
    {
      "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Test Case' And [System.Title] = 'username' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
    }
    

    Test in postman and it'll return the Test Case ID of matching the Test Case Name username

    enter image description here

    2 use Test Suit Add API to add test cases to suit. refer to doc:Test Suit Add This API need parameter testCaseIds, you can get it from step 1.