go test cli arguments

1,672 views
Skip to first unread message

pierre...@gmail.com

unread,
Sep 22, 2015, 1:29:49 PM9/22/15
to golang-nuts
Hello gophers,


I am trying to find a way to get to the command line options passed to 'go test', within an init() or TestMain() function, and so far have come empty handed.

My requirement is that I need to initialize pretty expensive data structures for benchmarking, that I do not need for normal tests. I would like to avoid initializing them as this would speed up my tests a lot.

I have tried the following but I do not get any argument:

func TestMain(m *testing.M) {
flag.Parse()
fmt.Printf("ARGS: %v\n", flag.Args())
os.Exit(m.Run())
}


Is it possible?

Thanks!

Pierre

Giulio Iotti

unread,
Sep 22, 2015, 1:38:11 PM9/22/15
to golang-nuts, pierre...@gmail.com
On Tuesday, September 22, 2015 at 8:29:49 PM UTC+3, pierre...@gmail.com wrote:
My requirement is that I need to initialize pretty expensive data structures for benchmarking, that I do not need for normal tests. I would like to avoid initializing them as this would speed up my tests a lot.

You can make a global boolean value "dontInitX" and check it before initializing your expensive X: if (dontInitX) return;

In init() of your x_test.go file, set dontInitX = true.

This should do what you want.

-- 
Giulio Iotti 

Jan Mercl

unread,
Sep 22, 2015, 1:43:47 PM9/22/15
to pierre...@gmail.com, golang-nuts


On Tue, Sep 22, 2015, 19:29  <pierre...@gmail.com> wrote:



Declare test flags in your foo_test.go at pkg level:

var x = flag.Bool(...)

Do not call flag.Parse in the test file. No need for TestMain either.

--

-j

pierre...@gmail.com

unread,
Sep 22, 2015, 2:56:20 PM9/22/15
to golang-nuts, pierre...@gmail.com
Hmm. Thanks for the answers but I knew this, and this is not what I am after :).


If I run the following command:
go test -bench Ext

I want to know within my _test.go file if I have the -bench flag and the value passed with it, i.e. the arguments to "go test", not to my program.

Basically I want to know if I need to initialize my data structures for benchmarks. I have a couple of them so I would like to run them in separate goroutines and only if I need them.


For now, I just initialize them in the BenchmarkExt1, etc functions and reset the timer, such as the following, but they run sequentially:
func BenchmarkExt1Has(b *testing.B) {
 initExt1()
 b.ResetTimer()
 ...
}

func BenchmarkExt2Has(b *testing.B) {
 initExt2()
 b.ResetTimer()
 ...
}

I dont think it is possible, but I ask just in case :).

James Bardin

unread,
Sep 22, 2015, 4:00:36 PM9/22/15
to golang-nuts, pierre...@gmail.com


On Tuesday, September 22, 2015 at 2:56:20 PM UTC-4, pierre...@gmail.com wrote:
Hmm. Thanks for the answers but I knew this, and this is not what I am after :).


If I run the following command:
go test -bench Ext

I want to know within my _test.go file if I have the -bench flag and the value passed with it, i.e. the arguments to "go test", not to my program.

Basically I want to know if I need to initialize my data structures for benchmarks. I have a couple of them so I would like to run them in separate goroutines and only if I need them.



In TestMain if you have one, or init(), you can use:

bench := flag.CommandLine.Lookup("test.bench")
if bench.Value.String() != "" {
fmt.Println("bench was set")
}




pierre...@gmail.com

unread,
Sep 22, 2015, 4:34:51 PM9/22/15
to golang-nuts, pierre...@gmail.com
Thank you James, this is exactly what I was after.

It works like a charm!

For future reference, the code looks like the following:

Thank you all!
Reply all
Reply to author
Forward
0 new messages