Skip to content

TestMain prevents other tests from running #74699

@pascal-blokur

Description

@pascal-blokur

Go version

go version go1.24.4 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='/home/pascal/go/bin'
GOCACHE='/home/pascal/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/pascal/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1468475678=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/pascal/go/src/github.com/pascal/awesomeProject/go.mod'
GOMODCACHE='/home/pascal/go/pkg/mod'
GONOPROXY='github.com/blokur/*'
GONOSUMDB='github.com/blokur/*'
GOOS='linux'
GOPATH='/home/pascal/go'
GOPRIVATE='github.com/blokur/*'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/pascal/go/installs/latest/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/pascal/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/pascal/go/installs/latest/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Suppose I have these two files:

main.go:

package main

func main() { 
}

main_test.go:

package main

import (
	"testing"
)

func TestMain(m *testing.M) {
	println("main")
}

func TestFatal(t *testing.T) {
	println("fatal")
	t.Fatal("foo")
}

func TestHappy(t *testing.T) {
	println("happy")
}

If I test the package, I get ok (which is strange, as my TestFatal should fail)

> go test -v 
main
ok  	github.com/pascal/awesomeProject	0.002s

However, if I remove the TestMain function from the file, and then run it again:

> go test -v
=== RUN   TestFatal
fatal
    main_test.go:9: foo
--- FAIL: TestFatal (0.00s)
=== RUN   TestHappy
happy
--- PASS: TestHappy (0.00s)
FAIL
exit status 1
FAIL    github.com/pascal/awesomeProject        0.002s

What did you see happen?

Running the test with the TestMain function seems to have prevented the TestFatal from running

What did you expect to see?

I expected the TestFatal to cause go test -v to fail when TestMain and TestFatal were present in the file.
(I also kind of expected the -v to display TestMain)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions