Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: Short() does not work in TestMain() #9825

Closed
umisama opened this issue Feb 10, 2015 · 7 comments
Closed

testing: Short() does not work in TestMain() #9825

umisama opened this issue Feb 10, 2015 · 7 comments
Milestone

Comments

@umisama
Copy link

umisama commented Feb 10, 2015

The testing.Short() have to report -test.short flag is set. But, it looks not working on TestMain(*testing.M).

You can see this behavior on this example.
https://gist.github.com/umisama/cebb1c8f9022e5402d21

Here is output on my environment.

$ go version
go version go1.4 linux/amd64
$ go test -short
false   on TestMain
true    on TestSomething
PASS
ok      _/home/umisama/testingshort  0.008s

In the example I expected to testing.Short() returns true for TestXxx() including TestMain(*testing.M), but it returns false on TestMain(*testing.M).

I'll create patch if this behavior is unexpected.

@minux minux added this to the Go1.5 milestone Feb 10, 2015
@minux
Copy link
Member

minux commented Feb 10, 2015

TestMain is executed before flag.Parse in (*testing.M).Run.

I think this is intentional, because it provides a way to modify the
command line arguments. I you want testing.Short(), then you
should call flag.Parse() manually in TestMain.

In conclusion, I think a docs clarification is needed, it's not a bug.

@minux minux changed the title testing: Short() not works on TestMain() testing: Short() does not work in TestMain() Feb 10, 2015
@hraban
Copy link

hraban commented Mar 9, 2015

Please include that testing.Verbose() also doesn't work in TestMain. Came here to post an issue about it, but now I see it's by design.

@gravis
Copy link

gravis commented Mar 16, 2015

I really would like to see changed as well.
I was planning to use testMain as:

func TestMain(m *testing.M) {
    // Reset test db before running test.
    // The schema is only imported once for all test
    if !testing.Short() {
        fmt.Println("Initializing test DB for integration test (disable with `go test -short`)")
        initTestDB()
    }
    os.Exit(m.Run())
}

And initTestDB() is always run :(

@minux
Copy link
Member

minux commented Mar 16, 2015

I don't think this is going to change.

If you want to use testing.Verbose or testing.Short, call flag.Parse in
TestMain before using them.

@gravis
Copy link

gravis commented Mar 16, 2015

Ok, thanks for the info

@minux
Copy link
Member

minux commented Mar 16, 2015 via email

@gravis
Copy link

gravis commented Mar 16, 2015

👍 👍

@minux minux closed this as completed in f63678a Mar 20, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants