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

Comments

Projects
None yet
5 participants
@umisama
Copy link

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

This comment has been minimized.

Copy link
Member

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 added the repo-main label Feb 10, 2015

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

@hraban

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link

commented Mar 16, 2015

Ok, thanks for the info

@minux

This comment has been minimized.

Copy link
Member

commented Mar 16, 2015

@gravis

This comment has been minimized.

Copy link

commented Mar 16, 2015

👍 👍

@minux minux closed this 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.
You can’t perform that action at this time.