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?
目录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:
- Video Editing Tool Clipify
- AI Article Publishing Tool StarBlog Publisher
- Icon Generation Tool SharpIco
- Image Processing Tool
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

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.

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

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

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.

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

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 许可协议。转载请注明出处!
相关文章
C#使用iKvm黑科技无缝接入JVM生态
C#开发者用iKVM白嫖JVM生态?省事,但依赖处理还得手动折腾。无缝接入?不过是把JAR转DLL,省了移植代码的麻烦。AI工具缺?直接调用HanLP,但依赖处理,呵呵。
2023-10-06 · C# · 约 1 分钟
Spark.NET:一个试图把 Django / Rails 式开发体验带回 .NET 世界的全栈 Web 框架。
又一个把Django/Rails那套“约定优于配置”搬进.NET生态的尝试,看着有点意思。预置了认证、队列、定时任务这些破事,省得自己天天重复造轮子。但767个star连个正式release都没有,就敢打着“生产级”旗号?呵,C#全栈爱好者们可以先玩玩,但别指望它能替代ASP.NET Core的体量。
2026-04-29 · C# · 约 1 分钟
花了不少时间,修复了一个SharpIco生成图标的bug
SharpIco bug修好了?真把自己当回事了,高分辨率图片转ico就崩,这逻辑够清奇。原来是位深度闹的,24转32而已,用点真本事解决?
2025-07-01 · C# · 约 1 分钟
纯 C#实现+AOT 打造的多功能图片处理工具 ImageGlider
纯C#+AOT打造的ImageGlider,图像处理功能堆得像山,还非得标榜零依赖?ImageSharp封装得倒是挺顺溜,就是不知道这模块化架构能走多远。Web API和CLI工具有点东西,但别指望我夸太离谱。
2025-08-05 · C# · 约 1 分钟
讨论
共 0 条加载评论中…