Testing Legacy Code

Testing Legacy Code

Elliotte Rusty Harold

Software Development Best Practices 2006

Thursday, September 14, 2006



What is Legacy Code?

Benefits of Test Driven Development are Well Known

But Not All Code Starts From a Clean Slate

How many of you have seen a comment like this?

/* I have no idea how this works but it seems to.  Whatever you  
   do, don't touch this function, and don't break this code! */

Entire systems are sometimes ruled off-limits because no one understands them; yet these systems still need to be maintained.

Old Code Must be Maintained

Can We Apply Test First Techniques When

The Answer is Yes, We Can

You Need To Give Up a Few Things

Give Up #1: 100% Test Coverage

Give Up #2: Unit Tests

Give Up #3: Frequent context switches

What TDD ideas do we keep?

Some important aspects of test driven development can apply to legacy testing:

What technology do we keep?

Getting Started

Testing main() method

Add fixtures

What Tests do you Already Have?

Converting Existing Tests

Characterization Testing

Testing by Functional Division

What to Test?

Testing by Code Structure

Top Down

Code Coverage Measurements?

Code Coverage Tools

How code coverage works

A Code Coverage Report

Sample code coverage report


Package Level Coverage

Line Level Coverage

Auto generating tests

Static Analysis

Don't neglect static analysis though. It will find real bugs you haven't noticed.

Test or debug?

How to decide


Summing Up

To Learn More

Index | Cafe con Leche

Copyright 2005 Elliotte Rusty Harold
Last Modified June 29, 2006