By its nature, testing is never complete. As the influential computer scientist Edsger Dijkstra put it, “Testing shows the presence, not the absence of bugs.” No quantity of tests can ever prove a package free of bugs. At best, they increase our confidence that the package works well in a wide range of important scenarios.
If you feel a bit fuzzy about your Go tests, you’re not alone. Even if you diligently write tests for all your code, there’s always that sneaking doubt: what if there’s some test case you didn’t think of?
In previous posts, we’ve talked about ways to generate test cases randomly, and how to use Go’s fuzz testing facility to automate this process. Now let’s see how to apply fuzz testing to a realistic function, and use it to find a very common Go bug, by writing a fuzz target.
Suppose we want to write some function FirstRune that takes a string and returns its first rune. Given the string "Hello", let’s say, it should return the rune 'H'. That sounds doable, doesn’t it?