Passing -v=10 flag to glog when running tests

5,769 views
Skip to first unread message

Daniel Garcia

unread,
Jan 23, 2014, 6:20:57 PM1/23/14
to golan...@googlegroups.com

I am using https://github.com/golang/glog for logs. I want to be able to set the log level when running tests with go test but I can't seem to figure out the syntax. Does anyone know how to do this?

Regards,
Daniel

David Symonds

unread,
Jan 23, 2014, 6:36:37 PM1/23/14
to Daniel Garcia, golang-nuts
Run
go help testflag
for the relevant syntax.

Daniel Garcia

unread,
Jan 27, 2014, 4:51:01 PM1/27/14
to David Symonds, golang-nuts

It seems like the only way to pass the flag is to compile a test binary with: go test -c . Then, execute the binary with flags to both modules: ./[testbinary] -v=10 -test.v
That is rather unfortunate as it looks like glog is not open to enhancement requests.

Thanks,

Rob Pike

unread,
Jan 27, 2014, 5:52:21 PM1/27/14
to Daniel Garcia, David Symonds, golang-nuts
That's simply not true.

$ cat x_test.go
package foo

import (
"flag"
"testing"
)

var f = flag.Int("f", 23, "test")

func TestFlag(t *testing.T) {
println(*f)
}
$ go test
23
PASS
ok _/Users/r/foo 0.026s
$ go test -f 27
27
PASS
ok _/Users/r/foo 0.025s
$

Rob Pike

unread,
Jan 27, 2014, 5:59:09 PM1/27/14
to Daniel Garcia, David Symonds, golang-nuts
Ah, I bet I know what's wrong: the -v is in conflict with go test -v.
Go test help says:

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.

But that's not true, at least in practice. There's a bug in go test.

But here's an easy workaround:

var myV = flag.Int("myV", 23, "test")

func TestFlag(t *testing.T) {
flag.Lookup("v").Value.Set(fmt.Sprint(*myV))
println(*v, *myV)
}

Then run go test -myV=27

-rob

Navneet Kumar

unread,
Mar 7, 2014, 5:23:53 AM3/7/14
to golan...@googlegroups.com, Daniel Garcia, David Symonds
I am using glog and setting its parameters programatically via flag.Set
The problem I am facing while running tests with verbose mode is;
flag provided but not defined: -test.v

If i try to define a flag named "test.v", it complains about redefinition
/var/folders/dx/x652bgh937v6vj485r3vrpqdqqmrm9/T/go-build640806676/command-line-arguments/_test/test.test flag redefined: test.v

Now I am confused, If its already defined then why it says "flag provided but not defined"

--
Navneet Kumar

Rob Pike

unread,
Mar 7, 2014, 6:54:13 AM3/7/14
to Navneet Kumar, golan...@googlegroups.com, Daniel Garcia, David Symonds
Works fine for me; the flag called "test.v" is available to the test functions.

% cat x_test.go
package main

import "testing"
import "flag"

func TestX(t *testing.T) {
err := flag.Set("test.v", "false")
if err != nil {
t.Fatal(err)
}
}

func TestY(t *testing.T) {
}
% go test -v x_test.go
=== RUN TestX # NOTE: does not print TestY
PASS
ok command-line-arguments 0.013s
%
If you don't show us your code we can't help you further.

-rob

navneet

unread,
Mar 7, 2014, 8:56:26 AM3/7/14
to Rob Pike, golan...@googlegroups.com, Daniel Garcia, David Symonds
I am also importing glog.
Here is the code
package impl_test

import (
"flag"
"fmt"
"testing"

)

func TestFlag(t *testing.T) {
fmt.Println("TestFlag testing:")

flag.Set("alsologtostderr", "true")
flag.Set("log_dir", ".")
flag.Set("v", "3")
flag.Parse()

glog.Info("Logging configured")
}
Note: This is in folder impl
The package name has been changed to "impl_test" to avoid circular dependency

