onsi / ginkgo

A Modern Testing Framework for Go
http://onsi.github.io/ginkgo/
MIT License
8.24k stars 650 forks source link

ginkgo v2 ignores flags when using TestMain #1233

Closed johnnycxm closed 1 year ago

johnnycxm commented 1 year ago

I have a project like this: test\ e2e.go e2e_test.go case1.go case2.go .... when I trying to run tests by cli: ginkgo --label-filter=XXX --focus=XXX. Any flags I passing after ginkgo command not working, it seems just ignored. Almost every flags of ginkgo cli I'v tried, even -vv is not working here is code in e2e_test.go:

package test
import (
    "flag"
    "os"
    "testing"
)

func TestMain(m *testing.M) {
    flag.StringVar(&MyFlag, "myflag", "default",
        "myflag arg")
        flag.Parse()
    os.Exit(m.Run())
}

func TestE2E(t *testing.T) {
    RunE2ETests(t)
}

and code in e2e.go

package test

import (
    "testing"

    "github.com/onsi/ginkgo/v2"
    "github.com/onsi/gomega"
)

func RunE2ETests(t *testing.T) {
    gomega.RegisterFailHandler(ginkgo.Fail)
    ginkgo.RunSpecs(t, "e2e suite")
}
onsi commented 1 year ago

hey there - first I'd encourage you to put all the test fils in the test package by giving them a name that ends in _test.go - what you're doing works but it can get a bit confusing.

As for flags - please follow the pattern established here:

https://onsi.github.io/ginkgo/#supporting-custom-configuration-custom-command-line-flags

I don't generally use TestMain and the pattern outlined above is the tested and supported pattern for passing custom flags into a Ginkgo suite.

johnnycxm commented 1 year ago

Thanks for your reply. I'v tied this pattern, put all custom command line flags parsing into a init() func, I changed e2e_test.go to this:

package test

import (
    "testing"

    "github.com/onsi/ginkgo/v2"
    "github.com/onsi/gomega"
)
func init() {
        flag.StringVar(&MyFlag, "myflag", "default",
        "myflag arg")
        flag.Parse()
}

func RunE2ETests(t *testing.T) {
    gomega.RegisterFailHandler(ginkgo.Fail)
    ginkgo.RunSpecs(t, "e2e suite")
}

but when I start test using command "ginkgo --focus Casexxx -- --myflag flag", I got this error: flag provided but not defined: -test.timeout This problem seems to be the same as the one described in issue #602

onsi commented 1 year ago

hey - try it without calling flag.Parse

johnnycxm commented 1 year ago

@onsi Thank you, this works for me!