Add an option to print a code coverage summary to standard output

Đang mở
#233 0 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
52/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
csharp
Lĩnh vực
cli, testing-qa

Hướng nghiên cứu

Bắt đầu từ phần xử lý tùy chọn dòng lệnh của Microsoft.Testing.Extensions.CodeCoverage và đường dẫn hoàn tất việc thu thập coverage được sử dụng bởi lời gọi dotnet test đã nêu. Xác định và triển khai hành vi tóm tắt ổn định được yêu cầu, sau đó xác minh rằng đầu ra chuẩn bao gồm các tổng coverage trong khi artifact Cobertura vẫn được tạo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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:

  1. Locates the generated Cobertura XML report.
  2. Reads the root line-rate attribute.
  3. Converts the fractional value to a percentage.
  4. 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.

Ngôn ngữ chính
C#
Star
125
Fork
17
Merge trung bình
1 giờ 17 phút
Pull request đã merge (30 ngày)
2

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/codecoverage

Tất cả issue của microsoft/codecoverage

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.