When running "go test", is there a way to get a count of how many tests were run?

Viewed 1932

I'd like to get the number of tests that were run with go test, as kind of a checksum to detect if all the tests are running. Since Go relies on filenames and method names to determine what's a test, it's easy to mistype something, which would mean the test would silently be skipped.

1 Answers

I think that the gotestsum tool is close to what you are looking for. It is a wrapper around go test that prints formatted test output and a summary of the test run.

Default go test:

go test ./...
?       github.com/marco-m/timeit/cmd/sleepit   [no test files]
ok      github.com/marco-m/timeit/cmd/timeit    0.601s

Default gotestsum:

gotestsum 
āˆ…  cmd/sleepit
āœ“  cmd/timeit (cached)

DONE 11 tests in 0.273s            <== see here

Checkout the documentation and the built-in help, it is well-written.

In my experience, gotestsum (and the other tools by the same organization) is good. For me, it is also very important to be able to use the standard Go test package, without other "test frameworks". gotestsum allows me to do so.

On the other hand, to really satisfy your requirement (print the number of declared tests and verify that that number is actually ran), you would need something like TAP, the Test Anything Protocol, which works for any programming language:

1..4                               <== see here
ok 1 - Input file opened
not ok 2 - First line of the input valid
ok 3 - Read the rest of the file
not ok 4 - Summarized correctly # TODO Not written yet

TAP actually is very nice and simple. I remember there was a Go port, tap-go, but it is now marked as archived.

Related