Naming tests to self-document
Fri, Feb 3, 2017Go doesn’t specifically enforce you how you choose your test names. Tests are a significant contributors for the maintainability of your code. Tests not just providing correctness checking but also are useful in self documenting your code and its usage. On top of that, tests are the single best source to read about responsibilities of a type, function, etc. This is where naming tests better can help the internal documentation and therefore the maintainability of your code.
Emphasize the role of what you are testing rather than naming after the inputs and outputs.
func TestTitleIllegalChar(t *testing.T) {}
Instead, explain that the doc needs to be able to escape illegal characters on edit.
func TestTitleEscape(t *testing.T) {}
With this rename, we also self-document how the illegal characters on the title will be handled.
We sometimes pick very inclusive test names and write big table-driven
tests. If you are running table-driven tests, you can convert them to
subtests and name the individual cases.
Then go test -v
can pick those name and its output will act as a spec of
your type, function, etc.