

When first getting started, I encourage developers to literally include the comments with those labels, but once you and your teammates get acclimated to the pattern those can be omitted. And in the final section Assert one or more conditions that you expect to be true. Act upon that function in the second section. Arrange what you need in the first section to be able to test your function.
GOLANG MAP GODOCS CODE
Organize your test into three sections of code separated by a blank line to add visual separation. This test uses the Arrange-Act-Assert design pattern. Looking at the three test names above, it is immediately clear what distinguishes each test from the others.Īs to the body, here is an example you will see again a bit later. You have already seen what I like to do with test names: highlight (via ALL_CAPS) what is unique or important about a test. Two things to look at here: the test name and the test body. In the immediate context, write tests to make them as clear and obvious as possible. Computers will understand your code no matter how convoluted you make it. What happens if we give an empty category? Or an invalid category? What constitutes a valid category? Write Tests for HumansĪ large portion of code we write does not have performance requirements down to microseconds. Immediately upon reading those, I bet you noticed we are missing some tests (and/or requirements!). HandleCategory trims LEADING and TRAILING spaces from valid category If you are I submit that you want to start the same way: write out the names of the necessary tests as you tackle each requirement. You may or may not be an advocate of TDD (test-driven development). Test names should correspond to the low-level requirements. Test names alone can reveal a lot about the quality of the code. After all, to test all possible inputs takes an unrealistically large amount of time for any but the most trivial function. You don’t have infinite time to write tests or, for that matter, to run tests. It is more of an art than a science.Īnd, of course, writing unit tests takes time, a very finite resource given the fast pace demanded by today’s market. Some might add (c) easy to that list but, as in any language, writing good unit tests is hard. Tests are both (a) vital and (b) practical in Go.

Go, being a newer language, has surprisingly good support for unit tests. But lose your unit tests and you are left with fragile, difficult to maintain, and immediately questionable code as soon as you make a change. What is more important than your code? Unit tests! Lose your code (for whatever reason) and you can recreate it from your unit tests (not saying it is trivial, but it’s possible).
GOLANG MAP GODOCS HOW TO

What you will find within are both concepts and practices, drawing from my experience across several languages, showing how to enrich your Go unit tests. I hope to fill in some of those gaps and provide a broad swath of ideas to tackle unit tests in Go, the language I am currently using on a daily basis.

We are expected to learn by osmosis, but often we end up dooming ourselves from the start, due to misconceptions or gaps in knowledge. Unit tests are crucial to long-term success of a project yet almost never taught formally.