when I run as
bash@navneet$ go test -v aa_test.go 
=== RUN TestFlag
TestFlag testing:
I0307 19:19:08.471506 53448 aa_test.go:19] Logging configured
--- PASS: TestFlag (0.00 seconds)
PASS
ok   command-line-arguments 0.050s 

But when I run as . or ./...
bash@navneet$ go test -v ./...

flag provided but not defined: -test.v
Usage of /var/folders/dx/x652bgh937v6vj485r3vrpqdqqmrm9/T/go-build359234741/zoho.com/deluge/api/query/impl/_test/impl.test:
  -alsologtostderr=false: log to standard error as well as files
  -log_backtrace_at=:0: when logging hits line file:N, emit a stack trace
  -log_dir="": If non-empty, write log files in this directory
  -logtostderr=false: log to standard error instead of files
  -stderrthreshold=0: logs at or above this threshold go to stderr
  -v=0: log level for V logs
  -vmodule=: comma-separated list of pattern=N settings for file-filtered logging
exit status 2
FAIL abc.com/X/Y/Z 0.342s

The test runs fine if I remove the -v parameter



Navneet Kumar


---- On Fri, 07 Mar 2014 17:24:13 +0530 Rob Pike<r...@golang.org> wrote ----

Jan Mercl

unread,
Mar 7, 2014, 10:13:24 AM3/7/14
to nav...@zohocorp.com, Rob Pike, golan...@googlegroups.com, Daniel Garcia, David Symonds
On Fri, Mar 7, 2014 at 2:56 PM, navneet <nav...@zohocorp.com> wrote:

Define flags in TLD vars in impl_test, not inside a test and, IINM,
don't call flag.Parse.

-j

Rob Pike

unread,
Mar 7, 2014, 3:54:45 PM3/7/14
to Jan Mercl, nav...@zohocorp.com, golan...@googlegroups.com, Daniel Garcia, David Symonds
Yes and yes. Do not call flag.Parse; the testing framework does that for you.

filmore...@gmail.com

unread,
Nov 1, 2015, 3:33:14 PM11/1/15
to golang-nuts, 0xj...@gmail.com, nav...@zohocorp.com, dan...@danielgarcia.info, dsym...@golang.org
The solution that worked for me to reconcile the -v flag collision between go test and glog is to use the -vmodule flag for glog instead of -v

edunc...@gmail.com

unread,
May 12, 2016, 2:03:40 PM5/12/16
to golang-nuts
This is what I use with glog in my test file(s):

func init() {
if testing.Verbose() {
flag.Set("alsologtostderr", "true")
flag.Set("v", "5")
}
}

(I just ran into this today, fyi)

imesh.g...@gmail.com

unread,
Aug 24, 2016, 9:45:18 AM8/24/16
to golang-nuts, edunc...@gmail.com
Thanks! This was useful! 

edunc...@gmail.com

unread,
Jan 15, 2017, 4:49:42 PM1/15/17
to golang-nuts, edunc...@gmail.com
Btw, I have to make a correction to my previous post (see below).  This init() doesn't seem to take effect all the time (or at all in some projects).  I also tried putting them into TestMain instead inside of my test files with the same "doesn't always work" result (most of the time, it does not detect the level).  Must be a combination of t.Parallel() and not calling glog.Flush().  

The only that truly works 100% of the time is to insert it into each test method.  But that's pretty ugly though.

I am not sure when Golang Tests introduced the -args flag; but, today I was reading the docs again and noticed this new -args flag.  

It works!

    $ go test -v -args -v 3 -logtostderr true

Target a specific function:

    $ go test -cover -v -run TestEventsTableTests -args -v 3 -logtostderr true

Obviously, -cover and -v are optional for tests and you can just get away with:

    $ go test -run TestEventsTableTests -args -v 3

^- glog seems to output to stderr anyways, without specifying -logtostderr.  

Per the docs, you have to add it to the end of the packages to test if specifying packages:

    $ go test -cover -v ./podcast -args -v 3 -logtostderr true
Reply all
Reply to author
Forward
0 new messages