返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
C#

Analyze the unit test coverage of C# projects to improve code quality

C# coverage analysis? Isn't it just about using tools to generate reports? Which tool out of VSCode, VS, or Rider can't do this? Do you really think this is some kind of rocket science?

曦远 (DealiAxy)约 11 分钟
目录12 个章节

Introduction

As I previewed in my previous article on ImageGlider, this post is also part of the series sharing my experience with the "C# + automated publishing" development workflow. I continue to document the pitfalls I encountered and the lessons learned, hoping we can all avoid unnecessary detours.

The importance of unit testing goes without saying, right? 😄

And coverage ensures the breadth and effectiveness of unit tests—it helps developers uncover overlooked logical branches and prevents the awkward situation of "testing something that wasn't actually tested."

Especially in today’s era of AI programming, comprehensive testing allows AI to automatically verify the results of functionality implementation.

Fortunately, C# has a very robust infrastructure. Such a feature-rich language is particularly well-suited for the AI era. I have a feeling that the .NET platform has a promising future in the age of AI! 😁

When it comes to coverage testing, there are many methods available. Initially, I used a third-party tool to generate HTML reports, but later I discovered that IDEs like VSCode, VS, and Rider all support this as well! 🤣

C# Toolbox

This year I have successively developed quite a few tools using C#

They cover a broad range of areas

It feels like they could be assembled into a small toolbox.

These tools are:

Dependencies

The dotnet-reportgenerator-globaltool tool can generate HTML reports.

dotnet tool install -g dotnet-reportgenerator-globaltool

Test Coverage

Using the ImageGlider project as an example.

Use the following command to analyze the project's unit test coverage and generate a test report webpage.

# Generate Test Coverage Report (生成测试覆盖率报告)
```bash
dotnet test --collect:"XPlat Code Coverage" --results-directory ./temp/TestResults

Using reportgenerator to Generate HTML Reports

reportgenerator -reports:"./temp/TestResults/*/coverage.cobertura.xml" -targetdir:"./TestResults/CoverageReport" -reporttypes:Html


Example of the generated test report path

```bash
temp\TestResults\4eaa9684-a3b6-4b2a-81ac-d75e1e375e4b\coverage.cobertura.xml

Simply open this webpage to view the coverage report.

HTML Report

Very detailed

Overview

image-20250804200121985

View Detailed Coverage

Here you can adjust the grouping mode.

The default is By assembly.

You can change it to By namespace, where the namespace mode is further divided into level 1 and level 2.

Both Line coverage and Branch coverage can be filtered.

For larger projects, it is recommended to select By Namespace level 2, which is more intuitive.

image-20250804200213376

Method Test Coverage

Click on a specific class to navigate to the method coverage page.

image-20250804200947028

Here you can see which methods lack tests or which test cases are not covered. In short, it's very convenient.

image-20250804201013952

VSCode

Right-click on the tests directory and run the coverage test.

Soon, battery-like icons (quite cute 😄) will appear across each directory.

This makes it easy to view the test coverage for each project and code file.

image-20250806213853180

Rider

I first discovered it in VSCode

I thought

If VSCode has this feature, then Rider, the established C# IDE, should definitely have it too

After testing it out, it really does

The menu path is Tests -> Cover Unit Tests

This feature is also pretty good, and it can even export to HTML, among other formats

image-20250806214904865

Summary

In summary, through dotnet-reportgenerator-globaltool and the built-in features of IDEs like VSCode, VS, and Rider, we can easily analyze the unit test coverage of C# projects, helping improve code quality and testing effectiveness.

_EOF_

Analyze the unit test coverage of C# projects to improve code quality

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…