Reputation: 45
I am trying to use "Code Coverage - Get Build Code Coverage" REST API, to get the code coverage details of my build:
When I use the api and do not specify any 'flags' values', I only get the summary information.
And when I add flags=7
or flags=2
or flags=4
, I only get
{
"value": [],
"count": 0
}
My question is how can I get the coverage details of my build using REST API?
Upvotes: 3
Views: 1560
Reputation: 25956
I found a different approach to get the coverage details.
Initially I tried getting the code coverage details by calling the Azure DevOps REST API too, but it gets too complicated since the API is password protected therefore I need to set personal access token (PAT), this PAT has expiry, so I want to avoid this complication.
Instead of calling the api, I realized that the coverage details are in the generated Cobertura.xml
or Summary.json
, so there's no need to call the API.
Here is a sample yaml pipeline code (the relevant part):
... build your code here, then...
- task: DotNetCoreCLI@2
displayName: 'Run functional tests'
inputs:
command: 'test'
projects: '**/*FunctionalTests.csproj'
arguments: '-c $(buildConfiguration) --collect:"XPlat Code Coverage" --settings $(Build.SourcesDirectory)/coverlet.runsettings.xml'
- task: reportgenerator@5 # https://josh-ops.com/posts/azure-devops-code-coverage/
displayName: 'Generate code coverage report'
inputs:
reports: '$(Agent.WorkFolder)/**/coverage.cobertura.xml'
targetdir: '$(Build.SourcesDirectory)/CoverageResults'
reporttypes: 'HtmlInline_AzurePipelines_Dark;Cobertura;JsonSummary'
- task: PowerShell@2
displayName: 'Check code coverage'
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
inputs:
targetType: 'inline'
script: |
$jsonSummary = Get-Content -Raw $(Build.SourcesDirectory)/CoverageResults/Summary.json | ConvertFrom-Json
$codeCoverage = $jsonSummary.summary.linecoverage
Write-Host "Code coverage: $codeCoverage%"
if ($codeCoverage -lt 80)
{
Write-Error "Code coverage is below 80%. Current coverage: $codeCoverage%"
Exit 1
}
else
{
Write-Host "Code coverage meets/exceeds 80%. Current coverage: $codeCoverage%" -ForegroundColor Green
}
..the rest of your pipeline code...
What's important is to set JsonSummary
in reporttypes
, then we can load that json and check the linecoverage
. Here the pipeline will spit an error if the line coverage is less than 80%.
Upvotes: 1
Reputation: 8278
You can delete the flags
parameter and make the url like this to check if the response body is your expected.
GET https://dev.azure.com/{organization}/{project}/_apis/test/codecoverage?buildId={buildId}&api-version=6.1-preview.1
As test result:
If we use the task Visual Studio Test
and enable the option Code coverage enabled, we could see the Download code coverage results
link and get the result as shown in the REST API doc
If we use the task Publish code coverage results, we could see the result in the UI, but cannot get response body via REST API.
Update1
According to the product team's response: Azure devops only support the download link for .coverage files currently. So we could not use this API to get the UI coverage report details
Update2
If you are using classic editor mode to create pipeline, we could see the option Code coverage enabled
, check the pic below.
If you are using YAML mode to create pipeline, the field is codeCoverageEnabled
, we need to add the code codeCoverageEnabled: true
Upvotes: 4