Merge multiple coverage files inside Github Actions
Problem Statement
When you have multiple test projects in your solution and you want to merge the coverage reports of all the test projects into a single report.
Solution
Install dotnet tool dotnet-coverage
which can merge all the coverage files into a single file.
1
| dotnet tool install -g dotnet-coverage
|
Usage
Merge multiple coverage files
1
| dotnet coverage merge --reports "path/to/coverage1.xml" "path/to/coverage2.xml" --output "path/to/merged.xml"
|
Merge all coverage files in a directory
You need to first set the output directory for all the tests to same folder. Then you can merge all the coverage files in that directory into one file. Which will be easier to upload to any coverage tool or using this single file we can generate HTML output.
1
| dotnet test --collect:"XPlat Code Coverage" --results-directory "coverage-results"
|
1
| dotnet coverage merge --reports "coverage-results/**/coverage.cobertura.xml" -f cobertura -o coverage-results/coverage.xml
|
Github Actions
1
2
3
4
5
6
7
| - name: Install dotnet-coverage tool
run: dotnet tool install -g dotnet-coverage
- name: Run tests and merge coverage files
run: |
dotnet test --collect:"XPlat Code Coverage" --results-directory "coverage-results"
dotnet coverage merge --reports "coverage-results/**/coverage.cobertura.xml" -f cobertura -o coverage-results/coverage.xml
|