Generatingcharacterizationtestsforlegacycode
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Generatingcharacterizationtestsforlegacycode

on

  • 149 views

 

Statistics

Views

Total Views
149
Views on SlideShare
149
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Generatingcharacterizationtestsforlegacycode Presentation Transcript

  • 1. Generating Characterization Testsfor Legacy CodeSlides via Jonas Follesø (@follesoe)
  • 2. Huge methods (~3000+ lines)
  • 3. Dave&Karinhttp://www.flickr.com/photos/dnk_uk/3525103502/~50 slowintegration tests
  • 4. How thedevelopmentteam felt...
  • 5. What theyneeded
  • 6. What youshould read
  • 7. FraserSpeirshttp://www.flickr.com/photos/fraserspeirs/3395595360/Legacy code is code without tests.Code without tests is bad code.-Michael C. Feathers
  • 8. A characterization test is test thatcharacterizes the actual behavior of a piece ofcode.It acts as a change detector, protecting legacycode from unintended changes
  • 9. public double Calc(double inv, double rt, int y){double ret = 0;for (int i = 1; i <= y; i++){ret = inv * Math.Pow(1.0 + rt / 100.0, i);}return ret;}
  • 10. [TestMethod]public void Calc_characterization(){var calc = new CalcUtil();double result = calc.Calc(10000, 10, 10);Assert.AreEqual(42.0, result);}
  • 11. Assert.AreEqual failed.Expected:<42>. Actual:<25937.424601>.
  • 12. [TestMethod]public void Calc_characterization(){var calc = new CalcUtil();double result = calc.Calc(10000, 10, 10);Assert.AreEqual(25937.424601, result);}
  • 13. Test run completed. Results 1/1 passed.
  • 14. public double CalculateCompoundInterest(double investment, double interest, int years){double projectedValue = 0.0;for (int year = 1; year <= years; year++){projectedValue = investment *Math.Pow(1.0 + interest / 100.0, year);}return projectedValue;}
  • 15. FraserSpeirshttp://www.flickr.com/photos/fraserspeirs/3395599536/…A pinch point is a natural encapsulationboundary. When you find a pinch point, you’vefound a narrow funnel for all the effects of a largepiece of code…-Michael C. Feathers
  • 16. fast characterization tests
  • 17. The Golden Master ApproachBefore making any change to production code, do the following:1) Create X number of random inputs, always using the same random seed, soyou can always generate the same set over and over again. You will probablywant LOTS of inputs for good coverage.2) Bombard the class or system under test with these random inputs.3) Capture the outputs for each individual random input.
  • 18. A pictures worth a 1000 tests.Unit testing asserts can be difficult to use.Approval tests simplify this by taking asnapshot of the results, and confirming thatthey have not changed.