adding unit test to legacy code

Post on 29-Nov-2014

1.591 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

terry.yinzhe@gmail.com

AgendaBackgroundMotivationPrinciplesPracticesWhat is fun?

BACKGROUND

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%!

Where do we usually call it legacy code?

MOTIVATION

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

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

PRINCIPLES

BASIC UNIT TESTING PRINCIPLES

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 ..."

PRINCIPLE 2 DON'T TRY TO ADD GOOD UT TO BAD 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

PRINCIPLE 4 STOP MAKING 'LEGACY CODE'

Test drive new codeAdd tests to legacy code before modificationTest drive changes to legacy code

Boy Scout Principle

PRINCIPLE 5EDUCATE THE PEOPLE

Do NOT let just one or two people do it.NEVER let interns do it!

WHERE TO START?

Setup the frameworkTo setup the framework for legacy code can be very challenging.Choose the test frameworkWe use CppUTest Ask for performance

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.

Identify the hot areaStart from the hot area will be most cost-efficientExample

Through SVN logAlong with the new work and bug fixing

Bottom-up?Have some integration test firstThen,

One practical approach is bottom-upGet a higher level of abstraction

Learn the function by testing it

Characterization TestStart from the 1st (failing) exitWrite 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 functionIf you are using C

‘Data injection’ to break the dependency on globals.

Break Dependency

WHAT IS FUN

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

REFERENCES

Acknowledgement

top related