Showing Code Coverage Report with Azure DevOps - azure-devops

I'm using Microsoft Unit Tests to validate my solutions via Azure DevOps Pipelines. I am able to see tests being executed and the % of code coverage
However, I can't see the code coverage results (or they won't render)...
Here is my publish code coverage task - am I missing something to have it render here?

This is a known issue on Azure devops. Now, we could only download the report, and open it with Visual Studio.
Azure devops only support the download link for .coverage files currently. The white page you see is a UI glitch. This scenario is only supposed to render a download link to the coverage file.
Besides, this issue has been submitted in this earlier suggestion ticket linked here:
support vstest .coverage "code coverage" build results tab
This feature request is On Roadmap, I believe it will be released soon, you can follow this thread to know its latest feedback.
In addition, we found a similar case and I have tested via the answer, set the code coverage tool to Cobertura, then I get the code coverage report in the Azure DevOps pipeline.

Related

How to generate Code Analysis report in Azure DevOps for a .NET 5.0 Project?

I am new to code quality analysis and I have a .NET 5.0 Project. Following the instructions in these two articles Overview of .NET source code analysis and Overview of source code analysis, I had added the following settings to the project file.
<PropertyGroup>
<AnalysisLevel>latest</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>
This looks fine as I build the project.
Now I want to get a report when I execute my Azure DevOps build pipeline.
Is there any specific Azure DevOps task for this. I found Roslyn Analyzers build task,
But so far I could not find a way to add this to the pipeline. Looks like this task is a part of Microsoft Security Code Analysis Extension. Furthermore, I may have to purchase this.
All I am looking for is a simple Azure DevOps task which can show me the the code quality analysis report(not code coverage). If there is there one, please suggest.

Calculate code metrics as part of build pipeline in Devops

This seems a pretty straightforward thing to do, but I cannot find relevant information. In Visual Studio is very easy to calculate code metrics for all projects and I would like to do the same during a build pipeline in Azure DevOps.
Has anyone done something like this?
In azure devops, you can review code coverage results. The results can be viewed and downloaded on the Code coverage tab.
Publish Code Coverage Results publishes code coverage results to
Azure Pipelines or TFS, which were produced by a build in Cobertura
or JaCoCo format.
Built-in tasks such as Visual Studio Test, .NET Core, Ant, Maven,
Gulp, Grunt, and Gradle provide the option to publish code coverage
data to the pipeline.
Here are some documents you can refer to:
Review code coverage results
Azure DevOps and the Code Coverage
In addition, you can get code analysis through SonarCloud integrated with Azure devops. SonarCloud is a cloud-based code quality and security service.
Here is the lab you can follow.
You can generate code metrics by adding the Microsoft.CodeAnalysis.Metrics nuget and building with the parameter that target the "Metrics" build, this will store the results in an xml file.
msbuild /t:Metrics
If you're building a solution with multiple projects, you have to add the nuget to every project otherwise the build will fail because the Metrics target won't be present for the other projects.
The build will produce one xml file per project.
You can also use the metrics command line tool from the roslyn analyzer repository.
See https://learn.microsoft.com/en-us/visualstudio/code-quality/how-to-generate-code-metrics-data?view=vs-2022

Azure Devops Pipelines Test Attachments for successful tests

As noted here:
https://learn.microsoft.com/en-us/azure/devops/pipelines/test/review-continuous-test-results-after-build?view=azure-devops#tests-tab
If you use the Visual Studio Test task to run tests, diagnostic output logged from tests (using any of Console.WriteLine, Trace.WriteLine or TestContext.WriteLine methods), will appear as an attachment for a failed test.
How can I enforce Azure Devops pipelines to publish attachments for successful tests?
Update
riQQ pointed to the another stackoverflow question:Azure DevOps: Console output for green tests is missing
Proposed solution doesn't help me, because they solved using TestContext. We are using xUnit, and there is no TestContext for xUnit.
How can I enforce Azure Devops pipelines to publish attachments for
successful tests?
Sorry but as I know this is not supported behavior for VSTest Task. It's by design that only failed tests should have the log as attachment. And after my check, no extensions in Marketplace extends this kind of option.
So if you do want this option, you may consider using rest api like what riQQ suggests above. Or you can post a new feature request in DC forum, it it gets enough votes, the team would consider it seriously.
Hope it helps :)

Integrate jacoco code coverage reports from Azure devops to Code climate

We are using Azure devops to run our build and jacoco to do code coverage in azure build agent. We want to integrate code coverage reports to code climate.
Has anyone tried it ? I tried searching for proper documentation or steps and it seems to be missing.
Any help/pointers ?
For this issue , as far as I know there should be no way to integrate jacoco code coverage reports from Azure devops to Code climate directly . After all, there is already a code coverage feature on azure devops, why integrate code coverage reports into Code climate.
You could add your request for this feature on our UserVoice site , which is our main forum for product suggestions. Our PM and product team will kindly review your suggestion.
A manual method I can think of is to download the code coverage results , then open it in vs and then push it into the repo of the code climate. Because I am not very familiar with code climate, I am not sure about the feasibility of this idea.

Azure DevOps add custom Tab to Build Summary

I want to add a custom tab to my build summary.
Inside these tab I want to display the results of the PowerShell Test Code Coverage task. The results are about 50-200 *.htm reports generated by ReportGenerator.
I try to achive with Write-Host "##vso[task.addattachment type=Distributedtask.Core.Summary;name=Report;]$localFile" but the amount of reports make this option not senseful.
Best case solution would be, if could display the reports like:
AssemblyNameA.dll collapsed:true
index.htm collapsed:false
TargetAssemblyA.htm
TargetAssemblyB.htm
TargetAssemblyC.htm
AssemblyNameB.dll collapsed:false
index.htm collapsed:false
TargetAssemblyA.htm
TargetAssemblyB.htm
TargetAssemblyC.htm
Is there a way to implement that?
Azure DevOps add custom Tab to Build Summary
First, I suppose you are using Azure DevOps Server 2019. Am I right? This is a known issue for Azure DevOps Server 2019:
Code coverage tab missing in Azure DevOps Server
A fix for this issue has been released! Install the most recent
release from https://visualstudio.microsoft.com/downloads/.
Second, if you want to create a real custom summary tab, just like Wouter said, you need to write an extension by Visual Studio Services Web Extension SDK.
MS team has provided a great sample on github Build Results Enhancer, you can check this sample for some more details.
Hope this helps.
To publish the coverage reports created by ReportGenerator you can use the Publish Code Coverage Results task. You need to make sure the format that ReportGenerator creates matches to what the Publish Code Coverage Results task expects.