adding unit test to legacy code
DESCRIPTION
Some experience sharing about adding unit test to legacy code in large embedded system.TRANSCRIPT
![Page 2: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/2.jpg)
AgendaBackgroundMotivationPrinciplesPracticesWhat is fun?
![Page 3: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/3.jpg)
BACKGROUND
![Page 4: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/4.jpg)
Large scale software Telecommunication device10+ years old1,000 people10,000,000 lines of code in one buildC, SDLSome very complicatedThousands LOCs functionsCyclomatic complexity > 50.Duplicate rate > 100%!
![Page 5: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/5.jpg)
![Page 6: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/6.jpg)
Where do we usually call it legacy code?
![Page 7: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/7.jpg)
MOTIVATION
![Page 8: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/8.jpg)
It's NOT about finding bugs!The purpose of unit testing is to facilitate changes
![Page 9: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/9.jpg)
not to do it.You still have the choice and reasons
![Page 10: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/10.jpg)
PRINCIPLES
![Page 11: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/11.jpg)
BASIC UNIT TESTING PRINCIPLES
![Page 12: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/12.jpg)
PRINCIPLE 1KEEP IT VERY SIMPLE, VERY STUPID
It is not that the more test cases the betterActually, it is on the contrary, the less the better. The purpose of UT is to facilitate change
It can only facilitate change if it surviveTherefore, it needs maintainabilitySo, it needs to be simple
"The only way for humans to deal with complexity is to avoid it ..."
![Page 13: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/13.jpg)
PRINCIPLE 2 DON'T TRY TO ADD GOOD UT TO BAD CODE
![Page 14: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/14.jpg)
PRINCIPLE 3DON'T MAKE ASSUMPTION
error_t release_message_received( msg_header_t * msg ) { DO_FAIL_TEXT("I'm called!!"); return SUCCESS_EC;}
Exploding stub
![Page 15: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/15.jpg)
PRINCIPLE 4 STOP MAKING 'LEGACY CODE'
Test drive new codeAdd tests to legacy code before modificationTest drive changes to legacy code
Boy Scout Principle
![Page 16: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/16.jpg)
PRINCIPLE 5EDUCATE THE PEOPLE
Do NOT let just one or two people do it.NEVER let interns do it!
![Page 17: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/17.jpg)
WHERE TO START?
![Page 18: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/18.jpg)
Setup the frameworkTo setup the framework for legacy code can be very challenging.Choose the test frameworkWe use CppUTest Ask for performance
![Page 19: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/19.jpg)
Domain ModelingReverse engineering to clarify the concepts used in the legacy code
And their relationshipsUse the terms consistently in your unit testing.It will also give your refactoring a road-map.
![Page 20: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/20.jpg)
Identify the hot areaStart from the hot area will be most cost-efficientExample
Through SVN logAlong with the new work and bug fixing
![Page 21: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/21.jpg)
Bottom-up?Have some integration test firstThen,
One practical approach is bottom-upGet a higher level of abstraction
![Page 22: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/22.jpg)
Learn the function by testing it
Characterization TestStart from the 1st (failing) exitWrite your plan on a piece of paper
![Page 23: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/23.jpg)
Make the legacy code testable
Use safe refactoring techniques to change the legacy code without unit testing.
Extract functionIf you are using C
‘Data injection’ to break the dependency on globals.
![Page 24: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/24.jpg)
Break Dependency
![Page 25: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/25.jpg)
WHAT IS FUN
![Page 26: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/26.jpg)
Bring chaos to orderLearn the featureDelete tons of codeFeel the peace of your heart
![Page 27: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/27.jpg)
REFERENCES
![Page 28: Adding Unit Test To Legacy Code](https://reader035.vdocuments.us/reader035/viewer/2022081413/547ba17eb37959582b8b4dd8/html5/thumbnails/28.jpg)
Acknowledgement