28

Terry yin adding unit-test_to_legacy_code

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 2: Terry yin   adding unit-test_to_legacy_code

AgendaBackgroundMotivationPrinciplesPracticesWhat is fun?

Page 3: Terry yin   adding unit-test_to_legacy_code

BACKGROUND

Page 4: Terry yin   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: Terry yin   adding unit-test_to_legacy_code
Page 6: Terry yin   adding unit-test_to_legacy_code

Where do we usually call it legacy code?

Page 7: Terry yin   adding unit-test_to_legacy_code

MOTIVATION

Page 8: Terry yin   adding unit-test_to_legacy_code

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

Page 9: Terry yin   adding unit-test_to_legacy_code

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

Page 10: Terry yin   adding unit-test_to_legacy_code

PRINCIPLES

Page 11: Terry yin   adding unit-test_to_legacy_code

BASIC UNIT TESTING PRINCIPLES

Page 12: Terry yin   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: Terry yin   adding unit-test_to_legacy_code

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

Page 14: Terry yin   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: Terry yin   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: Terry yin   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: Terry yin   adding unit-test_to_legacy_code

WHERE TO START?

Page 18: Terry yin   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: Terry yin   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: Terry yin   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: Terry yin   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: Terry yin   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: Terry yin   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: Terry yin   adding unit-test_to_legacy_code

Break Dependency

Page 25: Terry yin   adding unit-test_to_legacy_code

WHAT IS FUN

Page 26: Terry yin   adding unit-test_to_legacy_code

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

Page 27: Terry yin   adding unit-test_to_legacy_code

REFERENCES

Page 28: Terry yin   adding unit-test_to_legacy_code

Acknowledgement