-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The Easiest Way to Generate and Publish .NET Code Coverage in Azure DevOps | josh-ops #10
Comments
Just a heads up. If using the script snippet to add reportgenerator it will publish the reports to Then when you use the PublishCodeCoverageResults@1 snippet below that it will try to reference it in Those both have to be the same directory for it to work. |
Nice tip @msawayda, thank you! |
Hi Josh, When multiple unit test projects are present, ;you can also merge the generated reports by adding the following parameter in the test run of the last project to test. --merge-with $(Agent.TempDirectory)/**/coverage.cobertura.xml And then of course afterwards use the PublishCodeCoverageResults task |
@VincentOspazi Do you have a broader example you can share? I couldn't find much about |
Hi @joshjohanning, for ex ample: you can find it in the doc of coverlet : https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/GlobalTool.md
the last test command we output to both cobertura (for Azure DevOps) and opencover (for SonarQube to grab). Hope this helps. |
Thanks Josh I think I have got this up and working now. And the code coverage tab is good to have. I am struggling to find much detail on examples or anything but how to actually post something on the PR to this, even like just the Code Coverage % or something? Microsoft examples talk about only working with their standard coverage format which isn't too helpful. |
@james1301 You could perhaps use an extension or the API to post at least the code coverage % as a comment in the Pull Request? Maybe one of these would help: Use a condition to only run the task when triggered via PR. You would then just have to extract the code coverage from the <coverage line-rate="0.21974657217686028" Something like this might help: Example of coverage xml: |
This got my code coverage working..., thanks a lot! |
Is there a way to fail the build if code coverage is below a certain percentage? Say below 70%? |
Great!
@rosdi Yes! There are several extensions on the marketplace that bring in tasks for this, but my favorite are Colin's ALM Corner Custom Build Tasks, which has a Coverage Gate task. See more information on it here. |
Interesting..., didn't know it is called Coverage Gate. Will surely check these out. |
Great @rosdi. It would certainly be worth an addition to this post to call this task out 😄. |
FYI: I don't know if you know where to find source code for the tasks or not (based on your response about only finding information on
Only one version of the *.xml and *.trx are generated in the /TestResults folder relative to the test csproj file. |
@terryaney thanks for sharing! In your example, does that consolidate/merge the results of multiple test projects into one file? I think in @VincentOspazi's example, they were merging in multiple coverlet/cobertura results into one with the |
No, but as you stated in your post, the |
@terryaney ahhh, I see now. Yes, most definitely you could use the Based on coverlet's docs, it seems like your method is using MSBuild integration as opposed to the way I was referring to it in my post (with the VS Test Platform). If you only cared about the results posting to Azure DevOps and not the And I see now after second a second look in @VincentOspazi's example, they are running |
@rosdi For posterity, posting this here. I found this in the docs while looking up for the other question - you can fail the build natively if you use the You can use: dotnet test /p:CollectCoverage=true /p:Threshold=70 More advanced usage on whether you want have a threshold line/branch/method in the docs. |
Well, I guess I should re-read your post again for the 20th time, lol. I did the Admittedly, I'm just starting to learn about CICD in TFS, but as you can see below, I have the 'Tests' tab but I don't get a 'Coverage' tab (although I can download the zip file). I'm not sure if that is because of something I'm doing wrong or that TFS version we are on does not support it. I was going to look into it more, but this comment seems to say that if you try publishing |
Haha! @terryaney, even I had to re-read what I wrote a few times 😁 But that makes sense. I don't think the version of TFS should matter (but it's possible it does). Ahh, it seems I'm mistaken - when focusing on the Code Coverage report I forgot about the unit test results themselves! The You can upload manually, though, with the Publish Test Results task (it sounds like this is what you are maybe doing). I think instead of using the "Publish Test Results" task to upload the code coverage/
If you upload the I'm not exactly how this will show in your older version of TFS, though, since there is no separate tab for code coverage based on your screenshot 😄. Try it out and let us know! And finally, I don't think I included it in the post, but here's the YAML pipeline this post is using for reference. I played around with updating the Here's an example where I did just that. You have to use |
Thanks for sharing your pipeline @terryaney, did you ever figure it out? Is it something like a pathing error? Or that the If you only had a single test project, you could probably scrap the If you have multiple test projects that you do need to combine, you could try one of these debugging steps:
|
How can we exclude certain projects from code coverage tool? Currently I am using .runsettings as below but it is not working as expected.
|
Hmm @sontambharat - I might be tempted to use ReportGenerator in this case (example). You could have the code coverage reports created, and then simply delete the project(s) you want to exclude before running the ReportGenerator tool that should then combine them all (minus the excluded ones that were deleted). |
Unfortunately, no. I've simplified my build pipeline, but no change. During the processing in my command line tool for the first step, it simply runs this command:
Then I generate coverage reports via:
But I still only get the following view: |
The Easiest Way to Generate and Publish .NET Code Coverage in Azure DevOps | josh-ops
Publishing Code Coverage and making it look pretty in Azure DevOps is way harder than it should be
https://josh-ops.com/posts/azure-devops-code-coverage/
The text was updated successfully, but these errors were encountered: