gotestingbenchmarkingchannel

panic: testing: Verbose called before Init case in Go


Trying to run https://github.com/adonovan/gopl.io/blob/master/ch8/cake/cake_test.go

but got

panic: testing: Verbose called before Init

goroutine 1 [running]:
testing.Verbose(...)
  /usr/lib/go-1.17/src/testing/testing.go:453
.../cake_test.init()

It says the error comes from cake_test.init(), yet the cake_test.go file doesn't contain init():

$ grep init cake_test.go | wc
      0       0       0

What exactly is the problem?


Solution

  • For this specific case, adding Init() function did the trick.

    var defaults cake.Shop
    
    func Init() {
        defaults = cake.Shop{
            Verbose:      testing.Verbose(),
            Cakes:        20,
            BakeTime:     10 * time.Millisecond,
            NumIcers:     1,
            IceTime:      10 * time.Millisecond,
            InscribeTime: 10 * time.Millisecond,
        }
    }