Share
Embed
Dive deep into Go 1.15 changes to testing package
The presentation materials for Go 1.15 Release Party in Japan
Kazuki Higashiguchi
August 31, 2020
Transcript
27 slides
-
Dive deep into Go 1.15 changes to testing package Go
1.15 Release Party, AUGUST 31 2020 #go115party @hgsgtk Kazuki Higashiguchi -
Go 1.15 Release Notes > Minor changes to the testing
The testing.T type now has a Deadline method ... A TestMain function is no longer required to call os.Exit. ... The new methods T.TempDir and B.TempDir ... go test -v now groups output by test name, ... https://golang.org/doc/go1.15#testing -
Go 1.15 Release Notes > Minor changes to the testing
The testing.T type now has a Deadline method ... A TestMain function is no longer required to call os.Exit. ... The new methods T.TempDir and B.TempDir ... go test -v now groups output by test name, ... https://golang.org/doc/go1.15#testing Talk contents -
01 A TestMain function is no longer required to call
os.Exit Minor changes to the testing in 1.15 -
Go 1.15 Release Notes > Minor changes to the library
> testing A TestMain function is no longer required to call os.Exit. If a TestMain function returns, the test binary will call os.Exit with the value returned by m.Run. https://golang.org/doc/go1.15#testing -
1.15 or later, no longer required to call os.Exit No
need to call os.Exit() -
Common TestMain() mistakes $ go test -v ./... === RUN
TestHoge --- FAIL: TestHoge (0.00s) FAIL ok github.com/hgsgtk/go-snippets/testing -115/mistake 0.079s defer os.Exit() -
Issue #34129 testing: TestMain should not require os.Exit https://github.com/golang/go/issues/34129 “This
mistake does happen a lot, and the testing package that called TestMain and prepared the m can certainly record the result of m.Run for use in its own outer os.Exit.” (@rsc Russ Cox) -
https://go-review.googlesource.com/c/go/+/219639 219639: testing: do not require os.Exit in TestMain Major
change files ... src/testing/testing.go src/cmd/go/internal/load/ test.go -
`go test ./...` with TestMain (before 1.15) `go test` generate
_testmain.go code. src/cmd/go/main.go main() src/cmd/go/internal/test/test.go runTest() -> builderTest() src/cmd/go/internal/load TestPackagesFor() -> TestPackagesAndErrors() -> formatTestMain() -> testmainTmpl Template code generated by `go test` ( _testmain.go) SUT _test.go TestMain 1. Call testing.MainStart() src/testing/testing.go MainStart() 2. Call user-defined TestMain -
`go test ./...` with TestMain (1.15 or later) Go 1.15
or later, main func in `_testmain.go` call os.Exit(). src/cmd/go/main.go main() src/cmd/go/internal/test/test.go runTest() -> builderTest() src/cmd/go/internal/load TestPackagesFor() -> TestPackagesAndErrors() -> formatTestMain() -> testmainTmpl Template code generated by `go test` ( _testmain.go) SUT _test.go TestMain 1. Call testing.MainStart() src/testing/testing.go MainStart() 2. Call user-defined TestMain 3. Call os.Exit() with m.Run result code -
A TestMain function is no longer required to call os.Exit
• A TestMain function is no longer required to call os.Exit • testing.M の内部変更及び、 go test で生成される main 関 数のテンプレート修正によって実現されている -
02 Minor changes to the testing in 1.15 The testing.T
type now has a Deadline method -
Go 1.15 Release Notes > Minor changes to the library
> testing The testing.T type now has a Deadline method that reports the time at which the test binary will have exceeded its timeout. https://golang.org/doc/go1.15#testing -
t.Deadline() https://godoc.org/testing#T.Deadline Deadline reports the time at which the test
binary will have exceeded the timeout specified by the -timeout flag. The ok result is false if the -timeout flag indicates “no timeout” (0). % go test -v -timeout=5s -run=TestDeadlineConfirm t.Deadline() = 2020-08-31 18:37:39.502862 +0900 JST m=+5.000488349, timeout set = true $ go test -v -timeout=0 -run=TestDeadlineConfirm t.Deadline() = 0001-01-01 00:00:00 +0000 UTC, timeout set = false -
https://go-review.googlesource.com/c/go/+/202758/ 202758: testing: testing: add (*T).Deadline method for test timeout
Major change files ... src/testing/testing.go -
The testing.T type now has a Deadline method • A
new method t.Deadline • It is realized by adding `deadline` field in testContext in testing.T