首頁 > 後端開發 > Golang > 主體

如何在 Golang 單元測試中使用覆蓋工具?

WBOY
發布: 2024-06-02 15:55:01
原創
865 人瀏覽過

在 Go 單元測試中使用 go test -cover 指令測量覆蓋率,並指定 -cover 或 -coverprofile 選項以產生結果;-covermode 選項可設定覆蓋模式(set、count 或 atomic)。實戰案例示範如何編寫測試,並使用覆蓋率設定檔和 go tool cover 指令產生覆蓋率報告。

如何在 Golang 单元测试中使用覆盖工具?

如何使用覆寫工具在Golang 單元測試中測試覆蓋率

覆寫工具在單元測試中非常重要,因為它可以幫助您識別程式碼中未被測試的部分。這對於確保程式碼的品質和可靠性至關重要。在 Golang 中,可以使用 go test -cover 指令來測量單元測試中的覆蓋率。

安裝覆蓋工具

要在 Golang 中使用覆蓋工具,您需要安裝它。您可以使用以下命令進行安裝:

go install golang.org/x/tools/cmd/cover
登入後複製

測量覆蓋率

#要測量單元測試的覆蓋率,請使用go test 命令並指定-cover 標誌。此標誌隨後可以接受以下值:

  • -cover:顯示覆寫結果,包括未覆寫的行、檔案和套件。
  • -coverprofile=profile.out:將覆寫結果寫入指定的檔案中。這對於進一步分析或與其他工具一起使用很有用。
  • -covermode=mode:設定覆蓋模式。可接受的值包括:

    • set:使用 set 模式報告覆寫情況。它將為每個被覆蓋的程式碼行分配一個計數器,並在測試完成後產生報告。
    • count:使用 count 模式報告覆寫情況。它將為每個被覆蓋的程式碼行分配一個 boolean 值,並在測試完成後產生報告。
    • atomic:使用原子計數模式報告覆蓋情況。它將為每個被覆蓋的程式碼行分配一個 int64 計數器,並在測試完成後產生報告。

實戰案例

以下是一個示範如何測量Golang 單元測試中覆蓋率的簡要範例:

main.go

package main

import (
    "fmt"
    "strconv"
)

// Add two numbers
func Add(a, b int) int {
    return a + b
}

// Convert a string to a number
func StrToInt(s string) int {
    n, err := strconv.Atoi(s)
    if err != nil {
        fmt.Println(err.Error())
        return 0
    }
    return n
}
登入後複製

main_test.go

#
package main

import (
    "testing"
)

func TestAdd(t *testing.T) {
    tests := []struct {
        a int
        b int
        want int
    }{
        {1, 2, 3},
        {0, 0, 0},
        {-1, -1, -2},
    }

    for _, tt := range tests {
        t.Run(fmt.Sprintf("TestAdd%d_%d", tt.a, tt.b), func(t *testing.T) {
            if got := Add(tt.a, tt.b); got != tt.want {
                t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.want)
            }
        })
    }
}

func TestStrToInt(t *testing.T) {
    tests := []struct {
        s string
        want int
    }{
        {"1", 1},
        {"0", 0},
        {"-1", -1},
    }

    for _, tt := range tests {
        t.Run(fmt.Sprintf("TestStrToInt%s", tt.s), func(t *testing.T) {
            if got := StrToInt(tt.s); got != tt.want {
                t.Errorf("StrToInt(%s) = %d, want %d", tt.s, got, tt.want)
            }
        })
    }
}

func TestCoverage(t *testing.T) {
    t.Run("Add", func(t *testing.T) {
        coverProfile := "add_coverage.out"
        args := []string{"-test.coverprofile=" + coverProfile, "-test.covermode=set"}
        cmd := exec.Command("go", "test", args...)
        if err := cmd.Run(); err != nil {
            t.Fatalf("Could not run coverage test: %v", err)
        }
    })

    t.Run("StrToInt", func(t *testing.T) {
        coverProfile := "str_int_coverage.out"
        args := []string{"-test.coverprofile=" + coverProfile, "-test.covermode=set"}
        cmd := exec.Command("go", "test", args...)
        if err := cmd.Run(); err != nil {
            t.Fatalf("Could not run coverage test: %v", err)
        }
    })
}
登入後複製

在命令列中執行以下命令以產生覆蓋率報告:

go test -test.coverprofile=coverage.out
登入後複製

這將在coverage.out 檔案中建立一個覆蓋率報告。您可以使用 go tool cover 命令查看報告:

go tool cover -html=coverage.out
登入後複製

這將在瀏覽器中開啟一個 HTML 報告,顯示未覆蓋的程式碼行和檔案。

以上是如何在 Golang 單元測試中使用覆蓋工具?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!