Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

TestingontheToilet

ChangeDetectorTestsConsideredHarmful

Youhavejustfinishedrefactoringsomecodewithoutmodifyingitsbehavior.Thenyourunthetestsbefore
committingandabunchofunittestsarefailing.
Whilefixingthetests,yougetasensethatyouare
wastingtimebymechanicallyapplyingthesametransformationtomanytests
.Maybeyouintroduceda
parameterinamethod,andnowmustupdate100callersofthatmethodinteststopassanemptystring.
Whatdoesitlookliketowritetestsmechanically?
Hereisanabsurdbutobviousway:
//Productioncode:
defabs(i:Int)
return(i<0)?i*1:i

//Testcode:
for(line:StringinFile(prod_source).read_lines())
switch(line.number)
1:assertline.contentequalsdefabs(i:Int)
2:assertline.contentequalsreturn(i<0)?i*1:i

Thattestisclearlynotuseful
:itcontainsanexactcopyofthecodeundertestandactslikeachecksum.
A
correctorincorrectprogramisequallylikelytopass
atestthatisaderivativeofthecodeundertest.No
oneisreallywritingtestslikethat,buthowdifferentisitfromthisnextexample?
//Productioncode:
defprocess(w:Work)
firstPart.process(w)
secondPart.process(w)

//Testcode:
part1=mock(FirstPart)
part2=mock(SecondPart)
w=Work()
Processor(part1,part2).process(w)
verify_in_order
was_calledpart1.process(w)
was_calledpart2.process(w)

Itistemptingtowriteatestlikethisbecauseitrequireslittlethoughtandwillrunquickly.
Thisisa
changedetectortest

itisatransformationofthesameinformationinthecodeundertest

and
itbreaks
inresponseto
any
changetotheproductioncode,

withoutverifyingcorrectbehavior
ofeitherthe
originalormodifiedproductioncode.

Changedetectorsprovidenegativevalue
,sincethetestsdonotcatchanydefects,andtheadded
maintenancecostslowsdowndevelopment.Thesetestsshouldberewrittenordeleted.
Moreinformation,discussion,andarchives:
http://googletesting.blogspot.com
CopyrightGoogleInc.LicensedunderaCreativeCommons
AttributionShareAlike4.0License(
http://creativecommons.org/licenses/bysa/4.0/
).

You might also like