Title: How to use Azure DevOps Rest API to get code coverage details
Tags: azure-devops;azure-pipelines;azure-devops-rest-api
Question: I am trying to use &quot;Code Coverage - Get Build Code Coverage&quot; 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
&quot;value&quot;: [],
&quot;count&quot;: 0
My question is how can I get the coverage details of my build using REST API?


Here is another answer: 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}&amp;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
Comment for this answer: Hi @sacheu, I have updated the answer, please check the update2
Comment for this answer: Hi @sacheu, How about the issue? Does the answer below resolved your question? If not, would you please let me know the latest information about this issue?
Comment for this answer: Hi @sacheu, Would you mind sharing the final way to get code coverage details as the answer? It could help other community members who get the same issues and we could archive this thread. Thanks. Have a nice day. :)
Comment for this answer: Thanks. How can I create .coverage file? From Visual Studio Test task document, I don't see how to create a .coverage file.  https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/test/vstest?view=azure-devops
Comment for this answer: Thanks for your follow up. I have tried adding codeCoverageEnabled to my yaml file, but that does not solve this issue. And I have decided not to use ado rest api to get the information.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:01/12/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<azure-devops><azure-pipelines><azure-devops-rest-api>', 'Title': 'How to use Azure DevOps Rest API to get code coverage details', 'ViewCount': 586, 'id': 65679018, 'user_AboutMe': None, 'user_AccountId': 20217020.0, 'user_DisplayName': 'sacheu', 'user_Reputation': 15.0, 'user_Views': 24.0, 'user_id': 14827491.0}}