Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
Best practices for retrofitting legacy code with automated tests

When working in large legacy systems (large systems with no unit testing ever) I often come across people saying use unit testing as a tool against possible bugs. I wonder if somebody have tried it on large systems with no unit testing at all.

Of course it is easy to say unit testing helps ,but in large systems it can hugely time consuming process. It can take months if not years to fully unit test each part of the application .

When you are asked to make a functionality which is modification of existing functionality or adding new functionality to the application, how would you go about it? Of course there will be lots of instances where code in different classes will look similar and you would want to re-factor those classes which without proper unit testing can and most likely will open a can of worms.

So how would you go about unit testing large applications? And what other measures would you use to reduce possible bugs from your coding (of course some bugs will probably remain anyway)?

share|improve this question

marked as duplicate by gnat, maple_shaft May 23 '12 at 16:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
I think that is exactly the topic, or one of the topics, of Working Effectively with Legacy Code But I haven't read it, so I cannot be certain. –  Pete May 23 '12 at 9:44
    
Are you sure there are no unit tests? Good programmers always tested often and thoroughly long before "TDD" became a religion. Root around the dev libraries and test servers, you may find a large body of test harnesses and scripts. –  James Anderson May 23 '12 at 10:40
    
@JamesAnderson, granted, it is worth looking around, but I am afraid you may be naive here. Out of more than a dozen real-life projects I have seen so far, only one (see below) had any automated (unit, or whatever sort of) tests at the time I joined :-((( –  Péter Török May 23 '12 at 10:57
    
@James Anderson : fairly certain there are no unit tests . In systems where I work mangers don't give a lot of importance to unit testing as much to manual and black box testing . The code at times would be written decades ago . The app would certainly be tested thoroughly for bugs though (just no unit test) . –  minusSeven May 23 '12 at 10:59
    
add comment

1 Answer

up vote 6 down vote accepted

Covering legacy code with unit tests is a long, difficult, step by step process. But it is doable, and is an absolute must if you are maintaining a product with a long expected lifetime (i.e. more than a year - note that the lifetime of most project significantly extends their originally planned lifetime).

When I joined our current project close to 3 years ago, there existed less than 50 unit test cases and the coverage was below 0,5%. Around the end of last year, we had over 2600 unit tests and coverage was over 36%. (Since then development has been largely stalling.) During the same time, code size went down from 106 to 67 KLOC, and technical debt (as measured by Sonar) from over $513K to $213K - while new functionality was added too.

So yes, it is possible. But only with a management that supports code quality improvement and long term maintainability, and is willing to spend significant time and resources on it.

If you have such a management, my first advice is: run, don't walk, to buy Working Effectively With Legacy Code by Michael Feathers.

Then start adding unit tests. The simplest tactic is to add unit tests before you need to touch any part of the code, to cover the changing part(s) to some reasonable extent. Sometimes you just add a single test case at a time to cover a simple change in a huge class. Sometimes you have more time, so you can construct a more comprehensive test suite to fully cover a bigger method, or even several of them. Keep adding unit tests - the little changes will add up to ever greater sums in the long term.

share|improve this answer
add comment

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