3 ms·
"You might think that unit testing helps you avoid technical problems, but the fact is that unit testing is more about getting a psychological effect rather tha
by davesque 6y ago
"You might think that unit testing helps you avoid technical problems, but the fact is that unit testing is more about getting a psychological effect rather than a technical one, it's a way to make you feel better about yourself - yes, I said it."
Probably could not disagree more with this statement. I recently implemented an element-wise hash function over arrays of arbitrary length values. I wanted to be doubly sure that my logic for choosing memory offsets into these arrays was correct. So I tested the function extremely thoroughly on some important examples and also on randomly generated data. I did this because the hashing function was a core component of the system I was building. If it didn't work, especially on subtle edge cases, the consequences would be severe. So what did I get out of testing that function so thoroughly? Psychological effect, as the author puts it? Well, yes I suppose. I get to feel confident that the code works as expected. I get to believe this not just when I originally write it but also when changes are made to it on into the future. I get to believe the same about components built on top of and around it. But calling these benefits "psychological", while technically true, seems to miss the point. I'd also call them essential. If you want to increasingly waste your time chasing issues that pop up because your code isn't properly covered by tests (and do that in production because you deployed before realizing there was a problem), be my guest.
- jfim 6y agoIt really depends what one is building. As you point out, in your example, testing is very useful since it allows you to be confident that your code works. Some of the backlash around testing is about testing things that really shouldn't need to be. If there's a mandate from some clueless management that all code must be covered by tests and the new code is simple data containers, then one ends up having to write tests for methods like getValue() { return value; }, which is a bit absurd. I'm assuming the author of the article is in the latter situation, not the situation you describe where tests are actually useful.
- commandlinefan 6y ago> write tests for methods like getValue() { return value; } Well, if you're just measuring code coverage (which is what clueless management obsesses about), as long as something that uses that getter is tested, it will be covered and won't need its own separate unit test. And if it isn't covered, that means you're not using it in which case you can probably get rid of it...
- emodendroket 6y agoSurely that’s not really what’s meant though. I’m sure many of us have experience with tests where so many things are mocked out, and so many nonfunctional assertions made, that they mostly tell you whether you changed the code in any way at all.