Vowneee
Vowneee

Reputation: 1459

How to Automate test automation framework with Azure Test Plan in azuredevops

We are trying to automate the integration of test automation framework with Azure Test Plan as described in this article using azuredevops tasks.

I tried first step to get a testplanid from the parameter value for "planName", but its not giving any output. is it because as its not passing the accesstoken ?

parameters:
- name: planName
  type: string

stages:
- stage: get_TestplanID
  pool:
    name: myspoke
  jobs:
  - job: Get_TestPlanID
    steps:
    - script: |
        echo ${AZURE_DEVOPS_CLI_PAT} | az devops login
      env:
        AZURE_DEVOPS_CLI_PAT: $(System.AccessToken)
      displayName: 'Login to Azure DevOps'
    - bash: |
        TestPlan_ID=$(curl --location --request GET 'https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/plans/planID/suites?api-version=5.0' --header 'Authorization: Basic ${AZURE_DEVOPS_CLI_PAT}' | jq ".value[] | select(.name==\"${{ parameters.planName }}\")" | jq .id)
        echo "TestPlanId is $TestPlan_ID"

My requirement here is that, once I could export the value of testplanID, need to pass that variable to the next steps which should be also further azuredevops tasks as below.

  1. 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

  1. 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

  1. 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

  1. 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

  1. 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

  1. Create Bug (Optional step)

Endpoint : https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/wit/workitems/$bug?api-version=5.0 Method : POST Content-Type : application/json-patch+json Sample Payload: [{"op": "add","path": "/fields/System.Title","from":null, "value":"titleOfBug"}] JsonPath to get Bug ID : $.id 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}]}]

Upvotes: 0

Views: 722

Answers (1)

Kim Xu-MSFT
Kim Xu-MSFT

Reputation: 2206

Checking your first job to get a testplanid, the reason why the output is empty is the env variable not passing to the bash script task. Unlike a normal variable, secret variables are not automatically decrypted into environment variables for scripts. You need to explicitly map secret variables. Here is the doc.

Modify your script as below:

stages:
- stage: get_TestplanID
  pool:
    name: myspoke
  jobs:
  - job: Get_TestPlanID
    steps:
    - script: |
        echo ${AZURE_DEVOPS_CLI_PAT} | az devops login
      env:
        AZURE_DEVOPS_CLI_PAT: $(System.AccessToken)
      displayName: 'Login to Azure DevOps'
    - bash: |
        TestPlan_ID=$(curl --location --request GET 'https://dev.azure.com/yourOrganizationName/yourProjectName/_apis/test/plans/planID/suites?api-version=5.0' --header 'Authorization: Basic ${AZURE_DEVOPS_CLI_PAT}' | jq ".value[] | select(.name==\"${{ parameters.planName }}\")" | jq .id)
        echo "TestPlanId is $TestPlan_ID"
      env:
       AAZURE_DEVOPS_CLI_PAT: $(AZURE_DEVOPS_CLI_PAT) # the recommended way to map to an env variable

My requirement here is that, once I could export the value of testplanID, need to pass that variable to the next steps which should be also further azuredevops tasks as below.

If you would like to pass variable in the same job, you need to define the variable in the previous task, and use it in the next task. Refer to this doc.

Here is a sample:

stages:
- stage: get_TestplanID

  jobs:
  - job: createvar
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          Write-Host "##vso[task.setvariable variable=testplanID;]123"
    
    - powershell: echo $(testplanID)

enter image description here

Upvotes: 2

Related Questions