Tải bản đầy đủ - 0 (trang)
Chapter 6. Framework for Integrated Test: Beauty Through Fragility

Chapter 6. Framework for Integrated Test: Beauty Through Fragility

Tải bản đầy đủ - 0trang

6.FrameworkforIntegratedTest:

BeautyThroughFragility

MichaelFeathers

Ihavesomeideasaboutwhatgooddesignis.Everyprogrammerdoes.We



alldeveloptheseideasthroughpractice,andwedrawonthem

aswework.Ifwe'retemptedtouseapublicvariableinaclass,

werememberthatpublicvariablesareusuallyasymptomof

baddesign,andifweseeimplementationinheritance,we

rememberthatweshouldpreferdelegationtoinheritance.[*]

[*]DesignPatterns:ElementsofReusableObject-OrientedSoftware,Erich



Gamma,RichardHelm,RalphJohnson,andJohnVlissides,Addison-Wesley,

1995.



Rulesliketheseareuseful.Theyhelpusmoveourwaythrough

thedesignspaceaswework,butwedoourselvesadisservice

ifweforgetthattheyarejustrulesofthumb.Ifweforget,we

canendupwithdesignwhereweare"doingeverything"right,

butwestillmissthemark.

Thesethoughtsweredrivenhometomebackin2002when

WardCunninghamreleasedFrameworkforIntegratedTest

(FIT),hisautomatedtestingframework.FITconsistsofasmall

setofelegantJavaclasses.Theymaneuverinapatharound

nearlyeveryruleofthumbaboutdesignintheJavacommunity,

andeachlittleturnthattheymakeiscompelling.Theystandin

starkcontrasttodesignthatjustfollowstherules.

Tome,FITisbeautifulcode.It'saninvitationtothinkaboutthe

contextualnatureofdesign.

Inthischapter,I'llwalkthroughoneoftheearliestreleased

versionsofFIT.I'llshowhowFITdeviatesfrommuchofthe

currentacceptedwisdomofJavaandOOframework

development,anddescribehowFITchallengedmeto

reconsidersomeofmydeeplyheldpreconceptionsabout



design.Idon'tknowwhetheryou'llreconsideryoursafter

readingthischapter,butIinviteyoutolookjustthesame.With

luck,I'llbeabletoexpresswhatmakesFIT'sdesignspecial.



6.1.AnAcceptanceTestingFramework

inThreeClasses

FITisrelativelysimpletoexplain.It'salittleframeworkthat

letsyouwriteexecutableapplicationtestsinHTMLtables.Each

typeoftableisprocessedbyaprogrammer-definedclasscalled

afixture.WhentheframeworkprocessesapageofHTML,it

createsafixtureobjectforeachtableinthepage.Thefixture

usesthetableasinputtovalidationcodeofyourchoice:it

readscellvalues,communicateswithyourapplication,checks

expectedvalues,andmarkscellsgreenorredtoindicate

successorfailureofacheck.

Thefirstcellinthetablespecifiesthenameofthefixtureclass

thatwillbeusedtoprocessthetable.Forinstance,Figure6-1

showsatablethatwillbeprocessedbytheMarketEvaluation

fixture.Figure6-2showsthesametableafterFIThas

processedit;onscreen,theshadedcellswouldberedtoshowa

validationfailure.

Figure6-1.HTMLtabledisplayedbeforeFITprocessing



Figure6-2.HTMLtabledisplayedafterFITprocessing



ThekeyideabehindFITisthatdocumentscanserveastests.

Youcould,forinstance,embedtablesinarequirements

documentandrunthedocumentthroughFITtoseewhether

thebehaviorspecifiedinthosetablesexistsinyoursoftware.

ThesedocumentswithtablescanbewrittendirectlyinHTML,or

theycanbewritteninMicrosoftWordoranyotherapplication

thatcansavedocumentsasHTML.BecauseaFITfixtureisjust

apieceofsoftware,itcancallanyportionofanapplicationyou

