1
0
mirror of https://github.com/golang/go synced 2024-11-23 23:10:09 -07:00

testing: tests and benchmarks can assume flag.Parsed

testing.M.Run has this bit of code:

	if !flag.Parsed() {
		flag.Parse()
	}

It makes sense, and it's common knowledge for many Go developers that
test flags are automatically parsed by the time tests and benchmarks are
run. However, the docs didn't clarify that. The previous wording only
mentioned that flag.Parse isn't run before TestMain, which doesn't
necessarily mean that it's run afterwards.

Fixes #38952.

Change-Id: I85f7a9dce637a23c5cb9abc485d47415c1a1ca27
Reviewed-on: https://go-review.googlesource.com/c/go/+/232806
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Daniel Martí 2020-05-08 14:14:15 +01:00
parent 60368c2477
commit 7cfa7d6925

View File

@ -219,9 +219,12 @@
// directly. TestMain runs in the main goroutine and can do whatever setup // directly. TestMain runs in the main goroutine and can do whatever setup
// and teardown is necessary around a call to m.Run. m.Run will return an exit // and teardown is necessary around a call to m.Run. m.Run will return an exit
// code that may be passed to os.Exit. If TestMain returns, the test wrapper // code that may be passed to os.Exit. If TestMain returns, the test wrapper
// will pass the result of m.Run to os.Exit itself. When TestMain is called, // will pass the result of m.Run to os.Exit itself.
// 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. // 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. Command line flags are always parsed by the time test
// or benchmark functions run.
// //
// A simple implementation of TestMain is: // A simple implementation of TestMain is:
// //