go test - skip one test

6,176 views
Skip to first unread message

sundar.v...@gmail.com

unread,
Apr 23, 2015, 7:41:31 AM4/23/15
to golan...@googlegroups.com
Hello,

I’m trying to skip one test with the "go test" command. 

I found that it is easy to run only one test using the -run option (go test -run="Test_function_that_I_want_to_run")

How to skip only one function (go test -run="!Do_not_run_this_function") => does not seem to do the trick.

Any help

Thanks
Sundar

Fatih Arslan

unread,
Apr 23, 2015, 7:58:01 AM4/23/15
to sundar.v...@gmail.com, golang-nuts
By adding "SkipNow()" inside the test function:
http://golang.org/pkg/testing/#T.SkipNow
> --
> 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.

sundar.v...@gmail.com

unread,
Apr 23, 2015, 8:19:37 AM4/23/15
to golan...@googlegroups.com, sundar.v...@gmail.com
Thanks.. But I don’t want to modify the code.

I see from the help that -run supports regular expression.

" -run regexp
   Run only those tests and examples matching the regular
   expression.
"

 Hence wanted to see if I can find a way to do that from command line.

Dave Cheney

unread,
Apr 23, 2015, 10:18:40 AM4/23/15
to golan...@googlegroups.com
Interestingly this is the second request for this feature in the last two weeks - but for five years prior to that, zip.

If you could skip a test from the cli,  what would that enable you to do ?

Fatih Arslan

unread,
Apr 23, 2015, 2:28:46 PM4/23/15
to sundar.v...@gmail.com, golang-nuts
You are right. What you can do is defining a flag yourself and doing
the skipping automatically based on the flag name. I've just tested it
and it works. Note that this is experimental and just a proof of
concept:

main.go: http://play.golang.org/p/B93z1q1hrT
main_test.go: http://play.golang.org/p/OuXCiJOuv3

Example outputs:

demo ❯ go test
Running TestA
Running TestB
PASS
ok _/Users/fatih/demo 0.004s

demo ❯ go test -skipTest "TestB"
Running TestA
PASS
ok _/Users/fatih/demo 0.004s

Michael Banzon

unread,
Apr 23, 2015, 2:34:59 PM4/23/15
to golang-nuts
You could use build tags[1]. Put the test you want to be able to skip in a file and guard the file with the build tag.

sundar.v...@gmail.com

unread,
May 1, 2015, 6:55:58 PM5/1/15
to golan...@googlegroups.com
Hello Dave,

I have two binaries - a consumer and a server. I validate consumer and then it also populates the DB. Then I test the server, in which there is a test for checking if deleting an entry in the test works. I want to avoid running this "delete" test alone during development, because, if I run this test alone, then every time I have to repopulate the database freshly for again testing the "server".

I could add a flag, as suggested by Fatih, however, if an option using the "-run" works, it would be much more cleaner.

Thanks,
Sundar

Dave Cheney

unread,
May 1, 2015, 7:28:21 PM5/1/15
to golan...@googlegroups.com
This sounds like you're solving the wrong problem. You want a flag to avoid running the test that deletes some information from a shared database. It sounds like you should add code that populates your database to a known state before each test run.

Matthew Zimmerman

unread,
May 1, 2015, 7:44:17 PM5/1/15
to Dave Cheney, golan...@googlegroups.com
I've wanted this feature before too.  Usually I'd use it when one of my tests starts panic'ing (then other tests don't run). In debugging the panic, I'll often notice that it's a more specific test that I want to validate as working before diagnosing the larger (panic'ing) test.  So often, I've wanted to validate that the rest of the tests are passing before addressing the panic.

--

Dave Cheney

unread,
May 1, 2015, 8:03:42 PM5/1/15
to golan...@googlegroups.com
I see these examples, but I'm unconvinced in general that this -runnot flag pays for itself over the cost of documenting and describing the interactions with -run. It seems there are always more appropriate or simpler solutions to achieve the same result.

I don't know if you were redirects here after trying to raise this on the issue tracker, but in the case that you haven't, you could try raising a request for this on the issue tracker.

Dave

Jan Mercl

unread,
May 2, 2015, 3:17:37 AM5/2/15
to golan...@googlegroups.com
On Thu, Apr 23, 2015 at 1:41 PM <sundar.v...@gmail.com> wrote:

> I found that it is easy to run only one test using the -run option (go test -
> run="Test_function_that_I_want_to_run")

> How to skip only one function (go test -run="!Do_not_run_this_function") 
> => does not seem to do the trick.

Use the -run option again, ie. simply write a regexp which does not accept the test(s) you don't want to run. For example, if I would need to run all test of cznic/b except for TestDelete*:

jnml@4670:~$ cd $GOPATH/src/github.com/cznic/b
jnml@4670:~/src/github.com/cznic/b$ go test -v -run Test[^D]

-j

Reply all
Reply to author
Forward
0 new messages