Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you measure the quality of your unit tests?

If you (or your organization) aspires to thoroughly unit test your code, how do you measure the success or quality of your efforts?

  • Do you use code coverage, what percentage do you aim for?
  • Do you find that philosophies like TDD have a better impact than metrics?
like image 396
vfilby Avatar asked Nov 04 '08 17:11

vfilby


People also ask

What makes a unit test high quality?

Good unit tests should be reproducible and independent from external factors such as the environment or running order. Fast. Developers write unit tests so they can repeatedly run them and check that no bugs have been introduced.

How do you measure test effectiveness?

- Test effectiveness = Number of defects found divided by number of test cases executed.

What is good unit test coverage?

Aim for 95% or higher coverage with unit tests for new application code. When developers unit test as they program, they improve the longevity and quality of the codebase. The time a development team invests in unit tests pays off with less time spent troubleshooting defects and analyzing problems later.


2 Answers

My tip is not a way to determine whether you have good unit tests per se, but it's a way to grow a good test suite over time.

Whenever you encounter a bug, either in your development or reported by someone else, fix it twice. You first create a unit test that reproduces the problem. When you've got a failing test, then you go and fix the problem.

If a problem was there in the first place it's a hint about a subtlety about the code or the domain. Adding a test for it lets you make sure it's never going to be reintroduced in the future.

Another interesting aspect about this approach is that it'll help you understand the problem from a higher level before you actually go and look at the intricacies of the code.

Also, +1 for the value and pitfalls of test coverage already mentioned by others.

like image 78
webmat Avatar answered Oct 07 '22 14:10

webmat


Code coverage is a useful metric but should be used carefully. Some people take code coverage, specially the percentage covered, a bit too seriously and see it as THE metric for good unit testing.

My experience tells me that more important than trying to get 100% coverage, which is not that easy, people should focus on checking the critical sections are covered. But even then you may get false positives.

like image 34
t3mujin Avatar answered Oct 07 '22 15:10

t3mujin