Skip to content

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

Closed

Description

@umisama

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.

Activity

  1. added this to the Go1.5 milestone on Feb 10, 2015
  2. minux commented on Feb 10, 2015

    @minux
    Member

    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.

  3. changed the title [-]testing: Short() not works on TestMain()[/-] [+]testing: Short() does not work in TestMain()[/+] on Feb 10, 2015
  4. hraban commented on Mar 9, 2015

    @hraban

    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.

  5. gravis commented on Mar 16, 2015

    @gravis

    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 :(

  6. minux commented on Mar 16, 2015

    @minux
    Member

    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.

  7. gravis commented on Mar 16, 2015

    @gravis

    Ok, thanks for the info

  8. minux commented on Mar 16, 2015

    @minux
    Member
  9. gravis commented on Mar 16, 2015

    @gravis

    👍 👍

  10. locked and limited conversation to collaborators on Jun 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions