|
|
Subscribe / Log in / New account

Testing in Go: philosophy and tools

Testing in Go: philosophy and tools

Posted May 26, 2020 22:55 UTC (Tue) by phlogistonjohn (subscriber, #81085)
Parent article: Testing in Go: philosophy and tools

One pet peeve I have with the testing regime in Go is that the tests require '*testing.T' which is a concrete type rather than an interface. I think it's totally fine that the built-in testing tool wants to be simple. However, by building the test framework on '*testing.T' its more strongly coupled to the testing package than it needs to be. If an interface had been used instead, other frameworks could wrap tests that are written to work with the testing package by default and do some pretty interesting things (IMO, of course). It's also a slight failure of this part of the Go standard library to practice what Go often preaches in regards to interfaces.

On a different note, I didn't quite see the rationale for Cleanup, given that Go has defer. The issue that ended up getting the feature added seems to be the clearest explanation of the why behind Cleanup: https://github.com/golang/go/issues/32111
Personally, I don't see it as all that compelling but not bad or anything. :-)


to post comments

Testing in Go: philosophy and tools

Posted May 27, 2020 6:03 UTC (Wed) by kokkoro (guest, #139153) [Link]

Defer doesn't work properly with parallel tests. If you do:

func TestFoo(t *testing.T) {
  f := NewFoo()
  defer f.Close()
  for _, c := testCases {
    c := c
    t.Run(c.name, func(t *testing.T) {
      t.Parallel()
      c.func(t, f)
    })
  }
then f.Close() will be called immediately, concurrent to all of the parallel subtests. https://github.com/golang/go/issues/17791


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds