Provide additional/custom flag to go test tool

Skip to first unread message

Michael Whatcott

Apr 5, 2014, 1:29:02 AM4/5/14
I'm trying to make a custom command line flag available to a test function:

go test -custom=42

This kind of invocation results in the following error:

flag provided but not defined: -custom // followed by a flag usage listing... 

The only hack I've been able to come up with is to abuse the -timeout flag in order to pass my custom value:

go test -timeout=-42s // I'm using a specific *negative* duration for the custom flag value. Yuck.

That hack causes me to lose sleep. That is an ugly hack--I'm sunk if any sort of validation on that value is ever implemented. Is there a better way to pass a custom flag which the go test command will ignore so that my test function use it? I've goofed around with defining a separate flag set (flag.NewFlagSet(...)) but it didn't seem to help, but that might be because I didn't know my way around the flags package well enough.

Caleb Spare

Apr 5, 2014, 1:40:34 AM4/5/14
to Michael Whatcott, golang-nuts
This works fine for me. shows a
simple example; running 'go test -foo=hello' works and shows 'hello'
in the test output.

How are you defining your flag? Can you share your code?

Is that invocation ("go test -custom=42") exactly what you are
running? Note that 'go help testflag' says

"Flags not recognized by 'go test' must be placed after any specified packages."

> --
> 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
> For more options, visit

Michael Whatcott

Apr 5, 2014, 1:51:06 AM4/5/14
to, Michael Whatcott
Yes, that's getting close. My use case is somewhat involved: I'm the author of GoConvey, which is a two-part testing tool. The first part is a server that runs go test across several watched packages and reports the results to your web browser for easy viewing. The second part is a collection of functions that provide convenient ways to assert the results of your tests (that's a really brief summary). Basically, I want the server (part 1) to invoke go test in the same way for all watched packages:

go test -v -json // my custom flag

The problem is that it's not a requirement for any test package to make use of the GoConvey testing DSL (part 2) so that custom flag might not have been defined (because the GoConvey DSL wasn't imported). It's kind of a crazy use case but I'd really like to be able to pass an 'extra' flag to go test without it blowing up if that flag is not defined. Is that in any way possible?


Tamás Gulácsi

Apr 5, 2014, 2:01:29 AM4/5/14
Easy: expect the error, and try again without the custom flag if it is not defined.
(Maybe I misunderstood - if you just want to pass some info into tests, then you can use environment variables).
Reply all
Reply to author
0 new messages