Why we unit test.

Programmers write bugs (you didn’t think bugs wrote themselves right?). To deal with these logic regressions we write tests to keep ourselves honest. Test are both a way to ensure that the program really does what intended, and to communicate the programmers intent to the next person who looks at the code. But some of us still ask why we are doing this. Here are some reasons.

First lets consider the point of view of someone who doesn’t like unit testing. They often meet a few of the following criteria:

  • They are the only person working on their code (or part of a small team).
  • They are writing code where it doesn’t matter if all the edge cases are met. Small errors or bugs just aren’t that important.
  • They are working on a once off project (possibly literally a school project or employment code challenge) that they will never work on again after finishing it.
  • The codebase they are working on is tiny.

In those circumstances I probably wouldn’t want to do much (or any) testing either. If it is small and rather unimportant then testing just isn’t a priority. But here are some reasons that you might want to test your code.

  • Your are working with a larger team. When you have more coders then you move fast (and break things). 20 or 30 programmers in the same codebase are going to start stepping on each others toes and writing code that touches code they didn’t write in the first place. Then you need the tests to keep these coders (who don’t know the whole codebase) from inserting bugs into an area they might not fully understand.
  • Edge cases are important. If a financial instrument tries to divide by zero and you aren’t gracefully handling that then the errors that result could be costly. This happens even if the errors are just in statistics that are being displayed; an errors noticed will degrade trust in the app. Sometimes it pays to be sure that your code is exactly correct.
  • The project will be running for years. This is most business software and any open source software that gets popular. When code is used for a long time it must be proven dependable and it must be ready to be changed and upgraded. Adding features to a codebase that has testing is much easier than a codebase without testing.

So, those are some reasons to add tests to your codebase. For more I suggest you read better, faster code with test driven development.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *