.NET Github Actions 測試覆蓋率

如果熟悉 GIthub 我們經常可以在一些開源項目的 PR 上看到會配置測試的驗證以及覆蓋率的報告,並且可以強制覆蓋率不低於設定的值纔可以進行 Merge PR。

image-20230307010420028

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 賬號登錄,授權後它會自動獲取你賬號/組織下的倉庫。

image-20230307010650280

2.2 設置

找到需要設置的倉庫,點擊 setup repo,便會出現對應的配置教程。

image-20230307010815288

image-20230307010931161

設置 Token

爲了安全,我們不能在 yaml 直接配置我們的 token,需要在 Github 倉庫的 Secrets 設置。

image-20230307011157563

配置 codecov

點擊第二步的鏈接,配置 codecov app

image-20230307011418317

重新配置可以在 Installed GitHub Apps 找到

image-20230307011524934

配置 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,不然就會提示錯誤:

image-20230308201618333

image-20230308201634831

更多設置可以查看官方文檔:Status Checks (codecov.com)

關於 Patch

在上面的圖中可以看到有個 patch,他可以顯示出我們新增或者修改的代碼,那些沒有被測試覆蓋。

image-20230308201928117

新增了兩個方法,並沒有編寫對應的測試,就會被檢測到並且提示出來。

3.分支保護

Github 提供了分支保護規則的設置:Settings->Branches

image-20230308202104636

通過這個設置,可以限制 main 分支不允許直接 commit,必須經過多少人 Review 才能 Merge,必須通過指定的 Actions 後才能 Merge 等等。可以用來配合覆蓋率檢測,提升項目的質量管控。

4.總結

在本文中,我們介紹瞭如何使用 Github Actions 和 Codecov 這兩個工具來進行 .NET 項目的質量管控。通過在代碼倉庫中添加 Codecov 的 Action,我們可以自動化地收集測試覆蓋率和代碼質量等關鍵指標,並將其報告到 Codecov 的平臺上,以便於團隊更好地跟蹤和管理項目的質量狀況。

當然,Github Actions 和 Codecov 只是質量管控的一部分,要想確保項目的質量,還需要結合其他的質量控制措施,例如代碼審查、單元測試、自動化測試等等。只有通過多個層面的質量控制,才能保證項目的可維護性和穩定性。

以上總結 by ChatGPT

示例:https://github.com/stulzq/DotNetActionsExample

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章