4 ms·
Agreed. Code that retains state does not lend itself well to unit testing. One of my latest projects at work was a MySQL node monitor with automatic failover. I
by IgorPartola 16y ago
Agreed. Code that retains state does not lend itself well to unit testing. One of my latest projects at work was a MySQL node monitor with automatic failover. I did add unit tests to it, but I can tell you that I found more bugs, or rather quirks of MySQL's replication behavior and the MySQLdb driver through doing my manual testing. I also spent quite a bit of time fixing the test rather than the code, since the quirks meant I needed different behavior. As this piece of code is quite important to us, the ROI on it will be high. Then again the investment was sizable as well.
I think unit testing should be viewed in terms of ROI because, just like premature optimization, often times a lot of effort goes into it where the return is negligible. Pure functions can be easily tested, so test your critical calculations every time. On the other end of the spectrum you have things that interact with external systems. Do you unit test an email notification function? How about a CSS layout? Can you easily test for an out of memory condition or a failed hard disk? Sometimes it is just cheaper to use human judgement.
Lastly, as many have pointed out, unit testing is not a silver bullet. Just because you did not break the unit tests does not meant the code still functions properly.