This is a creation in Article, where the information may have evolved or changed.
Go Unit Test
- Go Unit Test
- Overview
- Instance
- Reference documents
Overview
Go provides a package for automated testing testing ,
Suppose we have a file youfile.go , then the name of the test file is created, yourfile_test.go and the file has a test function in the form of the following:
func TestXxx(*testing.T)
TestXxx Xxx The first letter in X it must be uppercase.
Place your source files yourfile.go in the yourfile_test.go same directory.
go testRun with Command
Instance
Here you use the Https://github.com/golang/example/tree/master/stringutil file test
There are two files in the Stringutil directory: reverse.go andreverse_test.go
Reverse.go
package stringutil// Reverse returns its argument string reversed rune-wise left to right.funcstringstring { r := []rune(s) for i, j := 0len(r)-1len(r)/2; i, j = i+1, j-1 { r[i], r[j] = r[j], r[i] } returnstring(r)}
Reverse_test.go
package stringutilimport"testing"func TestReverse(t *testing.T) { forrange []struct { string } { {"Hello, world""dlrow ,olleH"}, {"Hello, 世界""界世 ,olleH"}, {""""}, } { got := Reverse(c.in) if got != c.want { t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want) } }}
Run results
[stringnutil]# go testPASSok stringutil 0.002s
Reference documents
Pkg/testing