-
Run
go mod init github.com/monkrus/go-best-test -
Interesting testing-related packages :
testing,testing/quick,testing/iotestandnet/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
_testto filenames - Prefix tests with
Test - Accept one parameter
*testing.T - Remove
_test suffixfor whitebox tests
-
Receive
*testing.Tobject -
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 testorgo test {pkg1} -
go test./...Will fund tests on current directory and subtree -
go test -vGenerates 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.outorgo tool cover -func cover.out -
Graphical coverage
go test -coverprofile count.out -covermode countand 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