Go to Learn Go
0-using-doc
godoc -http :8000
- Packages : http://localhost:8000/pkg
- Tests examples : http://localhost:8000/pkg/testing/
1-hello-world
Writing a test is just like writing a function, with a few rules
- It needs to be in a file with a name like
xxx_test.go(your code file isxxx.go) - The test function must start with the word
Test - The test function takes one argument only
t *testing.T
Examples
hello.go
func Hello() string {
return "Hello, world"
}
hello_test.go
func TestHello(t *testing.T) {
got := Hello()
want := "Hello, world"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
In Go, we can declare function inside function. Doing that, we should use T.Helper().
Read more →
go