Slide 1

Slide 1 text

Dive deep into Go 1.15 changes to testing package Go 1.15 Release Party, AUGUST 31 2020 #go115party @hgsgtk Kazuki Higashiguchi

Slide 2

Slide 2 text

{ About me Kazuki Higashiguchi GitHub/Twitter: @hgsgtk BASE BANK, Inc. (BASE, Inc.)

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

01 A TestMain function is no longer required to call os.Exit Minor changes to the testing in 1.15

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

1.15 or later, no longer required to call os.Exit No need to call os.Exit()

Slide 8

Slide 8 text

https://github.com/golang/go/issues/34129 testing: TestMain should not require os.Exit #34129 Issued at 2019.9.26

Slide 9

Slide 9 text

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()

Slide 10

Slide 10 text

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)

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

`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

Slide 13

Slide 13 text

`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

Slide 14

Slide 14 text

src/testing/testing.go https://go-review.googlesource.com/c/go/+/219639/10/src/testing/testing.go m.Run records its own result in an unexported field of m.

Slide 15

Slide 15 text

src/cmd/go/internal/load/test.go https://go-review.googlesource.com/c/go/+/219639/10/src/cmd/go/internal/load/test.go if TestMain(m) returns, the outer test func main harness calls os.Exit with that code.

Slide 16

Slide 16 text

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 関 数のテンプレート修正によって実現されている

Slide 17

Slide 17 text

02 Minor changes to the testing in 1.15 The testing.T type now has a Deadline method

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

https://github.com/golang/go/issues/28135 testing: add (*T).Deadline #28135 Issued at Oct 11, 2018

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Inside src/testing/testing.go ※ 今回の変更に関連するものだけ抜粋しています testing.M.Run() runTests() testing.M.startAlarm() testing.T.Run() testing.T.Deadline() …..

Slide 23

Slide 23 text

src/testing/testing.go https://golang.org/src/testing/testing.go#L630 New field ‘deadline’ in testContext New method T.Deadline() testing.T has *testContext

Slide 24

Slide 24 text

src/testing/testing.go https://go-review.googlesource.com/c/go/+/202758/8/src/testing/testing.go t.Deadline() added in testing.go m.Run() call it. Pass arg to runTests()

Slide 25

Slide 25 text

src/testing/testing.go https://go-review.googlesource.com/c/go/+/202758/8/src/testing/testing.go runTests() set deadline to testContext.deadline

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Congrats Go 1.15 !!