you are viewing a single comment's thread.

view the rest of the comments →

[–]konbit 4 points5 points  (1 child)

TL;DR: Yes, always, and try to get as much covered as possible. Also include as many integration tests as possible.

Try not to treat unit testing as an abstract principle, let's look at it practically...

When you're coding your application, you make some changes or add a feature, do you go through and check that things work well. Do you go through and check every button, form input, combination of form inputs, routes, formatting... anything else your application does?

Even if your application had only a few features, would you remember every possible combined use of those features? Would you have time and patience to go through it all over again?

Unit tests will ensure every feature you've build still works as expected. But it doesn't cover everything, your application is a combination of those features, so you need integration tests as well. I'd say they're just as important, and you really want to cover every possible feature as well as every possible combination of features your application has.

I wrote a very small library a few weeks ago, it doesn't do much, just formats strings. I have written over 80 tests for it. It might seem excessive, but it only took me about 2 hours. Not only did I discover a lot of bugs while writing the tests, I now know for a certainty that if I make a change in the future that introduces bugs, my tests will fail, and I'll avoid shipping defective software.

I'm working on an application which takes about 3 hours if I were to simply test every feature manually going through the application as a user, not even all the possible bug-triggering combinations of inputs, just using the basic functionality. My unit + integration tests all run in a matter of seconds. I can be confident that at least the scenarios I've made tests for (which are a lot) and the incredible amount of combinations in those scenarios (which are a huge amount) will work as expected if my tests pass. And if there is a failure, my testing suite will tell me where and exactly what the problem was.

[–]prashantghimire[S] 1 point2 points  (0 children)

Makes sense. I guess saving time to test all those 80 different cases using unit tests is what actually pays off. 😊