Testing Legacy Code

Elliotte Rusty Harold

Architecture & Design 2007

Wednesday, July 25, 2007

elharo@metalab.unc.edu

http://www.cafeaulait.org/


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

Identification and Visualization of the Sphenomandibularis Muscle in the Visible Human Male and Female Data Sets; Gary D. Hack (1), Peter Ratiu (2), John P. Kerr (2), Gwendolyn F. Dunn (3), Michael Rothman, Univ of Maryland and EAI


Top Down


Code Coverage Measurements?


Code Coverage Tools


How code coverage works


A Code Coverage Report

Sample code coverage report

http://www.cafeconleche.org/cobertura-jaxen/


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


Refactoring


Summing Up


To Learn More