Skip to content

testing: nested t.Fatalf hides errors #22882

@scubed2

Description

@scubed2

What version of Go are you using (go version)?

go version
go version go1.8 linux/amd64

Does this issue reproduce with the latest release?

(Untested)

What operating system and processor architecture are you using (go env)?

go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/scubed/go"
GORACE=""
GOROOT="/nix/store/5428ij3ln77vam6cacnljc1f75f214fs-go-1.8/share/go"
GOTOOLDIR="/nix/store/5428ij3ln77vam6cacnljc1f75f214fs-go-1.8/share/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build180482905=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

I'm not sure how to put a test in the playground.
If someone could point out how to do that, I will.

nested.go
-------------
package nested
-------------
nested_test.go
-------------
package nested

import (
"testing"
)

func TestNested(t *testing.T) {
outer := t
t.Run("sub", func(t *testing.T) {
outer.Fatalf("Nope")
})
}
-------------
Note that renaming t to outer is just for emphasis.

What did you expect to see?

While it's not immediately obvious to me what should happen in this case, it should at least:

  1. Print out the string passed to Fatalf (in this case, "Nope")
  2. If this is invalid, say that it is calling Fatalf from the wrong scope, so people know what the actual problem is.

This came up when debugging a test that was using gomock incorrectly. I ended up spending time tracking down this instead of fixing the test.

What did you see instead?

It gives a not very helpful stacktrace.

go test nested_test.go
panic: test executed panic(nil) or runtime.Goexit

goroutine 6 [running]:
testing.tRunner.func1(0xc420076750)
/nix/store/5428ij3ln77vam6cacnljc1f75f214fs-go-1.8/share/go/src/testing/testing.go:622 +0x29d
runtime.Goexit()
/nix/store/5428ij3ln77vam6cacnljc1f75f214fs-go-1.8/share/go/src/runtime/panic.go:366 +0x153
testing.(*common).FailNow(0xc4200765b0)
/nix/store/5428ij3ln77vam6cacnljc1f75f214fs-go-1.8/share/go/src/testing/testing.go:486 +0x39
testing.(*common).Fatalf(0xc4200765b0, 0x5393ee, 0x4, 0x0, 0x0, 0x0)
/nix/store/5428ij3ln77vam6cacnljc1f75f214fs-go-1.8/share/go/src/testing/testing.go:530 +0x83
command-line-arguments.TestNested.func1(0xc420076750)
/dev/shm/a/nested_test.go:10 +0x5c
testing.tRunner(0xc420076750, 0xc42000eea0)
/nix/store/5428ij3ln77vam6cacnljc1f75f214fs-go-1.8/share/go/src/testing/testing.go:657 +0x96
created by testing.(*T).Run
/nix/store/5428ij3ln77vam6cacnljc1f75f214fs-go-1.8/share/go/src/testing/testing.go:697 +0x2ca
FAIL command-line-arguments 0.004s

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions