Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use "go test" to list all tests case

Tags:

go

With go test -v pattern I can select and only run the tests that matches a pattern, but is there any way that I can list all test cases without run them. There is this case that the project just handed over to me has a lot of test case and I need to select some tests to run. I know greping the sources xxx_test.go is a way but is there more elegant way? I mean, each test has its meta data stored in some where, right? as the tests are required to be in a specific signature(func TestXXX(*testing.T)). This meta data can be used to do this kind of work.

like image 219
fluter Avatar asked Apr 29 '16 12:04

fluter


3 Answers

Just saw this in the Go1.9 release notes (draft) and I think could be what you are looking for

The go test command accepts a new -list flag, which takes a regular expression as an argument and prints to stdout the name of any tests, benchmarks, or examples that match it, without running them.

like image 65
Ignacio Vergara Kausel Avatar answered Oct 03 '22 21:10

Ignacio Vergara Kausel


There's no saved metadata, grepping is the only way pretty much.

If you have ack, you can easily use something like this:

➜ ack 'func Test[^(]+'
like image 26
OneOfOne Avatar answered Oct 03 '22 22:10

OneOfOne


If you do not use ack as another answer mentions, you may simply grep -r "func Test" . | wc -l. This works because

  • grep for substring func Test, which should include every test func (it also includes TestMain)
  • flag -r tells grep to look in subdirectories recursively
  • wc -l will count the number of lines piped into it, which should be the number of test cases
like image 27
gastrodon Avatar answered Oct 03 '22 20:10

gastrodon