Skip to content

testing: clarify that flag.Parse is called before any tests run #38952

Closed

Description

@mvdan

Suppose I'm a new Go developer, and I want to write a test that uses a flag. I know I have to call flag.Parse in a normal func main program to use flags, so by default, I assume that the same applies to tests.

If I look at the testing docs, here is the only mention of flag parsing I can find:

When TestMain is called, flag.Parse has not been run. If TestMain depends on command-line flags, including those of the testing package, it should call flag.Parse explicitly.

So it's clear that, if I want my TestMain to use flags, I need to parse them there. That's fine.

However, what if I don't want or need a TestMain? It's not clear at what point the flag parsing happens, or if it happens at all before the tests and benchmarks run.

In practice, flag parsing happens in testing.M.Run if it hasn't happened already. I think the docs should clarify that.

CC @mpvl as per https://dev.golang.org/owners

Activity

  1. added
    DocumentationIssues describing a change to documentation.
    on May 8, 2020
  2. gopherbot commented on May 8, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/232806 mentions this issue: testing: tests and benchmarks can assume flag.Parsed

  3. locked and limited conversation to collaborators on May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions