Unit Testing – You’re Measuring it Wrong
Join the DZone community and get the full member experience.Join For Free
i’ve been having this problem ever since i’ve started teaching (and
preaching) about scrum, clean code, unit testing and tdd and any other
software development practice.
when implementing a change - how do you measure its success?
for that matter – how can you measure a developer productivity?
by our hero
a few years ago i worked with a “brilliant” manager who tried to measure developer’s productivity by counting the number of bugs a developer fixed on a given day. being my argumentative old self i’ve explained that no two bugs are created equal and besides we should aspire not to write these bugs in the first place.
my comment was not well received – i was branded a subversive element – he felt that i wanted to prevent him from assessing developer’s work (which i didn’t).
over the years several attempts has been made to calculate developer’s productivity – with similar result such as counting lines of code:
“measuring programming progress by lines of code is like measuring aircraft building progress by weight.”and it only get’s trickier when faced with the need to measure unit testing efforts.
- bill gates
how to measure unit testing effectiveness
the problem is that it’s impossible to measure both how much time was saved and how many bugs were prevented. the only viable way is to have two developers write the same feature and compare the results – and i’m yet to find a company willing to throw money down the drain.
a few years ago i found a research ( which i use all the time ) that did something similar: two teams working at the same same projects, one using tdd and one not were timed coding time and bug density was checked afterwards (guess who had less bugs). unfortunately i cannot reproduce that experiment on every single company i work with in order to show that unit testing/tdd improved the way they are doing things.
there are two “matrices” that seem to interest managers when unit testing are concerned:
- test count (i.e. how many tests we have)
- test coverage (i.e. what percentage of the code was tested)
test count does not tell me anything about the project’s state and it’s impossible to provide a number of tests per feature developed - how many tests are “enough”? 10, 100, 1000 or maybe a million?
as for code coverage it’s a tool, a very effective tool that helps me daily to find code i need to test and help me understand how healthy my unit testing suite is – to put it in the words of one of a former boss of mine:
what do you mean 20% code coverage? we had 75% three months ago…i know that i need an excellent reason to have less then 50% code coverage for a component, but how much coverage is enough coverage? 80%, 95% or 100%?
and just like any other tool code coverage can be gamed and easily abused to show good results for very bad code and tests.
both code coverage and test count are good at showing quantity not quality and can be used to measure progress (to some extent- we had 100 tests a week ago and 50% coverage and now we have 1000 tests and 85% coverage), and both should be handled with care.
why do you want to know?
there’s always a reason behind the need to measure or predict success. usually when asked about measuring unit tests it’s due to a need to estimate future effort (which we still don’t know how to do). someone up the food chain needs to fill reports, gantt-charts and approve expenses and needs to know what would be changed in the next x days/weeks/months. perhaps the guy who wanted to start using unit tests needs to explain it to his superior or investors.
the problem is that any metric given would be inaccurate – i can show you two different projects with roughly the same amount of unit tests and 90% code coverage -one would be a success and one an utter and complete failure.
from time to time i’m forced to provide these guesstimates – since people need to be able to know where they heading and how much work is planned. when doing this i do my best to explain that these are educated guesses and the real benefits are more then these numbers but hard to predict and quantify – but no one ever listens…
it all comes back to the fact that i cannot show time saved nor bugs which were not created due to the change – the two factors that i find most interesting.
success is measurable – in retrospectivei promise you one thing – after a short while the team would feel the change – for better or worse (usually for the better). adding features would be a breeze, less bugs introduced with each new feature, shipping a working product would be easier and faster and developers tends to be more effective in their work (and usually happier)
i usually use ndepend to show the current code complexity and how various coding violations disappeared after a few tests were written an refactoring were made.
looking back after a few weeks i can show real tangible improvement that i couldn’t possible predict with an accurate number.
and so i keep collecting these numbers with hope that someday when asked to predict or set measureable targets for developments i’ll have a good answer…
Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.