Enhancing testing

Testing in Go can be very verbose, on account of having to check, and trigger the appropriate log and failure functions. A quick look at some tests in Dgraph at present, there’s some redundant testing calls, like t.Error and then t.Fail. Further, when there are failures, the testing package doesn’t actually provide location where failure occurred.

I’ve been using GitHub - stretchr/testify: A toolkit with common assertions and mocks that plays nicely with the standard library for testing. It still uses the Go Test conventions, and the testing package can still be used and is appropriate in some circumstances. However it splits the Fail/FailNow ideas into assert/require, provides location details on all failures, and gives pretty printing of reasons for failure at a higher level than the testing package by default. Here’s an example of its use: missinggo/hostmaybeport_test.go at master · anacrolix/missinggo · GitHub. You can well imagine to do this using pure standard library would be several times longer, and probably contain many formatting mistakes that wouldn’t be apparent unless errors actually occured.

I’ve found the package to be stable, lightweight, and straightforward to use. There’s no need to retrofit it to any existing tests.

4 Likes

Sure, sounds like a good idea. Maybe do a big sweep across the code base and enable testify library across it.

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