SlideShare a Scribd company logo
terry.yinzhe@gmail.com
Agenda
 Background
 Motivation
 Principles
 Practices
 What is fun?
BACKGROUND
Large scale software
Telecommunication device
10+ years old
1,000 people
10,000,000 lines of code in one build
C, SDL
Some very complicated
Thousands LOCs functions
Cyclomatic complexity > 50.
Duplicate rate > 100%!
Adding Unit Test To Legacy Code
Where do we usually call it legacy code?
MOTIVATION
It's NOT about finding bugs!
The purpose of unit testing is to facilitate
changes
You still have the choice and reasons
          not to do it.
PRINCIPLES
BASIC UNIT TESTING
PRINCIPLES
PRINCIPLE 1
KEEP IT VERY SIMPLE, VERY STUPID



   It is not that the more test cases the better
       Actually, it is on the contrary, the less the better.
        The purpose of UT is to facilitate change
          It can only facilitate change if it survive
              Therefore, it needs maintainability
              So, it needs to be simple
  "The only way for humans to deal with complexity is to
  avoid it ..."
PRINCIPLE 2
DON'T TRY TO ADD GOOD UT TO BAD CODE
PRINCIPLE 3
DON'T MAKE ASSUMPTION




             Exploding stub

      error_t release_message_received(
         msg_header_t * msg ) {
         DO_FAIL_TEXT("I'm called!!");
         return SUCCESS_EC;
      }
PRINCIPLE 4
STOP MAKING 'LEGACY CODE'



  Test drive new code
  Add tests to legacy code before modification
  Test drive changes to legacy code

  Boy Scout Principle
PRINCIPLE 5
EDUCATE THE PEOPLE



  Do NOT let just one or two people do it.
  NEVER let interns do it!
WHERE TO START?
Setup the framework
 To setup the framework for legacy
 code can be very challenging.
 Choose the test framework
 We use CppUTest
  Ask for performance
Domain Modeling
 Reverse engineering to clarify the
 concepts used in the legacy code
   And their relationships
 Use the terms consistently in your
 unit testing.
 It will also give your refactoring
 a road-map.
Identify the hot area
 Start from the hot area will be most
 cost-efficient
 Example
   Through SVN log
   Along with the new work and bug fixing
Bottom-up?
 Have some integration test first
 Then,
   One practical approach is bottom-up
   Get a higher level of abstraction
Learn the function by testing it
  Characterization Test
  Start from the 1st (failing) exit
  Write your plan on a piece of paper
Make the legacy code testable
 Use safe refactoring techniques to
 change the legacy code without unit
 testing.
   Extract function
   If you are using C
     ‘Data injection’ to break the dependency on
     globals.
Break Dependency
WHAT IS FUN
Bring chaos to order
Learn the feature
Delete tons of code
Feel the peace of your heart
REFERENCES
Acknowledgement

More Related Content

What's hot (20)

PPTX
Unit Testing Concepts and Best Practices
Derek Smith
 
PPTX
An Introduction to Unit Testing
Joe Tremblay
 
PDF
Unit testing (workshop)
Foyzul Karim
 
PDF
Unit Testing Best Practices
Tomaš Maconko
 
PPTX
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Rob Hale
 
PPTX
Working Effectively With Legacy Code
Excella
 
PDF
Unit Testing Done Right
Brian Fenton
 
PPTX
Understanding Unit Testing
ikhwanhayat
 
PPTX
Unit Testing in Action - C#, NUnit, and Moq
XPDays
 
PPTX
Clean code - Getting your R&D on board
Ruth Sperer
 
PPTX
TDD - Test Driven Development
Tung Nguyen Thanh
 
PDF
Unit Test + Functional Programming = Love
Alvaro Videla
 
PPT
TDD (Test Driven Design)
nedirtv
 
PDF
An Introduction to Test Driven Development
CodeOps Technologies LLP
 
PPTX
Unit Testing And Mocking
Joe Wilson
 
ZIP
Test Driven Development
guy_davis
 
PDF
Clean Unit Test Patterns
Frank Appel
 
PPTX
Best practices unit testing
Tricode (part of Dept)
 
PDF
TDD Flow: The Mantra in Action
Dionatan default
 
PPTX
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
 
Unit Testing Concepts and Best Practices
Derek Smith
 
An Introduction to Unit Testing
Joe Tremblay
 
Unit testing (workshop)
Foyzul Karim
 
Unit Testing Best Practices
Tomaš Maconko
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Rob Hale
 
Working Effectively With Legacy Code
Excella
 
Unit Testing Done Right
Brian Fenton
 
Understanding Unit Testing
ikhwanhayat
 
Unit Testing in Action - C#, NUnit, and Moq
XPDays
 
Clean code - Getting your R&D on board
Ruth Sperer
 
TDD - Test Driven Development
Tung Nguyen Thanh
 
Unit Test + Functional Programming = Love
Alvaro Videla
 
TDD (Test Driven Design)
nedirtv
 
An Introduction to Test Driven Development
CodeOps Technologies LLP
 
Unit Testing And Mocking
Joe Wilson
 
Test Driven Development
guy_davis
 
Clean Unit Test Patterns
Frank Appel
 
Best practices unit testing
Tricode (part of Dept)
 
TDD Flow: The Mantra in Action
Dionatan default
 
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
 

Similar to Adding Unit Test To Legacy Code (20)

PPTX
[DevDay2018] Unit testing in PHP and Laravel Framework - Unit testing in PHP ...
DevDay Da Nang
 
PDF
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
Agile đây Vietnam
 
PDF
Workshop fight legacy code write unit test
Tung Nguyen Thanh
 
