.NET Github Actions 测试覆盖率
如果熟悉 GIthub 我们经常可以在一些开源项目的 PR 上看到会配置测试的验证以及覆盖率的报告,并且可以强制覆盖率不低于设定的值才可以进行 Merge PR。
1.测试
创建一个 xUnit 单元测试项目。
Class
/// <summary>
/// Represents a class with methods to perform addition and subtraction operations.
/// </summary>
public class MyClass
{
/// <summary>
/// Adds two integers and returns the result.
/// </summary>
/// <param name="a">The first integer to add.</param>
/// <param name="b">The second integer to add.</param>
/// <returns>The sum of the two integers.</returns>
public int Add(int a, int b)
{
return a + b;
}
/// <summary>
/// Subtracts one integer from another and returns the result.
/// </summary>
/// <param name="a">The integer to subtract from (the minuend).</param>
/// <param name="b">The integer to subtract (the subtrahend).</param>
/// <returns>The difference between the two integers.</returns>
public int Subtract(int a, int b)
{
return a - b;
}
}
Tests:
public class MyClassTests
{
[Fact]
public void TestAdd()
{
// Arrange
MyClass myClass = new MyClass();
// Act
int result = myClass.Add(2, 3);
// Assert
Assert.Equal(5, result);
}
[Fact]
public void TestSubtract()
{
// Arrange
MyClass myClass = new MyClass();
// Act
int result = myClass.Subtract(3, 2);
// Assert
Assert.Equal(1, result);
}
}
2.使用 Codecov
2.1 注册
直接访问 https://codecov.io ,使用 GIthub 账号登录,授权后它会自动获取你账号/组织下的仓库。
2.2 设置
找到需要设置的仓库,点击 setup repo
,便会出现对应的配置教程。
设置 Token
为了安全,我们不能在 yaml 直接配置我们的 token,需要在 Github 仓库的 Secrets 设置。
配置 codecov
点击第二步的链接,配置 codecov app
重新配置可以在 Installed GitHub Apps 找到
配置 workflow
添加 step:
- name: Test
run: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
paths: ./**/coverage.opencover.xml
配置 Status check
在项目根目录添加 codecov.yml
coverage:
# Commit status https://docs.codecov.io/docs/commit-status are used
# to block PR based on coverage threshold.
status:
project:
default:
target: auto
threshold: 0%
patch:
default:
informational: true
该配置要求 PR 的测试覆盖率减少<=0,不然就会提示错误:
更多设置可以查看官方文档:Status Checks (codecov.com)
关于 Patch
在上面的图中可以看到有个 patch,他可以显示出我们新增或者修改的代码,那些没有被测试覆盖。
新增了两个方法,并没有编写对应的测试,就会被检测到并且提示出来。
3.分支保护
Github 提供了分支保护规则的设置:Settings->Branches
通过这个设置,可以限制 main 分支不允许直接 commit,必须经过多少人 Review 才能 Merge,必须通过指定的 Actions 后才能 Merge 等等。可以用来配合覆盖率检测,提升项目的质量管控。
4.总结
在本文中,我们介绍了如何使用 Github Actions 和 Codecov 这两个工具来进行 .NET 项目的质量管控。通过在代码仓库中添加 Codecov 的 Action,我们可以自动化地收集测试覆盖率和代码质量等关键指标,并将其报告到 Codecov 的平台上,以便于团队更好地跟踪和管理项目的质量状况。
当然,Github Actions 和 Codecov 只是质量管控的一部分,要想确保项目的质量,还需要结合其他的质量控制措施,例如代码审查、单元测试、自动化测试等等。只有通过多个层面的质量控制,才能保证项目的可维护性和稳定性。
以上总结 by ChatGPT
相关文章
- .net题库第1-9章
- 用.NET开发的磁力搜索引擎——btbook.net「建议收藏」
- net 开源「建议收藏」
- 如何在ASP.NET MVC中获取客户端的IP地址?
- ASP NET MVC Web开发教程
- .NET WebAPI 实现 接口版本控制并打通 Swagger支持
- 又一个VB.NET制作的好看登录界面
- NLog在.NET Core Console Apps中的简单应用详解编程语言
- Linux.Net:开启新技术之旅(linux.net)
- 利用Oracle.Net实现数据库互联(oracle.net)
- .net让Oracle与ASP.NET实现无缝连接(oracleasp)
- 跨平台利器:.NET在Linux上的运行(.net运行在linux)
- Linux上安装.NET:提高开发效率,拓展技术栈(linux安装.net)
- 程序员级别鉴定书(.NET面试问答集锦)
- Net与Oracle构建连接的数据价值(.net 链oracle)
- NET与Oracle携手走向未来(.net 对oracle)
- Net环境下MySQL数据库驱动的研究与应用(.net的mysql驱动)
- Net中连接MySQL技术简述(.net中引用mysql)
- NET与MySQL实现无缝连接(.net mysql连接)
- ASP.NET数据库编程之处理文件访问许可
- asp.net下cookies的丢失和中文乱码
- ASP.NET保留文件夹详解
- asp.net得到本地电脑基本信息的简单方法
- asp.net扩展GridView增加单选按钮列的代码
- 导致Asp.Net站点重启10个原因小结分析
- 基于ASP.NET的lucene.net全文搜索实现步骤
- 保护.net中的dll文件方法(防止破解、反编译dll)
- ASP.NET连接MySql数据库的2个方法及示例