golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.54k stars 17.41k forks source link

cmd/go: go test -list does things other than listing tests #25339

Open echlebek opened 6 years ago

echlebek commented 6 years ago

This is a small quibble, but caused me some surprise recently.

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

go version go1.10.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/eric/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/eric/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build753721336=/tmp/go-build -gno-record-gcc-switches"

What did you do?

foo_test.go

package foo

import (
    "fmt"
    "testing"
)

func init() {
    fmt.Println("Hello, world!")
}

func TestFoo(t *testing.T) {}
$ go test -list=TestFoo

What did you expect to see?

TestFoo

What did you see instead?

Hello, world!
TestFoo
ok      github.com/echlebek/foo 0.001s

In https://github.com/golang/go/issues/17209, Nemith proposes that go test -list would list the tests, but no tests would be run or any other operation.

I think that since it can be statically determined what the tests in a package are, the -list functionality should not depend on executing the test binary.

ianlancetaylor commented 6 years ago

Perhaps it was a mistake to add a -test.list option to test binaries. Now that we've done that, it seems clearly desirable for go test -list and go test -c; ./pkg.test -test.list do the same thing.

vedujoshi commented 6 years ago

Same issue was seen with below code as well...when asked to list, BeforeSuite() and AfterSuite() methods too get called

func TestMain(m *testing.M) {
    BeforeSuite()
    exitCode := m.Run()
    AfterSuite()
    os.Exit(exitCode)
}
fredrikaverpil commented 3 months ago

This problem has existed for quite some time: https://github.com/golang/go/issues/17209

But yes, it would be super useful to have tests listed (not executed) even if the subtests/table tests cannot be found.