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.
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.
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[^(]+'
If you do not use ack
as another answer mentions, you may simply grep -r "func Test" . | wc -l
. This works because
func Test
, which should include every test func (it also includes TestMain
)-r
tells grep to look in subdirectories recursivelywc -l
will count the number of lines piped into it, which should be the number of test casesIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With