2

I was wondering if unit testing is effective in finding bugs or if that is not one of its major goals at all.

We wrote unit tests for many business rules over time and after a few months we did not appear to gain much in finding bugs.

9
  • 1
    It is not very clear what you're asking. Or you probably should give us some more context. Whether it is effective or not depends on numerous different factors starting from how you treat effectiveness.
    – Alexey R.
    May 22, 2018 at 10:06
  • 1
    Possibly related: stackoverflow.com/questions/11917323/…
    – dvniel
    May 22, 2018 at 10:09
  • what kind of bugs are we talking about? bugs in new features? regression? also, you didn't find bugs with the unit tests, but did you find them via manual testing or in production?
    – Pieter A
    May 22, 2018 at 15:52
  • 'Did not appear to gain much in finding bugs'. Do you have a clear mapping of tests to requirements/Business rules? what is your current test coverage? May 23, 2018 at 0:34
  • @AlexeyR. Business rules are designed and implemented seperately in our coding framework. So there are many simple and seperated rules to test. We have not found much bugs in unit testing nor acceptance testing in this area. Do we targeted wrong area of code for unit testing? May 23, 2018 at 5:47

4 Answers 4

1

Unit testing is helpful for several reasons, primarily:

  • as part of TDD/BDD
  • to allow changes not to break stuff without you knowing it
  • as part of Continuous Integration
  • as part of Continuous Delivery

The interesting part of your question of course is why after a few months we did not appear to gain much in finding bugs.

There can be be a few reasons for this, primarily:

  • you test the wrong things
  • you have low quality tests
  • you fail to test the right things
  • you use a framework to guide you
  • you have not changed your development practices
  • you do not need or seek faster turnaround for feature implementation
  • you use linters and code quality tools which also uncover bugs and issues
  • many bugs are due to dependencies and your unit tests mock and stub them out
  • you still do a lot of manual testing which uncovers errors immediately as you develop
3

Well-written unit tests are good at finding regression bugs. The accepted answer to this question gives an excellent listing of the reasons unit tests can be a good thing.

Your biggest gains in your scenario right now are that you know the business rules you are testing are working correctly (assuming that your unit tests correctly test the rules).

When you need to refactor of change your code, your unit tests will tell you if you break any of the business rules you are testing, giving you a much shortened feedback loop (that is, the regression bug that is introduced will be found and fixed before the breaking change gets anywhere near a customer).

My personal view is that unit tests are invaluable not for finding bugs but for the assurance they give that new bugs have not been introduced into previously bug-free code.

3

You wrote the question as if you are a developer "since we wrote...", and developers would typically be aware of the benefits of unit tests. Therefore I wonder why you are asking.

Short answer: Yes

When developing software, it's best to find bugs at the earliest possible stage. The early stages might be bugs in the requirements document, or design document. A later stage where bugs might exist is within the code. An even later stage is bugs in release candidates. The worst possible place to find bugs is in production.

Writing unit tests allows developers to exercise their methods, classes, or even collections of classes at a low level. Haven written unit tests I know myself that sometimes when I put my testing hat on, I'll think of things I hadn't thought of while writing the method such throwing an accurate exception if some invalid input is passed into a method.

So, in this sense, unit tests would catch bugs, but no one but the developer may know about them - they fix them on the fly.

The other major benefit of unit tests is for. maintenance. If a new developer comes along, changes something, and then sees the unit tests fail, they'll be able to do something about it. Again, the QA team may never be aware of this, as the developer may fix it on-the-fly using just his local deployment.

I've worked in a few software companies, and the above two benefits are generally accepted. The cost of unit tests is outweighed by the benefits. Perhaps your case is an exceptional one where the benefits have not yet been rewarded.

0

Generally, unit tests are written by the developers in order to find bugs in their code. In some cases the developer will write a unit test and it will help him to find the bug immediately. In other cases, all the unit tests will pass and the code would be bug-free. In this case, unit tests assure that even when the code will be changed in the future - there won't be any new bugs. For example, if the developers write a new piece of code - they can make sure it doesn't affect the already-written code by running the tests again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.