Package testing
Table of Contents
Basics of testing
tutorial
_test.go
files are considered as tests.
import "testing"
func TextXxx(t *testing.T) {
t.Errorf("Failed! %d", 10)
}
func BenchmarkXxx(b *testing.B)
func ExampleXxx()
setup and teardown
How test files are treated within a package dicussion
_test.go
files are not part of the package ordinarily built bygo build
but are a part of it when built bygo test
.- You can use a different package name in
_test.go
files. - For example, in a file
foo/abc_test.go
, you can declare a name likepackage foo_test
, notpackage foo
, so the file belong to a separated packagefoo_test
.
You exploit this characteristic to expose internal functions for testing. For example:
export_test.gopackage foo
// `doSomething` is defined in some other normal file and it is intended to use internally.
// However, if you want to use this internal function in an external package for testing,
// You can expose it only for testing as follows.
// Since the line is declared in `_test.go` file, it is exposed only during testing.
var DoSomething = doSomething
- http://www.gopl.io/ (11.2)