Skip to content

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

@mvdan

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

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