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 许可协议。转载请注明出处!
相关文章
开发现代化的.NetCore控制台程序:(3)将nuget包发布到GitHubPackages
发布Nuget包到GitHub Packages?不过是配置token和nuget.config的重复劳动。手动关联包与仓库?GitHub的设计真够奇葩的。搞定,但谁有空折腾这些。
2023-11-10 · C# · 约 1 分钟
纯 C#实现+AOT 打造的智能PDF目录提取工具 PdfTocExtractor
用C#写个PDF目录提取工具?挺有意思,还整什么语义识别,效果真有那么好吗?AOT编译挺香,但别搞得太复杂就行。
2025-08-20 · C# · 约 1 分钟
C#爬虫:CatSpider#-开发笔记
前言 (PS:我这里用了#号代替了Sharp这个单词) CatSpider是毕设里的数据采集模块,本来爬虫类的应用肯定使用python来开发嘛,不过用request_html做解析的时候,python的动态类型真的把我恶心到了,而且感觉这个库也不是很成熟,html5lib也不好用,也没心思去深入了,之前看到有大佬用.net core平台做爬虫,于是我也来试试,没想到效果贼好,特别是配合LinqPa
2022-05-27 · C# · 约 1 分钟
使用C#和MemoryCache组件实现轮流调用APIKey以提高并发能力
本文介绍了如何利用C#语言中的MemoryCache组件,结合并发编程技巧,实现轮流调用多个API Key以提高系统的并发能力。通过示例代码和详细说明,读者将了解如何有效地管理API Key的调用次数限制,并优化系统性能。
2024-03-09 · C# · 约 1 分钟
讨论
共 0 条加载评论中…