SlideShare a Scribd company logo
1 of 28
Download to read offline
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%!
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

Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)Foyzul Karim
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best PracticesTomaš Maconko
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeExcella
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done RightBrian Fenton
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqXPDays
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on boardRuth Sperer
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven DevelopmentTung Nguyen Thanh
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)nedirtv
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguy_davis
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test PatternsFrank Appel
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionDionatan default
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010guest5639fa9
 

What's hot (20)

Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on board
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Best practices unit testing
Best practices unit testing Best practices unit testing
Best practices unit testing
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 

Similar to Adding Unit Test To Legacy Code

Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...mCloud
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Myeongseok Baek
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtrykapib57390
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguestc8093a6
 
Test Automation Principles
Test Automation PrinciplesTest Automation Principles
Test Automation PrinciplesNetSuite
 
Test-Driven Development
 Test-Driven Development  Test-Driven Development
Test-Driven Development Amir Assad
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Testsradin reth
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentjakubkoci
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flexmichael.labriola
 
Test driven development
Test driven developmentTest driven development
Test driven developmentnamkha87
 
How to complement TDD with static analysis
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysisPVS-Studio
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven DevelopmentMichael Denomy
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012Pietro Di Bello
 
Why Your Selenium Tests are so Dang Brittle, and What to Do About It
Why Your Selenium Tests are so Dang Brittle, and What to Do About ItWhy Your Selenium Tests are so Dang Brittle, and What to Do About It
Why Your Selenium Tests are so Dang Brittle, and What to Do About ItJay Aho
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
Stop fearing legacy code
Stop fearing legacy codeStop fearing legacy code
Stop fearing legacy codeYaki Koren
 
Beyond Testing: Specs and Behavior Driven Development
Beyond Testing: Specs and Behavior  Driven DevelopmentBeyond Testing: Specs and Behavior  Driven Development
Beyond Testing: Specs and Behavior Driven DevelopmentRabble .
 

Similar to Adding Unit Test To Legacy Code (20)

Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Test Driven
Test DrivenTest Driven
Test Driven
 
Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기Ddc2011 효과적으로레거시코드다루기
Ddc2011 효과적으로레거시코드다루기
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Automation Principles
Test Automation PrinciplesTest Automation Principles
Test Automation Principles
 
Test-Driven Development
 Test-Driven Development  Test-Driven Development
Test-Driven Development
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Tests
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
How to complement TDD with static analysis
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysis
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
Why Your Selenium Tests are so Dang Brittle, and What to Do About It
Why Your Selenium Tests are so Dang Brittle, and What to Do About ItWhy Your Selenium Tests are so Dang Brittle, and What to Do About It
Why Your Selenium Tests are so Dang Brittle, and What to Do About It
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Stop fearing legacy code
Stop fearing legacy codeStop fearing legacy code
Stop fearing legacy code
 
Beyond Testing: Specs and Behavior Driven Development
Beyond Testing: Specs and Behavior  Driven DevelopmentBeyond Testing: Specs and Behavior  Driven Development
Beyond Testing: Specs and Behavior Driven Development
 

More from Terry Yin

Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTerry Yin
 
Six Years of Teaching Certified Scrum Developers
Six Years of Teaching Certified Scrum DevelopersSix Years of Teaching Certified Scrum Developers
Six Years of Teaching Certified Scrum DevelopersTerry Yin
 
Abstraction is a communication tool
Abstraction is a communication toolAbstraction is a communication tool
Abstraction is a communication toolTerry Yin
 
No Reuse Before Use
No Reuse Before UseNo Reuse Before Use
No Reuse Before UseTerry Yin
 
Programming exercises
Programming exercisesProgramming exercises
Programming exercisesTerry Yin
 
Misconceptions Of Unit Testing
Misconceptions Of Unit TestingMisconceptions Of Unit Testing
Misconceptions Of Unit TestingTerry Yin
 

More from Terry Yin (6)

Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code Analyzer
 
Six Years of Teaching Certified Scrum Developers
Six Years of Teaching Certified Scrum DevelopersSix Years of Teaching Certified Scrum Developers
Six Years of Teaching Certified Scrum Developers
 
Abstraction is a communication tool
Abstraction is a communication toolAbstraction is a communication tool
Abstraction is a communication tool
 
No Reuse Before Use
No Reuse Before UseNo Reuse Before Use
No Reuse Before Use
 
Programming exercises
Programming exercisesProgramming exercises
Programming exercises
 
Misconceptions Of Unit Testing
Misconceptions Of Unit TestingMisconceptions Of Unit Testing
Misconceptions Of Unit Testing
 

Adding Unit Test To Legacy Code