Unit testing, everybody’s favorite part about writing code. Ok, it may not be for everyone, but maybe that’s just because you have not had the opportunity. Until recently I was in the same boat. “The code works, why do I need to test it?” Well, after writing a few myself, it didn’t actually seem so bad. I would even argue that it made me a better programmer.
It goes a little like this: “Do this, expect that. Do this, with this input, and expect this other thing.” You’re basically proving that your code will do what it was intended to do, with all the possible inputs. At the very least, it gives you confidence that your code will not break, or have any unintended consequences. Sweet, right? You can have it fail gracefully or as expected. As an added bonus, it prevents other programmers from unintentionally breaking your code in the future. You may even go back to the original function and improve it, for more usability.
Now let’s start with the code that we will be testing.