公告

本次更新:

公告样式,参考自 VuePress-Reco

本博客只技术分享!


Skip to content
本站总访问量
本站访客数 人次
标签
javascriptfacetestgopythonHTML前端CSSvitepress

vscode 适配 go语言

1、要求

编写测试和函数很类似,其中有一些规则

  • 程序需要在一个名为 xxx_test.go 的文件中编写
  • 测试函数的命名必须以单词 Test 开始
  • 测试函数只接受一个参数 t *testing.T

2、例子

```go
package iteration

import "testing"

func TestRepeat(t *testing.T) {
    repeated := Repeat("a")
    expected := "aaaaa"

    if repeated != expected {
        t.Errorf("expected '%q' but got '%q'", expected, repeated)
    }
}

func TestRepeat1(b *testing.B) {
    b.Run("", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            Repeat("a")
        }
    })
}
```

3、覆盖率查看

  • 到测试文件目录下 执行 go test -cover
本站访客数 人次 本站总访问量