--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I agree with Chris. Because testing main packages is a pain, I prefer to keep as much logic out of the command as possible. The command should just parse the flags and arguments and marshal them into the form that the logic, in another package, requires. If this can be outsourced to a library like cli or cobra, even better.
// In test code
var glogv = flag.Int("glogv", 0, "")
// Once after flag.Parse is called, but before logging starts.
// Probably in TestMain, but that was only added in Go 1.4.
// I was doing it in a SetUpSuite method handled by gopkg.in/check.v1.
flag.Lookup("v").Value.Set(fmt.Sprint(*glogv))
Another scenario is encountered when using the popular package github.com/golang/glog. It provides leveled logging with a global singleton and also gets its configuration from the flag package. I used glog for several months until I became frustrated by its lack of flexibility. One such annoyance is that glog defines a -v flag to control verbosity. That's great, except go test also defines a -v flag (which it passes down to the -test.v flag defined in package testing). Thus, if you would like to change logging verbosity for a test run the test code must define a separate flag to collect the logging verbosity and then copy the value into the glog flags. That adding the following two bits to the package test code:
// In test code
var glogv = flag.Int("glogv", 0, "")
// Once after flag.Parse is called, but before logging starts.
// Probably in TestMain, but that was only added in Go 1.4.
// I was doing it in a SetUpSuite method handled by gopkg.in/check.v1.
flag.Lookup("v").Value.Set(fmt.Sprint(*glogv))
-v
print the names of packages as they are compiled.
go test packagename -v verboseLevel -test.v testVerboseLevel
go test [-c] [-i] [build and test flags] [packages] [flags for test binary]
If the test binary needs any other flags, they should be presented after the package names. The go tool treats as a flag the first argument that begins with a minus sign that it does not recognize itself; that argument and all subsequent arguments are passed as arguments to the test binary.
--