单元测试

Golang Unit Testing - GoLang Docs https://golangdocs.com/golang-unit-testing

Golang Testing单元测试指南 - sunsky303 - 博客园 https://www.cnblogs.com/sunsky303/p/11818480.html

Go语言重新开始,Go Modules的前世今生与基本使用 https://mp.weixin.qq.com/s/LIIyUQemHPiQkMwTMFsXKw

首先创建一个新目录/home/gopher/hello,然后进入到这个目录中,接着创建一个新文件,hello.go:

package hellofunc Hello() string {    return "Hello, world."}

然后再写个对应的测试文件hello_test.go:

package helloimport "testing"func TestHello(t *testing.T) {    want := "Hello, world."    if got := Hello(); got != want {        t.Errorf("Hello() = %q, want %q", got, want)    }}

现在我们拥有了一个package,但它还不是一个Module,因为还没有创建go.mod文件。如果在/home/gopher/hello目录中执行go test,则可以看到:$go test

go:go.mod file not found in current directory or any parent directory;see'go help modules'

可以看到Go命令行提示没有找到go.mod文件,可以参考Go help Modules。这样的话可以使用Go mod init来初始化一下,然后再执行Go test:

go mod init example.com/hellogo: creating new go.mod: module example.com/hellogo: to add module requirements and sums:go mod tidy$go mod tidygo go testPASSok example.com/hello  0.020s$

这样的话,Module测试就完成了。然后执行的go mod init命令创建了一个go.mod文件:

$ cat go.modmodule example.com/hellogo 1.17
原文地址:https://www.cnblogs.com/rsapaper/p/15571894.html