KEY
Unit Testing Your Application
Paladin Web Services
 
PPT
Google test training
Thierry Gayet
 
PPS
Why Unit Testingl
priya_trivedi
 
PPS
Why Unit Testingl
priya_trivedi
 
PPS
Why unit testingl
Priya Sharma
 
PPTX
Testing, a pragmatic approach
Enrico Da Ros
 
PPTX
An Introduction to Developer Testing
Will Green
 
PDF
Driving Quality with TDD
Steven Mak
 
PDF
Let's test!
Andrea Giuliano
 
PDF
DSR Testing (Part 1)
Steve Upton
 
PPTX
Best pratice
Eugenio Romano
 
PDF
Unit testing - An introduction
Alejandro Claro Mosqueda
 
KEY
Client Side Unit Testing
cloud chen
 
PDF
Test Drive Development
satya sudheer
 
PDF
Into The Box 2018 | Assert control over your legacy applications
Ortus Solutions, Corp
 
PPTX
Software Testing_A_mmmmmmmmmmmmmmmmmmmmm
IwannatelluAstorylas
 
PDF
Real Developers Don't Need Unit Tests
John Ferguson Smart Limited
 
[DevDay2018] Unit testing in PHP and Laravel Framework - Unit testing in PHP ...
DevDay Da Nang
 
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
Agile đây Vietnam
 
Workshop fight legacy code write unit test
Tung Nguyen Thanh
 
Unit Testing Your Application
Paladin Web Services
 
Google test training
Thierry Gayet
 
Why Unit Testingl
priya_trivedi
 
Why Unit Testingl
priya_trivedi
 
Why unit testingl
Priya Sharma
 
Testing, a pragmatic approach
Enrico Da Ros
 
An Introduction to Developer Testing
Will Green
 
Driving Quality with TDD
Steven Mak
 
Let's test!
Andrea Giuliano
 
DSR Testing (Part 1)
Steve Upton
 
Best pratice
Eugenio Romano
 
Unit testing - An introduction
Alejandro Claro Mosqueda
 
Client Side Unit Testing
cloud chen
 
Test Drive Development
satya sudheer
 
Into The Box 2018 | Assert control over your legacy applications
Ortus Solutions, Corp
 
Software Testing_A_mmmmmmmmmmmmmmmmmmmmm
IwannatelluAstorylas
 
Real Developers Don't Need Unit Tests
John Ferguson Smart Limited
 
Ad

More from Terry Yin (6)

PDF
Test Driven Development of A Static Code Analyzer
Terry Yin
 
PDF
Six Years of Teaching Certified Scrum Developers
Terry Yin
 
PDF
Abstraction is a communication tool
Terry Yin
 
PDF
No Reuse Before Use
Terry Yin
 
PDF
Programming exercises
Terry Yin
 
PDF
Misconceptions Of Unit Testing
Terry Yin
 
Test Driven Development of A Static Code Analyzer
Terry Yin
 
Six Years of Teaching Certified Scrum Developers
Terry Yin
 
Abstraction is a communication tool
Terry Yin
 
No Reuse Before Use
Terry Yin
 
Programming exercises
Terry Yin
 
Misconceptions Of Unit Testing
Terry Yin
 
Ad

Adding Unit Test To Legacy Code

  • 2. Agenda Background Motivation Principles Practices What is fun?
  • 4. Large scale software Telecommunication device 10+ years old 1,000 people 10,000,000 lines of code in one build C, SDL Some very complicated Thousands LOCs functions Cyclomatic complexity > 50. Duplicate rate > 100%!
  • 6. Where do we usually call it legacy code?
  • 8. It's NOT about finding bugs! The purpose of unit testing is to facilitate changes
  • 9. You still have the choice and reasons not to do it.
  • 12. PRINCIPLE 1 KEEP IT VERY SIMPLE, VERY STUPID It is not that the more test cases the better Actually, it is on the contrary, the less the better. The purpose of UT is to facilitate change It can only facilitate change if it survive Therefore, it needs maintainability So, it needs to be simple "The only way for humans to deal with complexity is to avoid it ..."
  • 13. PRINCIPLE 2 DON'T TRY TO ADD GOOD UT TO BAD CODE
  • 14. PRINCIPLE 3 DON'T MAKE ASSUMPTION Exploding stub error_t release_message_received( msg_header_t * msg ) { DO_FAIL_TEXT("I'm called!!"); return SUCCESS_EC; }
  • 15. PRINCIPLE 4 STOP MAKING 'LEGACY CODE' Test drive new code Add tests to legacy code before modification Test drive changes to legacy code Boy Scout Principle
  • 16. PRINCIPLE 5 EDUCATE THE PEOPLE Do NOT let just one or two people do it. NEVER let interns do it!
  • 18. Setup the framework To setup the framework for legacy code can be very challenging. Choose the test framework We use CppUTest Ask for performance
  • 19. Domain Modeling Reverse engineering to clarify the concepts used in the legacy code And their relationships Use the terms consistently in your unit testing. It will also give your refactoring a road-map.
  • 20. Identify the hot area Start from the hot area will be most cost-efficient Example Through SVN log Along with the new work and bug fixing
  • 21. Bottom-up? Have some integration test first Then, One practical approach is bottom-up Get a higher level of abstraction
  • 22. Learn the function by testing it Characterization Test Start from the 1st (failing) exit Write your plan on a piece of paper
  • 23. Make the legacy code testable Use safe refactoring techniques to change the legacy code without unit testing. Extract function If you are using C ‘Data injection’ to break the dependency on globals.
  • 26. Bring chaos to order Learn the feature Delete tons of code Feel the peace of your heart