Search code examples
azure-devopsazure-pipelinesspecflownunit-3.0

How to "Associate a test case" from my Specflow Nunit Automated UI test project to Azure test plan test case If I am not using Azure Devops Repo?


I have a specflow + Nunit with playwright project that has automated UI tests. I have created build and release pipelines for the project which seems to work fine except for one small caveat.

While using Azure Devops Repository and Azure Test Plan, we can using Visual Studio> Test Explorer window and right click on any test to associate it with any test cases in Azure test plan.

From this link: https://learn.microsoft.com/en-us/azure/devops/test/associate-automated-test-with-test-case?view=azure-devops enter image description here

This seems to work if my git repo is hosted in Azure DevOps Repo. Unfortunately at our work we have been using bitbucket. Is there a way I can keep using bitbucket and associate the test cases with Azure test plan test cases?

Background info. We have been using bitbucket of git repository but our CICD is configured in Azure DevOps. We are trying to add Automated UI testing. This setup has worked fine for a while for us until now.

Thanks in advance.


Solution

  • Instead of using Visual Studio, you can use this REST API to update the Associated Automation of testcases.

    PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=7.1-preview.3
    
    [
      {
        "op": "add",
        "path": "/fields/Microsoft.VSTS.TCM.AutomatedTestName",
        "value": "[namespace.classname.methodname (e.g. AzureWebAppDotNetCore.Tests.ProgramTests.AddTest)]"
      },
      {
        "op": "add",
        "path": "/fields/Microsoft.VSTS.TCM.AutomatedTestStorage",
        "value": "[assembly name (e.g. AzureWebAppDotNetCoreTests.dll)]"
      },
      {
        "op": "add",
        "path": "/fields/Microsoft.VSTS.TCM.AutomatedTestType",
        "value": "Unit Test"
      }
    ]
    

    Taking my demo below for example, the test case#838 AddTest was not associated with any automated test. I used the API to associate the test assembly from my code with this test case in my test suite #839of the test plan #836.

    Here is a PowerShell script to use this API and authenticates with Personal Access Token, which is easy to generate and convenient for test but requires your attention to avoid leaking.

    $organization = "YourADOOrgName"
    $project = "TheProjectName"
    $MyPAT = 'xxxxxx'
    
    $B64PAT = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPAT"))
    $URL = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/838?api-version=7.1-preview.3"
    $header = @{
    'Authorization' = 'Basic ' + $B64PAT
    'Content-Type' = 'application/json-patch+json'
    }
    $body = @"
    [
      {
        "op": "add",
        "path": "/fields/Microsoft.VSTS.TCM.AutomatedTestName",
        "value": "AzureWebAppDotNetCore.Tests.ProgramTests.AddTest"
      },
      {
        "op": "add",
        "path": "/fields/Microsoft.VSTS.TCM.AutomatedTestStorage",
        "value": "AzureWebAppDotNetCoreTests.dll"
      },
      {
        "op": "add",
        "path": "/fields/Microsoft.VSTS.TCM.AutomatedTestType",
        "value": "Unit Test"
      }
    ]
    "@
    
    
    
    Invoke-RestMethod -Method PATCH -Uri $URL -Headers $header -Body $body | ConvertTo-Json
    

    enter image description here enter image description here