FAQ
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

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Giulio Iotti at Sep 22, 2015 at 5:38 pm

    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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Sep 22, 2015 at 5:43 pm
    On Tue, Sep 22, 2015, 19:29 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

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Pierre Curto at Sep 22, 2015 at 6:55 pm
    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 :).



    Le mardi 22 septembre 2015 19:43:47 UTC+2, Jan Mercl a écrit :

    On Tue, Sep 22, 2015, 19:29 <pierre...@gmail.com <javascript:>> 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
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Sep 22, 2015 at 8:00 pm

    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")
    }




    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Pierre Curto at Sep 22, 2015 at 8:34 pm
    Thank you James, this is exactly what I was after.

    It works like a charm!

    For future reference, the code looks like the following:
    http://play.golang.org/p/4iUs_TW9a7

    Thank you all!


    Le mardi 22 septembre 2015 22:00:36 UTC+2, James Bardin a écrit :


    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")
    }



    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 22, '15 at 5:29p
activeSep 22, '15 at 8:34p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase