Despite the unfortunate reality that a very small part of the code a developer writes has unit tests, those are very useful. Everybody hates unit testing when it comes to write it, most often because one sadly realizes what kind of code just wrote. Yes, it happened to me. I was pretty excited about my opera, but when I attempted to write the unit tests I was like: wait, what?
The simplified purpose of tests are to make sure your code does what’s supposed to, doesn’t do what’s not supposed to (and when it actually does, well, it’s a feature), and the new code you’re going to write doesn’t break the current code. Yes, it applies to refactoring also.
Implementation tests. Writing implementation unit tests is expensive in terms of time, effort and have questionable value starting some point. It’s a daring statement, but those days, boy, the things change so fast and often! It’s great to be having implementation tests, they give you the extra safety on writing code you’re not sure about (do they?). They tell you when you’re using that new variable that does an extra processing – that may cost you app extra speed and memory. Happy performance!
Functional tests. Unlike the implementation ones, the functional unit tests are covering the public functions only and they’re like a black box. You don’t care how (and don’t have control over) the code is doing the thing, you just care that the function is returning the right object. You can refactor the code 10 times, if the return remains the same, your unit test is happy. With some attention, you keep this test happy for a long time. Happy Refactoring!
How about you? Which of those you consider more useful?
Image source: Capgemini