This is a follow-up to my previous post: The Easiest Way to Generate and Publish .NET Code Coverage in Azure DevOps
I was familiar with adding Code Coverage to my pipelines in Azure DevOps and having a Code Coverage tab appear on the pipeline summary page, but I wasn’t sure what was available for GitHub Actions. With GitHub Actions really starting to pick up steam, especially with recent additions such as Composite Actions, I thought now would be a great time to explore.
Adding Code Coverage Report to GitHub Actions
I found this GitHub Action in the marketplace - Code Coverage Summary. There might be others, but this one seemed simple and had the functionality I was looking for.
Here’s the relevant part of my
.github/workflows Action file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # Add coverlet.collector nuget package to test project - 'dotnet add <TestProject.cspoj> package coverlet - name: Test run: dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" --logger trx --results-directory coverage - name: Copy Coverage To Predictable Location run: cp coverage/*/coverage.cobertura.xml coverage/coverage.cobertura.xml - name: Code Coverage Summary Report uses: irongut/CodeCoverageSummary@v1.0.2 # uses: joshjohanning/CodeCoverageSummary@v1.0.2 with: filename: coverage/coverage.cobertura.xml badge: true format: 'markdown' output: 'both' - name: Add Coverage PR Comment uses: marocchino/sticky-pull-request-comment@v2 if: github.event_name == 'pull_request' with: recreate: true path: code-coverage-results.md
Note the test command here that we are using to generate the Cobertura code coverage summary file:
1 dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" --logger trx
We have to use a copy command to copy the
coverage.cobertura.xml to a known location - the marketplace action we are using doesn’t seem to support wildcards and Coverlet uses a random guid folder path.
1 cp coverage/*/coverage.cobertura.xml coverage/coverage.cobertura.xml
The next action is the Code Coverage Summary Report action:
- filename: coverage/coverage.cobertura.xml
- badge: true | false
- format: markdown | text
- output: console | file | both
1 2 3 4 5 6 7 - name: Code Coverage Summary Report uses: irongut/CodeCoverageSummary@v1.0.2 with: filename: coverage/coverage.cobertura.xml badge: true format: 'markdown' output: 'both'
This would be enough to show the code coverage in the action run: Code Coverage Summary Report in the Action run logs
However, the fun doesn’t stop there. How useful would it be to post this to the PR so it’s nice and easy for reviewers? Well, the next action shows a simple way we can add (and sticky) a PR comment with our code coverage report:
1 2 3 4 5 6 - name: Add Coverage PR Comment uses: marocchino/sticky-pull-request-comment@v2 if: github.event_name == 'pull_request' with: recreate: true path: code-coverage-results.md
Perfect - nothing for us to configure here, either. On the pull request, this comment is added:
Code Coverage Summary Report added as a pinned comment to the Pull Request
This is also demonstrated on my pull request here.
You’ll notice the badge along with the markdown table summarizing the code coverage report.
Also, if a new commit is pushed to the PR, triggering a new action run, the comment will be deleted/re-added with the updated code coverage summary.
The above works well if you have a single test project, but if you have more than one, see the “Why not ReportGenerator? section in my previous post for the commands and rationale.
The equivalent in GitHub Actions would be:
1 2 3 4 - name: Create code coverage report run: | dotnet tool install -g dotnet-reportgenerator-globaltool reportgenerator -reports:$(Agent.WorkFolder)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:'Cobertura'
Maybe not as pretty as the Cobertura report shown in Azure DevOps, but just as effective!
And hey, now on the GitHub Pull Request, you get to actually see the code coverage report before the end of the entire pipeline run like in Azure DevOps :).