Back to all stories
Lobstersseen ×6

The MustCreate Pattern for Go Tests(mtlynch.io)

The article discusses a technique for improving the readability and reducing repetition in Go tests, specifically when dealing with constructors that can fail. The author previously used a helper function approach, creating a new function like `MustCreateUsername` for each type, but found this approach scaled poorly. With the introduction of generics in Go 1.18, the author discovered a better solution: defining a single, generic `MustCreate` helper function that can be used with any type, eliminating the need for multiple helper functions and making tests more concise and efficient.