Dive deep into Go 1.15 changes to testing package

Dive deep into Go 1.15 changes to testing package

The presentation materials for Go 1.15 Release Party in Japan

88964b936e864ca7d326272eaa70fa9a?s=128

Kazuki Higashiguchi

August 31, 2020
Tweet

Transcript

  1. Dive deep into Go 1.15 changes to testing package Go

    1.15 Release Party, AUGUST 31 2020 #go115party @hgsgtk Kazuki Higashiguchi
  2. { About me Kazuki Higashiguchi GitHub/Twitter: @hgsgtk BASE BANK, Inc.

    (BASE, Inc.)
  3. 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
  4. 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
  5. 01 A TestMain function is no longer required to call

    os.Exit Minor changes to the testing in 1.15
  6. 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
  7. 1.15 or later, no longer required to call os.Exit No

    need to call os.Exit()
  8. https://github.com/golang/go/issues/34129 testing: TestMain should not require os.Exit #34129 Issued at

    2019.9.26
  9. 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()
  10. 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)
  11. 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
  12. `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
  13. `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
  14. 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.
  15. 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.
  16. 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 関 数のテンプレート修正によって実現されている
  17. 02 Minor changes to the testing in 1.15 The testing.T

    type now has a Deadline method
  18. 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
  19. 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
  20. https://github.com/golang/go/issues/28135 testing: add (*T).Deadline #28135 Issued at Oct 11, 2018

  21. 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
  22. Inside src/testing/testing.go ※ 今回の変更に関連するものだけ抜粋しています testing.M.Run() runTests() testing.M.startAlarm() testing.T.Run() testing.T.Deadline() …..

  23. 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
  24. 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()
  25. src/testing/testing.go https://go-review.googlesource.com/c/go/+/202758/8/src/testing/testing.go runTests() set deadline to testContext.deadline

  26. 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
  27. Congrats Go 1.15 !!