fluter
fluter

Reputation: 13836

Use "go test" to list all tests case

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.

Upvotes: 16

Views: 13886

Answers (3)

gastrodon
gastrodon

Reputation: 87

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

Upvotes: 1

Ignacio Vergara Kausel
Ignacio Vergara Kausel

Reputation: 6026

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.

Upvotes: 20

OneOfOne
OneOfOne

Reputation: 99351

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[^(]+'

Upvotes: 3

Related Questions