The MustCreate Pattern for Go Tests
0 comments · go, testing
- Comments23
0 comments · go, testing
Story details from Lobsters ↗ · linked article on mtlynch.io.
The MustCreate Pattern for Go Tests was first seen by Trendoday on Nov 6, 2026. Since then it has appeared in 89 checks on 3 different days. It reached #2 on Lobsters · Hottest on Nov 6, 2026 UTC. It was last seen on Lobsters · Hottest on Nov 8, 12:51 UTC.
Position on Lobsters · Hottest at every check (lower is better).
Best rank per day (UTC).
Platform metric as reported by Lobsters.
A few top-level comments from Lobsters, shortened. Read the full thread on Lobsters.
I think in these cases, I would either ignore the error (`username, _ := ...`) or write a simple `assert(t, err == nil)` that makes the `if` check just one line. Then, fuzz `NewUsername` separately to handle the cases where the requested username could be invalid and those errors matter. But hey, decent application…
Simpler, but with `panic` ("with a value I know is valid"): ```go func Must[T any](t T, err error) T { if err != nil { panic(err) } return t } ``` Then it is a normal function call: `user := Must(NewUsername("john"))` https://go.dev/play/p/XEx083CU9EJ --- If you are really allergic to panic: ```go func Must[V any](v…
Thanks for reading! I similarly don't think I'd object to yours in the wild, but I do find it less expressive. The advantage of `MustCreate` is that it communicates to the reader that this is logic that's unrelated to the system under test. If you do `assert(t, err == nil)`, then it looks like any other assertion in…
Archived daily, weekly and monthly lists that include this entry.
Not at the moment. Its last recorded appearance was on Nov 8, 12:51 UTC.
#2 on Nov 6, 2026 UTC.
It was first seen on Nov 6, 2026 and has been on the list on 3 days (89 checks) since.