caretotest,andmakethosecallsatanylevel.It'sallunder

yourcontrolasaprogrammer.

Iwon'tspendanymoretimeexplainingFITanditsproblem

domain;there'smoreinformationontheFITwebsite

(http://fit.c2.com).ButIdowanttodescribethedesignofFIT

andsomeoftheinterestingchoicesitembodies.

ThecoreofFITisonlythreeclasses:Parse,Fixture,and

TypeAdapter.Theirfieldsandmethods,andtherelationships

betweenthem,areshowninFigure6-3.

Figure6-3.RelationsamongFITclasses



Let'swalkthroughit.

Inanutshell,theParseclassrepresentstheHTMLofa

document.TheconstructorofParseacceptsastringand

recursivelyconstructsatreeofParseobjects,knittogether

usingthefieldspartsandmore.EachParseobjectrepresents

someportionofthedocument:there'sanindividualParsefor

eachtable,row,andcell.

TheFixtureclasstraversesthetreeofparses,and

TypeAdapterconvertstestingvalues(numerics,dates,etc.)to

textandbackagain.Fixturestalktotheapplicationyouare

testingandmarkindividualcellsredorgreenifacheckpasses

orfails.

MostoftheworkinFIThappensinsubclassesoftheFixture

class.FixturesdefinetheformatoftheHTMLtablesthey

interpret.Ifyouwanttocreateatablethatconsistsof,say,a

seriesofcommandstoexecuteagainstyourapplication,you

usethepredefinedActionFixtureclass.Ifyouwanttoquery



yourapplicationformultipleresultsandcomparethemagainst

asetofexpectedvalues,youusetheRowFixtureclass.FIT

providesthesesimplesubclasses,butitalsoallowsyouto

subclassFixtureyourself.

FITisausefulframework.Iuseitoften,andI'mcontinually

amazedatwhatyoucandowiththatcoreofthreeclasses.

Manyframeworkswouldtakethreeorfourtimesasmany

classestodothesameamountofwork.







FrameworkforIntegratedTest:BeautyThroughFragility

>AnAcceptanceTestingFrameworkinThreeClasses



6.FrameworkforIntegratedTest:

BeautyThroughFragility

MichaelFeathers

Ihavesomeideasaboutwhatgooddesignis.Everyprogrammerdoes.We



alldeveloptheseideasthroughpractice,andwedrawonthem

aswework.Ifwe'retemptedtouseapublicvariableinaclass,

werememberthatpublicvariablesareusuallyasymptomof

baddesign,andifweseeimplementationinheritance,we

rememberthatweshouldpreferdelegationtoinheritance.[*]

[*]DesignPatterns:ElementsofReusableObject-OrientedSoftware,Erich



Gamma,RichardHelm,RalphJohnson,andJohnVlissides,Addison-Wesley,

1995.



Rulesliketheseareuseful.Theyhelpusmoveourwaythrough

thedesignspaceaswework,butwedoourselvesadisservice

ifweforgetthattheyarejustrulesofthumb.Ifweforget,we

canendupwithdesignwhereweare"doingeverything"right,

butwestillmissthemark.

Thesethoughtsweredrivenhometomebackin2002when

WardCunninghamreleasedFrameworkforIntegratedTest

(FIT),hisautomatedtestingframework.FITconsistsofasmall

setofelegantJavaclasses.Theymaneuverinapatharound

nearlyeveryruleofthumbaboutdesignintheJavacommunity,

andeachlittleturnthattheymakeiscompelling.Theystandin

starkcontrasttodesignthatjustfollowstherules.

Tome,FITisbeautifulcode.It'saninvitationtothinkaboutthe

contextualnatureofdesign.

Inthischapter,I'llwalkthroughoneoftheearliestreleased

versionsofFIT.I'llshowhowFITdeviatesfrommuchofthe

currentacceptedwisdomofJavaandOOframework

development,anddescribehowFITchallengedmeto

reconsidersomeofmydeeplyheldpreconceptionsabout



design.Idon'tknowwhetheryou'llreconsideryoursafter

readingthischapter,butIinviteyoutolookjustthesame.With

luck,I'llbeabletoexpresswhatmakesFIT'sdesignspecial.



6.1.AnAcceptanceTestingFramework

inThreeClasses

FITisrelativelysimpletoexplain.It'salittleframeworkthat

letsyouwriteexecutableapplicationtestsinHTMLtables.Each

typeoftableisprocessedbyaprogrammer-definedclasscalled

afixture.WhentheframeworkprocessesapageofHTML,it

createsafixtureobjectforeachtableinthepage.Thefixture

usesthetableasinputtovalidationcodeofyourchoice:it

readscellvalues,communicateswithyourapplication,checks

expectedvalues,andmarkscellsgreenorredtoindicate

successorfailureofacheck.

Thefirstcellinthetablespecifiesthenameofthefixtureclass

thatwillbeusedtoprocessthetable.Forinstance,Figure6-1

showsatablethatwillbeprocessedbytheMarketEvaluation

fixture.Figure6-2showsthesametableafterFIThas

processedit;onscreen,theshadedcellswouldberedtoshowa

validationfailure.

Figure6-1.HTMLtabledisplayedbeforeFITprocessing



Figure6-2.HTMLtabledisplayedafterFITprocessing



ThekeyideabehindFITisthatdocumentscanserveastests.

Youcould,forinstance,embedtablesinarequirements

documentandrunthedocumentthroughFITtoseewhether

thebehaviorspecifiedinthosetablesexistsinyoursoftware.

ThesedocumentswithtablescanbewrittendirectlyinHTML,or

theycanbewritteninMicrosoftWordoranyotherapplication

thatcansavedocumentsasHTML.BecauseaFITfixtureisjust

apieceofsoftware,itcancallanyportionofanapplicationyou

caretotest,andmakethosecallsatanylevel.It'sallunder

yourcontrolasaprogrammer.

Iwon'tspendanymoretimeexplainingFITanditsproblem

domain;there'smoreinformationontheFITwebsite

(http://fit.c2.com).ButIdowanttodescribethedesignofFIT

andsomeoftheinterestingchoicesitembodies.

ThecoreofFITisonlythreeclasses:Parse,Fixture,and

TypeAdapter.Theirfieldsandmethods,andtherelationships

betweenthem,areshowninFigure6-3.

Figure6-3.RelationsamongFITclasses



Let'swalkthroughit.

Inanutshell,theParseclassrepresentstheHTMLofa

document.TheconstructorofParseacceptsastringand

recursivelyconstructsatreeofParseobjects,knittogether

usingthefieldspartsandmore.EachParseobjectrepresents

someportionofthedocument:there'sanindividualParsefor

eachtable,row,andcell.

TheFixtureclasstraversesthetreeofparses,and

TypeAdapterconvertstestingvalues(numerics,dates,etc.)to

textandbackagain.Fixturestalktotheapplicationyouare

testingandmarkindividualcellsredorgreenifacheckpasses

orfails.

MostoftheworkinFIThappensinsubclassesoftheFixture

class.FixturesdefinetheformatoftheHTMLtablesthey

interpret.Ifyouwanttocreateatablethatconsistsof,say,a

seriesofcommandstoexecuteagainstyourapplication,you

usethepredefinedActionFixtureclass.Ifyouwanttoquery



yourapplicationformultipleresultsandcomparethemagainst

asetofexpectedvalues,youusetheRowFixtureclass.FIT

providesthesesimplesubclasses,butitalsoallowsyouto

subclassFixtureyourself.

FITisausefulframework.Iuseitoften,andI'mcontinually

amazedatwhatyoucandowiththatcoreofthreeclasses.

Manyframeworkswouldtakethreeorfourtimesasmany

classestodothesameamountofwork.







Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 6. Framework for Integrated Test: Beauty Through Fragility

Tải bản đầy đủ ngay(0 tr)

×