Contrary to popular belief, unit tests are useful for quality and eliminate certain classes of bugs. The belief that unit tests don’t prevent bugs,

How Unit Tests Really Help Preventing Bugs

submited by
Style Pass
2024-06-06 10:00:12

Contrary to popular belief, unit tests are useful for quality and eliminate certain classes of bugs. The belief that unit tests don’t prevent bugs, or are not helpful, or a futile exercise arises because people do unit testing wrong.

Not every unit test is the same. Not all code coverage is the same. 80% (line or branch) in one code base can be very different from 80% of another code base. It is in the quality of the unit tests that determines how useful they are. This is the reason I advise all my CTO clients to put QA in charge of reviewing all tests, including unit tests. This way they support developers to write better tests, and educate developers and help with preventing bugs.

When you have low code coverage or no measuring of code coverage at all, unit tests are not going to help (contrary to a small number of E2E tests). To benefit from unit tests, you will need to measure and raise code coverage. If you have no code coverage, start with a goal of 10% coverage. Then increase by month or quarter the coverage level by 10%. Reaching 70% is straightforward, so aim for that at first.

For the example we can write a test to see if add returns an error for error conditions (which might also be that add calls another method that returns an error instead of failing).

Leave a Comment