0% found this document useful (0 votes)
72 views

Statement Coverage Testing

Statement coverage testing is a widely used white box testing technique where all statements of source code are executed at least once to calculate test coverage percentage. It derives test cases based on the code structure. The goal is to cover all possible executing statements by taking different input values to check coverage for each scenario. An example calculates 71% coverage for a scenario that prints a positive result and 0% for a scenario with a negative result.

Uploaded by

NANDHINI AK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Statement Coverage Testing

Statement coverage testing is a widely used white box testing technique where all statements of source code are executed at least once to calculate test coverage percentage. It derives test cases based on the code structure. The goal is to cover all possible executing statements by taking different input values to check coverage for each scenario. An example calculates 71% coverage for a scenario that prints a positive result and 0% for a scenario with a negative result.

Uploaded by

NANDHINI AK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

St

atementCov
erageTest
ing

Stat
ementcover
agei
soneoft
hewi
del
yusedsof
twar
etest
ing.I
tcomesunderwhi
te
boxtest
ing.

Statementcover
aget echni
queisusedtodesignwhi
teboxt
estcases.Thi
stechni
que
i
nv olv
esexecuti
onofal lstat
ementsofthesourcecodeatl
eastonce.Itisusedto
calculat
ethetotalnumberofexecut edst
atement
sinthesourcecodeoutoft otal
statementspr
esenti
nt hesourcecode.

Stat
ementcover
ageder i
vesscenar
iooftestcasesunderthewhit
eboxt
esti
ngprocess
whichi
sbasedupont hestr
uctur
eofthecode.
Inwhit
eboxtesting,concent
rat
ionofthet est
erisont heworki
ngofi
nter
nalsource
codeandfl
owchar torf
lowgraphofthecode.

Generall
y ,
intheinternalsourcecode,thereisawidevari
etyofelementsli
keoperators,
methods,ar rays,l
oopi ng,contr
olstat
ement s,except
ionhandlers,et
c.Basedont he
i
nputgi ventot heprogr am,somecodest atementsareexecut
edandsomemaynotbe
executed.The goalofst atementcov erage t
echni
que i
st o coverallthe possibl
e
executi
ngst atementsandpat hl
inesi
nt hecode.

Let
'sunder
standt
hepr
ocessofcal
cul
ati
ngst
atementcov
eragebyanexampl
e:

Here,wearetakingsourcecodetocreat
etwodiffer
entscenar
iosaccordi
ngt
oinput
val
uestocheckthepercent
ageofst
atementcov
erageforeachscenar
io.

Sour
ceCodeSt
ruct
ure:

o Takei
nputoft
wov
aluesl
i
kea=0andb=1.
o Fi
ndt
hesum oft
heset
wov
alues.
o I
fthesum i
sgr
eat
ert
han0,
thenpr
int"
Thi
sist
heposi
ti
ver
esul
t.
"
o I
fthesum i
slesst
han0,
thenpr
int"
Thi
sist
henegat
iver
esul
t.
"

1.input  
(int
 a,
 i
nt 
b)  
 
