SlideShare une entreprise Scribd logo
1  sur  18
Generating Characterization Tests
for Legacy Code
Slides via Jonas Follesø (@follesoe)
Huge methods (~3000+ lines)
Dave&Karinhttp://www.flickr.com/photos/dnk_uk/3525103502/
~50 slow
integration tests
How the
development
team felt...
What they
needed
What you
should read
FraserSpeirshttp://www.flickr.com/photos/fraserspeirs/3395595360/
Legacy code is code without tests.
Code without tests is bad code.
-Michael C. Feathers
A characterization test is test that
characterizes the actual behavior of a piece of
code.
It acts as a change detector, protecting legacy
code from unintended changes
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;
}
[TestMethod]
public void Calc_characterization()
{
var calc = new CalcUtil();
double result = calc.Calc(10000, 10, 10);
Assert.AreEqual(42.0, result);
}
Assert.AreEqual failed.
Expected:<42>. Actual:<25937.424601>.
[TestMethod]
public void Calc_characterization()
{
var calc = new CalcUtil();
double result = calc.Calc(10000, 10, 10);
Assert.AreEqual(25937.424601, result);
}
Test run completed. Results 1/1 passed.
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;
}
FraserSpeirshttp://www.flickr.com/photos/fraserspeirs/3395599536/
…A pinch point is a natural encapsulation
boundary. When you find a pinch point, you’ve
found a narrow funnel for all the effects of a large
piece of code…
-Michael C. Feathers
fast characterization tests
The Golden Master Approach
Before making any change to production code, do the following:
1) Create X number of random inputs, always using the same random seed, so
you can always generate the same set over and over again. You will probably
want 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.
A picture's worth a 1000 tests.
Unit testing asserts can be difficult to use.
Approval tests simplify this by taking a
snapshot of the results, and confirming that
they have not changed.

Contenu connexe

En vedette

ขั้นตอนการทำโครงงานคอมพิวเตอร์
ขั้นตอนการทำโครงงานคอมพิวเตอร์ขั้นตอนการทำโครงงานคอมพิวเตอร์
ขั้นตอนการทำโครงงานคอมพิวเตอร์ณัชชา บัวผัด
 
Content marketing slideshow np os-v3
Content marketing slideshow np os-v3Content marketing slideshow np os-v3
Content marketing slideshow np os-v3Bob Duffy
 
Genetic adaptation to__organic
Genetic adaptation to__organicGenetic adaptation to__organic
Genetic adaptation to__organicاسماء نصر
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome EconomyHelge Tennø
 

En vedette (6)

ขั้นตอนการทำโครงงานคอมพิวเตอร์
ขั้นตอนการทำโครงงานคอมพิวเตอร์ขั้นตอนการทำโครงงานคอมพิวเตอร์
ขั้นตอนการทำโครงงานคอมพิวเตอร์
 
Content marketing slideshow np os-v3
Content marketing slideshow np os-v3Content marketing slideshow np os-v3
Content marketing slideshow np os-v3
 
Genetic adaptation to__organic
Genetic adaptation to__organicGenetic adaptation to__organic
Genetic adaptation to__organic
 
14
1414
14
 
Case s 1
Case s 1Case s 1
Case s 1
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome Economy
 

Similaire à Generatingcharacterizationtestsforlegacycode

Generating characterization tests for legacy code
Generating characterization tests for legacy codeGenerating characterization tests for legacy code
Generating characterization tests for legacy codeJonas Follesø
 
Aaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security TeamsAaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security Teamscentralohioissa
 
Mutation testing for a safer Future
Mutation testing for a safer FutureMutation testing for a safer Future
Mutation testing for a safer FutureCocoaHeads France
 
Software Testing:
 A Research Travelogue 
(2000–2014)
Software Testing:
 A Research Travelogue 
(2000–2014)Software Testing:
 A Research Travelogue 
(2000–2014)
Software Testing:
 A Research Travelogue 
(2000–2014)Alex Orso
 
Machine Learning Model Bakeoff
Machine Learning Model BakeoffMachine Learning Model Bakeoff
Machine Learning Model Bakeoffmrphilroth
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and ProsperKen Kousen
 
Agile Developer Immersion Workshop, LASTconf Melbourne, Australia, 19th July ...
Agile Developer Immersion Workshop, LASTconf Melbourne, Australia, 19th July ...Agile Developer Immersion Workshop, LASTconf Melbourne, Australia, 19th July ...
Agile Developer Immersion Workshop, LASTconf Melbourne, Australia, 19th July ...Victoria Schiffer
 
(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and backDavid Rodenas
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!Ortus Solutions, Corp
 
White-box Unit Test Generation with Microsoft IntelliTest
White-box Unit Test Generation with Microsoft IntelliTestWhite-box Unit Test Generation with Microsoft IntelliTest
White-box Unit Test Generation with Microsoft IntelliTestDávid Honfi
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxVictor Rentea
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testingpleeps
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionAlex Su
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingIevgenii Katsan
 
Python mocking intro
Python mocking introPython mocking intro
Python mocking introHans Jones
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 

Similaire à Generatingcharacterizationtestsforlegacycode (20)

Generating characterization tests for legacy code
Generating characterization tests for legacy codeGenerating characterization tests for legacy code
Generating characterization tests for legacy code
 
Aaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security TeamsAaron Bedra - Effective Software Security Teams
Aaron Bedra - Effective Software Security Teams
 
Mutation testing for a safer Future
Mutation testing for a safer FutureMutation testing for a safer Future
Mutation testing for a safer Future
 
Software Testing:
 A Research Travelogue 
(2000–2014)
Software Testing:
 A Research Travelogue 
(2000–2014)Software Testing:
 A Research Travelogue 
(2000–2014)
Software Testing:
 A Research Travelogue 
(2000–2014)
 
Machine Learning Model Bakeoff
Machine Learning Model BakeoffMachine Learning Model Bakeoff
Machine Learning Model Bakeoff
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and Prosper
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Agile Developer Immersion Workshop, LASTconf Melbourne, Australia, 19th July ...
Agile Developer Immersion Workshop, LASTconf Melbourne, Australia, 19th July ...Agile Developer Immersion Workshop, LASTconf Melbourne, Australia, 19th July ...
Agile Developer Immersion Workshop, LASTconf Melbourne, Australia, 19th July ...
 
Mutation @ Spotify
Mutation @ Spotify Mutation @ Spotify
Mutation @ Spotify
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back(automatic) Testing: from business to university and back
(automatic) Testing: from business to university and back
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
White-box Unit Test Generation with Microsoft IntelliTest
White-box Unit Test Generation with Microsoft IntelliTestWhite-box Unit Test Generation with Microsoft IntelliTest
White-box Unit Test Generation with Microsoft IntelliTest
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptx
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
Ruslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testingRuslan Shevchenko - Property based testing
Ruslan Shevchenko - Property based testing
 
Getting Started With Testing
Getting Started With TestingGetting Started With Testing
Getting Started With Testing
 
Python mocking intro
Python mocking introPython mocking intro
Python mocking intro
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 

Generatingcharacterizationtestsforlegacycode