microsoft / microsoft/codecoverage
Add an option to print a code coverage summary to standard output
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 125
- Forks
- 17
- Avg merge
- 1h 17m
- Merged PRs (30d)
- 2
Description
Summary
Add an option to Microsoft.Testing.Extensions.CodeCoverage that prints a machine-readable code coverage summary to standard output after coverage collection completes.
The output should include at least the total line coverage percentage and ideally branch and method coverage as well.
This would allow CI systems such as GitLab to extract the coverage percentage directly from the test job output without requiring an additional script that reads and parses the generated Cobertura XML file.
Background and Motivation
I am using Microsoft Testing Platform together with the Microsoft.Testing.Extensions.CodeCoverage package in GitLab CI.
A typical test invocation looks similar to this:
dotnet test \
--no-build \
-- \
--coverage \
--coverage-output-format cobertura \
--coverage-output coverage.cobertura.xml
The extension generates the requested coverage report, but it does not print a summary of the resulting coverage percentages to standard output.
GitLab uses the coverage keyword to extract a coverage percentage from the job log with a regular expression:
test:
script:
- dotnet test -- --coverage --coverage-output-format cobertura
coverage: '/Line coverage:\s*(\d+(?:\.\d+)?)%/'
Because Microsoft.Testing.Extensions.CodeCoverage does not print the resulting percentage, there is no line for GitLab to match.
The current workaround is to add a separate script that:
- Locates the generated Cobertura XML report.
- Reads the root
line-rateattribute. - Converts the fractional value to a percentage.
- Prints a custom line to standard output.
For example:
COVERAGE_FILE=$(find tests-result/coverage -name 'Assembly*.xml' -print -quit)
LINE_RATE=$(awk -F'"' '/^<coverage / { print $2 }' "$COVERAGE_FILE")
awk -v rate="$LINE_RATE" 'BEGIN { printf "Assembly coverage: %.2f%%\n", rate*100 }'
This introduces platform-specific scripting and duplicates parsing logic that is already available inside the coverage extension when it produces the report.
It also makes the integration less convenient than other code coverage tools.
For example, Coverlet prints a coverage summary after collection, including line, branch, and method coverage. Coverlet also supports the teamcity output format, which emits service messages containing values such as:
CodeCoverageL
CodeCoverageB
CodeCoverageM
CodeCoverageAbsLTotal
CodeCoverageAbsLCovered
ReportGenerator provides text-based summary output through report types such as:
-reporttypes:TextSummary
It can read Cobertura and other coverage formats and generate a concise summary containing the total coverage values.
For C and C++ coverage, gcovr supports:
--print-summary
which prints a coverage summary while still allowing an XML report to be generated.
A similar capability directly in Microsoft.Testing.Extensions.CodeCoverage would simplify CI integration and eliminate the need for custom XML parsing.
Proposed Feature
Add a command-line option that prints a coverage summary after the report has been generated.
For example:
--coverage-print-summary
A test command could then look like this:
dotnet test \
-- \
--coverage \
--coverage-output-format cobertura \
--coverage-output coverage.cobertura.xml \
--coverage-print-summary
The standard output could contain a stable summary such as:
Code coverage summary:
Line coverage: 82.46% (1247/1512)
Branch coverage: 71.18% (284/399)
Method coverage: 79.03% (506/640)
GitLab could then extract the total line coverage with:
coverage: '/Line coverage:\s*(\d+(?:\.\d+)?)%/'
It would be useful to define the output as a stable, documented contract so that CI integrations can safely parse it.
An optional format argument could also be provided:
--coverage-summary-format <text|json|teamcity>
Possible formats could include:
text
Human-readable output suitable for terminal logs and regular-expression parsing:
Line coverage: 82.46%
Branch coverage: 71.18%
Method coverage: 79.03%
json
A single machine-readable line:
{"line":{"covered":1247,"total":1512,"percentage":82.46},"branch":{"covered":284,"total":399,"percentage":71.18},"method":{"covered":506,"total":640,"percentage":79.03}}
The minimum requested functionality is a stable text line containing total line coverage, for example:
Line coverage: 82.46%
The summary should be written to standard output by default when the option is enabled and should not replace the generated coverage artifact.
The feature should be included in Microsoft.Testing.Extensions.CodeCoverage, so users of Microsoft Testing Platform do not need to install another global tool or maintain an XML parsing script.
Alternative Designs
Always print a short summary
Instead of introducing a new option, the extension could always print a short coverage summary whenever collection succeeds:
Code coverage results: coverage.cobertura.xml
Line coverage: 82.46%
This would provide a useful default experience, but it would change the current console output. An explicit option would preserve backward compatibility.
Add a text output format
A new value could be added to --coverage-output-format:
--coverage-output-format text
However, this would make users choose between a coverage artifact and console output unless multiple output formats were also supported.
A separate summary option would allow users to generate Cobertura or XML while also printing the result to standard output.
Use ReportGenerator
ReportGenerator can read the generated report and produce a text summary:
reportgenerator \
-reports:coverage.cobertura.xml \
-targetdir:coverage-summary \
-reporttypes:TextSummary
This works, but it requires an additional tool installation, another command, and another generated file.
The coverage extension already has access to the calculated totals, so printing them directly would be simpler and more efficient.
Use a custom XML parsing script
The pipeline can parse the Cobertura line-rate attribute with PowerShell, Python, xmllint, or another utility.
This is the current workaround, but it adds operating-system-specific code and must be duplicated across repositories.
Use Coverlet
Coverlet prints line, branch, and method totals after collection and also supports CI-oriented formats such as teamcity.
However, users who have selected Microsoft Testing Platform and Microsoft.Testing.Extensions.CodeCoverage should not need to switch coverage engines or add another collector only to obtain a console summary.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the Microsoft.Testing.Extensions.CodeCoverage command-line option handling and the coverage collection completion path used by the shown dotnet test invocation. Define and implement the requested stable summary behavior, then verify that standard output includes the coverage totals while the Cobertura artifact is still generated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- cli, testing-qa
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100