.NET Github Actions 测试覆盖率

打印 上一主题 下一主题

主题 1015|帖子 1015|积分 3045

如果熟悉 GIthub 我们经常可以在一些开源项目的 PR 上看到会配置测试的验证以及覆盖率的报告,并且可以强制覆盖率不低于设定的值才可以进行 Merge PR。

1.测试

创建一个 xUnit 单元测试项目。
Class
  1. /// <summary>
  2. /// Represents a class with methods to perform addition and subtraction operations.
  3. /// </summary>
  4. public class MyClass
  5. {
  6.     /// <summary>
  7.     /// Adds two integers and returns the result.
  8.     /// </summary>
  9.     /// <param name="a">The first integer to add.</param>
  10.     /// <param name="b">The second integer to add.</param>
  11.     /// <returns>The sum of the two integers.</returns>
  12.     public int Add(int a, int b)
  13.     {
  14.         return a + b;
  15.     }
  16.     /// <summary>
  17.     /// Subtracts one integer from another and returns the result.
  18.     /// </summary>
  19.     /// <param name="a">The integer to subtract from (the minuend).</param>
  20.     /// <param name="b">The integer to subtract (the subtrahend).</param>
  21.     /// <returns>The difference between the two integers.</returns>
  22.     public int Subtract(int a, int b)
  23.     {
  24.         return a - b;
  25.     }
  26. }
复制代码
Tests:
  1. public class MyClassTests
  2. {
  3.     [Fact]
  4.     public void TestAdd()
  5.     {
  6.         // Arrange
  7.         MyClass myClass = new MyClass();
  8.         // Act
  9.         int result = myClass.Add(2, 3);
  10.         // Assert
  11.         Assert.Equal(5, result);
  12.     }
  13.     [Fact]
  14.     public void TestSubtract()
  15.     {
  16.         // Arrange
  17.         MyClass myClass = new MyClass();
  18.         // Act
  19.         int result = myClass.Subtract(3, 2);
  20.         // Assert
  21.         Assert.Equal(1, result);
  22.     }
  23. }
复制代码
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:
  1. - name: Test
  2.   run: dotnet test  /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
  3. - name: Upload coverage reports to Codecov
  4.   uses: codecov/codecov-action@v3
  5.   with:
  6.     token: ${{ secrets.CODECOV_TOKEN }}
  7.     fail_ci_if_error: true
  8.     paths: ./**/coverage.opencover.xml
复制代码
配置 Status check

在项目根目录添加 codecov.yml
  1. coverage:
  2.   # Commit status https://docs.codecov.io/docs/commit-status are used
  3.   # to block PR based on coverage threshold.
  4.   status:
  5.     project:
  6.       default:
  7.         target: auto
  8.         threshold: 0%
  9.     patch:
  10.       default:
  11.         informational: true
复制代码
该配置要求 PR 的测试覆盖率减少Branches

通过这个设置,可以限制 main 分支不允许直接 commit,必须经过多少人 Review 才能 Merge,必须通过指定的 Actions 后才能 Merge 等等。可以用来配合覆盖率检测,提升项目的质量管控。
4.总结

在本文中,我们介绍了如何使用 Github Actions 和 Codecov 这两个工具来进行 .NET 项目的质量管控。通过在代码仓库中添加 Codecov 的 Action,我们可以自动化地收集测试覆盖率和代码质量等关键指标,并将其报告到 Codecov 的平台上,以便于团队更好地跟踪和管理项目的质量状况。
当然,Github Actions 和 Codecov 只是质量管控的一部分,要想确保项目的质量,还需要结合其他的质量控制措施,例如代码审查、单元测试、自动化测试等等。只有通过多个层面的质量控制,才能保证项目的可维护性和稳定性。
以上总结 by ChatGPT
示例:https://github.com/stulzq/DotNetActionsExample

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

飞不高

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表