$30 off During Our Annual Pro Sale. View Details »

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

Kazuki Higashiguchi

August 31, 2020
Tweet

More Decks by Kazuki Higashiguchi

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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)

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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.

    View Slide

  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.

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  27. Congrats Go 1.15 !!

    View Slide