2.{  
 
3.Funct ion 
to pri
nt 
sum  of 
these 
int
eger
 val
ues 
(sum 
= a+b)
  
 
4.If (
sum>0)  
  
5.{  
 
6.Pr i
nt (
This i
s posi
tive r
esult)
  
 
7.} else 
   
8.{  
 
9.Pr i
nt (
This i
s negati
v e 
resul
t) 
  
10.
}  
 
11.

  

So,
thi
sist
hebasi
cst
ruct
ureoft
hepr
ogr
am,
andt
hati
sthet
aski
tisgoi
ngt
odo.

Now,let
's see the t
wo dif
fer
entscenar
ios and cal
cul
ati
on oft
he per
cent
age of
St
atementCoverageforgi
vensour
cecode.

Scenar
io1:
I
fa=5, b=4

1.pri
nt (
int
 a, 
int
 b) 

  
2.i
nt sum = a+b;
   
3.i
f (
sum>0)   
 
4.pri
nt (
"This 
is 
a posi
ti
ve r
esult
") 
  
5.el
se   
6.pri
nt (
"This 
is 
negati
ve r
esult
") 
  
7.} 
  

Inscenari
o1, wecanseetheval
ueofsum wil
lbe9t
hati
sgr
eat
ert
han0andaspert
he
condit
ionresul
twil
lbe"Thi
sisaposi
ti
veresul
t."

To cal
cul
ate st
atementcover
age ofthe f
ir
stscenari
o,take t
he t
otalnumberof
st
atement
sthatis7andthenumberofusedst
atement
sthatis5.

1.Tot
al 
number 
of st
atements 
= 7  
2.Number 
of 
executed 
stat
ement s 
= 5 
 

1.St
atement 
cov
erage 
= 5/
7*100 
  
2.= 
500/7 
 
3.= 
71%  
 

Li
kewi
se,
inscenar
io2,

Scenar
io2:
I
fA=- 2,B=-7

1.pr
int
 (
int
 a, 
int
 b) 

  
2.i
nt 
sum = a+b;
   
3.i
f (
sum>0) 
  
4.pri
nt 
("
Thi
s i
s a 
posit
ive r
esult
") 
  
5.el
se  
 
6.pri
nt 
("
Thi
s i
s negat
ive r
esult
") 
  
7.} 
  

Inscenari
o2,wecanseetheval
ueofsum wil
lbe-9t
hati
slesst
han0andaspert
he
condi
tion,
resul
twi
l
lbe"Thi
sisanegat
iver
esul
t."

To cal
cul
ate st
atementcover
age ofthe f
ir
stscenar
io,t
ake t
he t
otalnumberof
st
atement
sthatis7andthenumberof
usedstat
ementst
hati
s6.

Tot
alnumberofstat
ements=7
Numberofexecut
edstat
ement
s=6

1.St
atement 
cov
erage 
= 6/
7*100 
<br
>  
2.= 
600/7 
 
3.= 
85%  

But,wecanseeallthest
atement
sarecoveredi
nbot
hscenar
ioandwecanconsi
der
thatt
heover
all
stat
ementcover
ageis100%.

So,
thest
atementcov
eraget
echni
quecov
ersdeadcode,
unusedcode,
andbr
anches.

Br
anchCov
erageTest
ing

Branchcoveragetechni
queisusedt ocoverallbr
anchesofthecontr
olfl
ow graph.It
coversal
lthepossibl
eoutcomes( tr
ueandfal
se)ofeachcondit
ionofdeci
sionpointat
l
eastonce.Branchcover
aget echni
queisawhit
eboxtesti
ngt
echniquet
hatensuresthat
everybr
anchofeachdecisionpointmustbeexecut
ed.

Howev er
,branchcov
eragetechni
queanddecisi
oncover
agetechni
quear everysimil
ar,
butthereisakeydiff
erencebetweenthet
wo.Decisi
oncoveragetechniquecoversall
branchesofeachdecisi
onpointwhereasbrancht
esti
ngcoversal
lbr anchesofev er
y
deci
sionpoi
ntoft
hecode.

Li
kedecisi
oncov
erage,i
tal
sousesacont
rolf
low gr
apht
ocal
cul
atet
henumberof
br
anches.

Howt
ocal
cul
ateBr
anchcov
erage?

Ther
earesever
almet
hodst
ocal
cul
ateBr
anchcov
erage,butpat
hfi
ndi
ngi
sthemost
commonmethod.

Inthismethod,t henumberofpathsofexecut edbranchesisusedt ocalculat


eBr anch
coverage.Br anch cov
erage t
echnique can be used as the al
ternat
ive ofdeci sion
coverage.Somewher e,i
tisnotdef
inedasani ndiv
idualtechni
que,butiti
sdi st
inctfrom
decisi
oncov erageandessenti
alt
otestallbr
anchesoft hecontrol
flowgraph.

Let
'sunder
standi
twi
thanexampl
e:

1.Read X 
 
2.Read Y 
 
3.I
F X+Y > 
100 THEN 
 
4.Pri
nt 
"Large" 
 
5.ENDIF  
6.I

X + Y<100 THEN 
 
7.Pri
nt 
"Smal l

 
8.ENDIF  

Thisist hebasiccode struct


urewhere we t
ookt wo var
iablesX and Y and t
wo
condit
ions.Ift
hefir
stcondi
ti
onistr
ue,
thenpri
nt"Lar
ge"andifitisf
alse,
thengotothe
nextconditi
on.I
fthesecondcondi
ti
oni
strue,
thenpri
nt"Smal
l."

Cont
rol
flowgr
aphofcodest
ruct
ure
I
nt heabov ediagram,contr
olflowgraphofcodei sdepi
ct ed.Inthefi
rstcasetraver
sing
thr
ough" Yes"decisi
on,thepathis 
A1-B2-C4-D6-E8,
andt henumberofcov er
ededgesi s
1,2,4,5,6and8butedges3and7ar enotcov eredinthispat h.Tocovertheseedges,
wehav etot r
aversethr
ough" No"deci
sion.Inthecaseof" No"decisi
onthepat hisA1-B3
-5-
D7,andt henumberofcov erededgesi s3and7.Sobyt raveli
ngthroughthesetwo
paths,
allbrancheshav ecovered.

Path1-A1-B2-
C4-D6-
E8
Path2-A1-B3-
5-D7
BranchCov
erage(BC)=Numberofpat
hs=2
Case Cov
eredBr
anches Pat
h Branch
coverage

Yes 1,
2,4,
5,6,
8 A1-
B2-
C4-
D6-
E8 2

No 3,
7 A1-
B3-
5-D7

Condi
ti
onCov
erageTest
ing
Conditi
oncov erageisal soknownasPr edi
cateCov erageinwhicheachoneoft he
Booleanexpr essionhav ebeenev aluatedtobothTRUEandFALSE.Thi smeanst hat
 
each indi
v i
dualcondi t
ion isonet imet rueand f al
se.I notherwordswecov eral
l
condit
ions,hence condi t
ion coverage.The out come oft he deci
sion pointi
s onl
y
rel
evantf orchecki ng thecondi t
ions.Al so t
hecombi nati
onsofconditionsarenot
rel
evant.Sincet her
ear eonlyt wopossi bl
eoutcomesofacondi t
ion(trueorfal
se),
condit
ioncov erageresultsin2testsi t
uati
onsperdecisionpoint.

Deci
sionCov
erageTest
ing
Decisi
oncov eraget echniquecomesunderwhi teboxt est
ingwhi chgivesdecision
coveragetoBooleanv alues.Thi
stechniquerepor
tstrueandfalseoutcomesofBoolean
expressi
ons.Whenev ert her
ei s a possi
bil
it
yoft wo ormor e outcomes fr
om t he
stat
ement sli
kedowhi l
est at
ement,ifstatementandcasest atement( Cont
rolfl
ow
stat
ement s)
,itisconsideredasdecisionpointbecausetherearet wooutcomeseither
tr
ueorf al
se.

Decisi
oncover
agecov
ersallpossibl
eoutcomesofeachandev
eryBool
eancondi
ti
onof
thecodebyusi
ngcont
rolf
lowgr aphorchart
.

Gener al
ly
,adecisi
onpoi
nthast wodeci si
onv al
uesoneistrue,andanotheri
sf al
se
that'
swhymostoft heti
mest het ot
alnumberofout comesi stwo.Thepercentof
decisioncover
agecanbefoundbydi vi
dingthenumberofexerci
sedoutcomewiththe
totalnumberofout
comesandmul t
ipli
edby100.
Inthi
st echni
que, i
tistought oget100%cov eragebecausesomet i
mesexpressi
onsget
compl i
cated.Due t ot his,ther
e are severaldif
fer
entmet hods toreportdeci
sion
coverage.Allthesemet hodscoverthemosti mport
antcombi nat
ionsandv er
ymuch
simil
art odecisioncov erage.Thebenef i
toft hesemet hodsisenhancementoft he
sensi
tivit
yofcont r
olfl
ow.

Wecanf
indt
henumberofdeci
sioncov
erageasf
oll
ows.

Let
'sunder
standi
tbyanexampl
e.

Consi
dert
hecodet
oappl
yondeci
sioncov
eraget
echni
que:

1.Test 
(i
nt a)
  
  
2.{ 
  
3.If
(a>4) 
  
4.a=a*3  
 
5.Pri
nt (
a) 
  
6.} 
  

Scenari
o1:
Valueofai
s7(
a=7)

1.Test 
(i
nt 
a=7)
  
 
2.{ 
if
 (
a>4) 
  
3.a=a*3 
  
4.pri
nt 
(a)
  
 
5.} 
  

Thecodehi
ghli
ghtedinyel
lowi
sexecut
edcode.Theout
comeoft
hiscodei
s"Tr
ue"i
f
condi
ti
on(
a>4)ischecked.

Cont
rol
flowgr
aphwhent
hev
alueofai
s7.
Cal
cul
ati
onofDeci
sionCov
erageper
cent
:

Deci
sion 
Cov
erage 
= ½*
100 
 (
Onl

"Tr
ue"
 i
s exer
cised)
  

 
  
  
  
  
  
  
 =100/
2  

 
  
  
  
  
  
  
  
= 50 
 

Deci
sion 
Cov
erage 
i
s 50% 
  

Scenari
o2:
Valueofais3(
a=3)

1.Test 
(i
nt 
a=3)
  
 
2.{ 
if
 (
a>4) 
  
3.a=a*3 
  
4.pri
nt 
(a)
  
 
5.} 
 
Thecodehi
ghl
ightedinyel
lowwi
l
lbeexecut
ed.Theout
comeoft
hiscodei
s?Fal
se?i
f
condi
ti
on(
a>4)ischecked.

Cont
rol
flowgr
aphwhent
hev
alueofai
s3

Cal
cul
ati
onofDeci
sionCov
erageper
cent
:

1.= 
½* 100 
 (Onl

"Fal
se" 
is 
exer
cised)
 <br
>  
2.=100/2  
 
3.= 
50  
4.Decisi
on Cover
age 
= 50%  

Resul
ttabl
eofDeci
sionCov
erage:
TestCaseVal
ueofAOutput
Deci
sionCov
erage
1 3 3 50%
2 7 21 50%

MULTI
PLECONDI
TIONCOVERAGE
I
trepor
ts aboutwhet
herev
ery possi
ble combi
nat
ion of
Bool
eansubexpr
essi
onsoccur
s.
 Testcasesr
equi
redar
egi
venbyt
hel
ogi
caloper
atort
rut
h
t
abl
efort
hecondi
ti
on.
 Thi
srequi
ref
ourt
estcasesf
oreachoft
hecombi
nat
ionsof
aandbf
orbot
htr
ueandf
alse.
I
tismor
edet
ail
edf
orm.
I
trequi
res t
hat al
ltr
ue-
fal
se combi
nat
ion of si
mpl
e
condi
ti
onsbeexer
cisedat
leastonce.
 Ther
e ar
e atmost2^
ntr
ue f
alse combi
nat
ions f
ora
compoundpr
edi
cat
ewi
thnsi
mpl
econdi
ti
ons.
 Mul
ti
ple condi
ti
on cov
erage ensur
es t
hatal
lst
atement
br
anchesandcondi
ti
onsar
ecov
eredbutnotal
lpat
hs.
 Reachi
ngal
lcondi
ti
onscombi
nat
ionsmaybei
mpossi
ble.
 Thi
smayal
sopr
ecl
udecer
tai
ncombi
nat
ions.

You might also like