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: clarify that flag.Parse is called before any tests run #38952

Closed
mvdan opened this issue May 8, 2020 · 1 comment
Closed

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

mvdan opened this issue May 8, 2020 · 1 comment

Comments

@mvdan
Copy link
Member

mvdan commented May 8, 2020

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

@gopherbot
Copy link

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

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

2 participants