List of test names

Is there a simple way to get a list of test names in a package? And list of benchmark names?

For the latter, currently I use go test -bench=. -run=^$ -benchtime 0ms and it runs each benchmark with b.N=1.

I was about to file a proposal for -list flag in go test but decided to ask first. To me go test is the most obvious place for this functionality.

1 Like

Sounds good. That’s what I wanted for a long time.

It might be a little bit excessive, but still fine. I’d put it in godoc though. Ex: godoc -tests makes more sense to me.

What about something like this ?

go list -f '{{ join .TestGoFiles "\n" }} {{ join .XTestGoFiles "\n" }}' | xargs grep '^func [Test|Benchmark]'
3 Likes

I’ve considered this, but I’d like the list to be correct in all cases. It Is easy to trick grep, e.g.

var notATest = `
func TestNotATest
`

Sure, this isn’t perfect, but it is free and available today for all
versions of go.

2 Likes

A more realistic example that would not work with grep is

/** 
func TestDisabled(t *testing.T) {
}
*/

FWIW, I didn’t find a clean way to change existing go tools to output test list, so not going to propose anything:

  • go test --list would be mutually exclusive with almost all existing go test flags except -run and -bench, whereas -run wouldn’t not be accurate anymore (nothing is actually run)
  • go list -f "{{ join .Tests '\n' }}" would be inconsistent because besides tests there would be no top-level functions, types, variables, etc. Adding them is probably outside of the go list scope which is supposed to deal with package-level concepts.
  • go doc is supposed to print comments and a program would have to parse them and discard. Adding flag to go doc to not print comments would be odd.
  • oracle reports info about a selection in a file, not file as a whole.

For my purposes, I found that the simplest way is to use parser.ParseFile to get a list of functions. A program that just prints function names is 3 Mb though.

Right, so you can use some shell fu like I showed above – it isn’t perfect, if you try hard, you can fool it, but for the general case, where you’re not trying to stab yourself in the foot, it should work just fine, today, and doesn’t clock in at 3mb.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.