How to automate Classes in code coverage to Azure (ADO) User Stories

52 Views Asked by At

I've been thinking if there is a tool that can help with the creation of azure user stories based on the clasess available in code coverage under c# project? What I wanted to happen is:

  1. Setup runsetting file for c# project
  2. Run the Code Coverage
  3. All classess that needs coverage will automatically create user stories in ADO

this will be helpful before starting committing the start of unit testing.

i havent tried and but im asking if there is an existing app for this or something similar available? Im planning to create one as well if this is possible

1

There are 1 best solutions below

1
Kevin Lu-MSFT On BEST ANSWER

I am afraid that there is no out-of-box method can automatically create User Story Work item based on whether the class runs code coverage.

For a workaround, we can use the script to read the .coverage file(generate after running code coverage) and get class name to create the work item.

Here are the steps:

Step1: Covert the .coverage file to .coveragexml file with the following command:

dotnet tool install --global dotnet-coverageconverter

dotnet-coverageconverter --CoverageFilesFolder "$(Agent.TempDirectory)\TestResults"

.coveragexml sample:

....
<functions>
        <function id="8272" token="0x6000001" name="BankAccount()" namespace="Bank" type_name="BankAccount" block_coverage="0.00" line_coverage="0.00" blocks_covered="0" blocks_not_covered="2" lines_covered="0" lines_partially_covered="0" lines_not_covered="2">

....

Step2: Add powershell task and use PowerShell script to read the .coveragexml file and get all class names. Then we can use the Rest API: Work Items - Create to create the work item.

PowerShell script sample:

$coveragefilename = Get-ChildItem $(Agent.TempDirectory)\TestResults\**\*.coveragexml  |  Select-Object -ExpandProperty Name

[xml]$DSConfig = gc "$(Agent.TempDirectory)\TestResults\**\$coveragefilename"


$ParametersNode = $DSConfig.SelectNodes('//function')
$a =@()
# Loop over selected nodes
foreach($function in $ParametersNode){
   $a += $function.type_name
  }


$a = $a | select -Unique


foreach($classname in $a)
{
   echo $classname
   $token = "PAT"
   
   $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
   $url= "https://dev.azure.com/{organizationname}/{projectname}/_apis/wit/workitems/`$user story?api-version=6.0"
   $JSON3 = "
  [
  {
    `"op`": `"add`",
    `"path`": `"/fields/System.Title`",
    `"from`": null,
    `"value`": `" classname: $classname`"
  }
]
"
 $response3 = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON3 -ContentType application/json-patch+json

}

Result:

enter image description here