Adding Unit Test To Legacy Code

28

description

Some experience sharing about adding unit test to legacy code in large embedded system.

Transcript of Adding Unit Test To Legacy Code

Page 2: Adding Unit Test To Legacy Code

AgendaBackgroundMotivationPrinciplesPracticesWhat is fun?

Page 3: Adding Unit Test To Legacy Code

BACKGROUND

Page 4: Adding Unit Test To Legacy Code

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
Page 6: Adding Unit Test To Legacy Code

Where do we usually call it legacy code?

Page 7: Adding Unit Test To Legacy Code

MOTIVATION

Page 8: Adding Unit Test To Legacy Code

It's NOT about finding bugs!The purpose of unit testing is to facilitate changes

Page 9: Adding Unit Test To Legacy Code

not to do it.You still have the choice and reasons

Page 10: Adding Unit Test To Legacy Code

PRINCIPLES

Page 11: Adding Unit Test To Legacy Code

BASIC UNIT TESTING PRINCIPLES

Page 12: Adding Unit Test To Legacy Code

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

PRINCIPLE 2 DON'T TRY TO ADD GOOD UT TO BAD CODE

Page 14: Adding Unit Test To Legacy Code

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

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

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

WHERE TO START?

Page 18: Adding Unit Test To Legacy Code

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

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

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

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

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

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

Break Dependency

Page 25: Adding Unit Test To Legacy Code

WHAT IS FUN

Page 26: Adding Unit Test To Legacy Code

Bring chaos to orderLearn the featureDelete tons of codeFeel the peace of your heart

Page 27: Adding Unit Test To Legacy Code

REFERENCES

Page 28: Adding Unit Test To Legacy Code

Acknowledgement