-
Run
go mod init github.com/monkrus/go-best-test
-
Interesting testing-related packages :
testing
,testing/quick
,testing/iotest
andnet/http/httptest
-
Other projects :
github.com/stretchcom/testif (assertions),
github.com/onsi/ginkgo (BDD),
goconvey.com (visual results in the browser),
github.com/gavv/httpexpect (end-to-end testing, e.g. RestAPI),
code.google.com/p/gomock (injecting mocks into your tests),
github.com/DATA-DOG/go-sqlmock (DB testing)
- Add
_test
to filenames - Prefix tests with
Test
- Accept one parameter
*testing.T
- Remove
_test suffix
for whitebox tests
-
Receive
*testing.T
object -
No special assertions API, use normal Go code
-
Report errors using methods on
*testing.T object
-
Immediate failure FailNow(), Fatal(args ..interface{}), Fatalf(format strings, args ..interface{})
-
Non-immediate failure Fail(), Error(args ...interface{}), Errorf(format string, args ..interface{})
-
go test
orgo test {pkg1}
-
go test./...
Will fund tests on current directory and subtree -
go test -v
Generates verbose output -
go test -run {regexp}
To concentrate on the spesific tests (matching) -
go help testflags
-
go test run {function name}
- to run specific function
-
go test -cover
-
Reports
go test -coverprofile cover.out
orgo tool cover -func cover.out
-
Graphical coverage
go test -coverprofile count.out -covermode count
and thengo tool cover -html cover.out
-
Log and logf-(write a message testing output)
-
Helper (for helper method)
-
Skip, Skipf, SkipNow
-
Run (create subtest)
-
Parallel