Skip to content

testing: T.FailNow() in cleanup function causes test report to not be printed #76447

@adombeck

Description

@adombeck

Go version

go version go1.25.3 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/user/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build148784901=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/tmp/test/go.mod'
GOMODCACHE='/home/user/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/user/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.25.3.linux-amd64'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/user/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/user/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.25.3.linux-amd64/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Calling T.FailNow() in a function that is deferred via T.Cleanup causes the test report to not be printed if there are parallel subtests and the tests are run without -test.v.

Minimal reproducer:

package main

import (
    "testing"
)

func Test(t *testing.T) {
    t.Parallel()
    t.Cleanup(func() { t.Fatal("This error is never printed") })
    t.Run("subtest", func(t *testing.T) { t.Parallel() })
}

If a subtest fails, those test failures are also not reported:

package main

import (
	"testing"
)

func Test(t *testing.T) {
	t.Parallel()
	t.Cleanup(func() { t.Fatal("This error is never printed") })
	t.Run("subtest", func(t *testing.T) {
		t.Parallel()
		t.Error("This error is also never printed")
	})
}

What did you see happen?

The test fails without any error message:

$ go test ./...
FAIL
FAIL	example.com/m	0.003s
FAIL

What did you expect to see?

I expected the test to print the test report, as it does when there are no parallel subtests:

--- FAIL: Test (0.00s)
    main_test.go:11: 
        	Error Trace:	/tmp/test/main_test.go:11
        	            				/home/user/go/src/testing/testing.go:1308
        	            				/home/user/go/src/testing/testing.go:1572
        	            				/home/user/go/src/testing/testing.go:1912
        	Error:      	This error is never printed
        	Test:       	Test
FAIL
FAIL	example.com/m	0.003s
FAIL

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone 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