White Box vs Black Box

Embed Size (px)

DESCRIPTION

Hai kĩ thuật kiểm thử tiêu biểu.

Text of White Box vs Black Box

A

Statement coverage vs Branch coverage

Statement coverage : tm s lng ng t nht sao cho tt c cc nt u c i qua. Node gm : nhng node iu kin, node kt qu. Branch coverage : cn c ci tn khc l Decision coverage, tm s ng t nht sao cho tt c cc cnh u c i qua. Cho nn s lng testcase ca dng ny a phn l nhiu hn dng Statement. Bi 1 1 2 3 4 5 6 7 8 if (conditionA) { c=c+1 } if(conditionB) d=d-1 else e++

CFG(control flow graph) :biu lung iu khin Statement coverage = 2 Branch coverage = 3 Bao ph lnh s i qua tt c cc nt : + 1, 3, 5, 6, end + 1, 3, 4, 8, end + Cn nt N sao khng i qua, N ch l dng lnh th n( cui cng), b N cng c. + Cch lm th ny : Ta ang ng nt 1, ta c hai la chn, mt i qua phi, mt i qua tri. Ta thy, nu i qua phi th khng c nt no trn con ng i c, nn ta khng i. Cn bn tri th c nt s 3. S con ng t nht i qua tt c cc nt l 2 Bao ph nhnh =3 v c t nht l 3 con ng i qua tt c 1

cc cnh, ch no c cnh l n phi i qua. ng nhin n nhiu hn bao ph lnh. Nh iu ny : bao ph nhnh l cha ca bao ph lnh, nu nh ta c 100% bao ph nhnh, th bao ph lnh cng t 100%, ngc li khng ng. Bi 2

Bi ny ni v Bao ph lnh : bi ny c t nht mt testcase. Bi 3

2

Bao ph nhnh c t 100% khng ? Khng bao gi t. V thiu default, y l trng hp t bit. Bi 4 : cch v CFG . Xem cc v d sau trc khi xem v d ny

Dng 9 c and cho nn phi tch ra lm 2 thnh 9 v 9 . 9 tng ng vi Y 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=10, Y=30 Output: 50 Note: We still cover statements like we did before, but now the focus is on the edges that we hit. Ta thy n u tin i theo true trc. Edge Coverage: Test Case 2

6

1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Input: X=15, Y=30

Sau mi i theo false.( ch 7->5 ; ch 9 -12) Sau hai con ng ta c hnh nh sau 1 function P return INTEGER 2 begin 3 X, Y: INTEGER; 4 READ(X); READ(Y); 5 while (X > 10) loop 6 X := X - 10; 7 exit when X = 10; 8 end loop; 9 if (Y < 20 and then X mod 2 = 0) then 10 Y := Y + 20; 11 else 12 Y := Y - 20; 13 end if; 14 return 2 * X + Y; 15 end P; Test Case 1: X=20, Y=10 Test Case 2: X=15, Y=30

7

Cn mt cnh, v tng t. Cng nh dng bao ph lnh, bi ny cng c nhiu con ng khc nhau, nhng ta ch chn ra t nht thi theo ng nh ngha. Kt hp hai phng php s cho kt qu tt hn. Bi 7 Read a; Read b; if(a>=b) print a else print b Do x; if (a ==0) print Zero do nothing;

t 100% bao ph lnh ta cn nhng con ng no

8

9

Ta thy, if a==0 ta chn i theo hng no, i xung th khng ng do khng c node no khc trn ng i, trong khi i qua phi th c mt node Print 0, ging nh gia mt con ng c nhiu ngi vi mt con ng trng trn, bn i ng no, ng nhin l i ng c nhiu ngi, i ng trng coi chng. t 100% bao ph nhnh ta cn nhng con ng no

10

Nh l i theo true trc ri mi ti false .

11

Bi 8 : switch case Hy xem cch v ( sa cht : 8-11 b)

12

Hoc

13

Lu : bi ny cc bn t v s ng hn. Bi trn cha chc ng do su tp trn mng. ( bi ny c trong bi thc hnh) C bao nhiu ng t bao ph cnh 100% ? Nh l i theo ng true trc ri mi ti false. i ti chng no cc cnh u c duyt qua l ok, mc d cn nhiu na nhng ch dng li thi. Nu mun i ht theo kiu t hp, th i mt ngh, rt l nhiu trng hp. Bi 9 Cch v cho vng lp do while

14

Bi 10

100% bao ph lnh : 1-2-3 . Khng c 1-3 v nu i bn phi th t 1-3 khng c node no na. 100% bao ph nhnh : 1-2-3 v 1-3 Ch cn mt testcase thi l bao ph lnh t 100% trong khi bao ph nhnh phi cn ti 2 Bi 11

15

100% bao ph lnh : 1-2-5 v 1-4-5 100% bao ph nhnh : 1-2-5 v 1-4-5 Tng t nhau. Bi 12

100% bao ph nhnh : C 3 nhnh: 1-2-3-4-5 ; 1-2-4-5 ; 1-5 100% bao ph lnh : 1-2-3-4-5 . Ti 1 : nu i bn phi th khng c nt no, nu i bn tri th c nt, nn i bn tri. Ti 2 : i ti 4 th nu i bn phi th khng c nt no trong khi bn tri th c nt 3 nn i theo bn tri.

Bi 13

16

Bao ph nhnh 1-2-3-6-7 1-2-5-6-7 1-4 Bao ph lnh 1-2-3-6-7 1-2-5-6-7 Bi 14

Bao ph nhnh 1-2-3-6-9 1-2-5-6-9 1-8-9 17

Bao ph lnh 1-2-3-6-9 1-2-5-6-9 1-8-9 Bi 15

Bi 16. Sa li iu kin trong while P0) {x=x+1;} if (b==3) {y=0;}

19

B Path coverage

Hy nhn s khc bit v bn c th t on ra c im ca dng ny. N l mt loi bao ph nhnh nhng m lit k ton b cc con ng i c th. iu ny l khng kh thi trong nhng bi ton ln v s lng test rt ln. Cho nn t s dng phng php ny. Nu c n nhnh th cn 2n test. Chng hn nh vng lp sau 20

Khi for ( i=1->1000) th bn cn phi c 1000 test. Trong khi bao ph nhnh th ch cn 2 test ( mt l khi true v mt khi false) V d : 100 % Path Coverage

21

22

Ta c tng cng 4 testcase cho trng hp bao ph path n gin nht.

C

Multiple Condition Decision Coverage

L mt dng bao ph nhnh. im khc l c nhiu iu kin xen k. V d If( a> || b-10 || c%2==0) . V d : if {(X or Y) and Z} then Vi dng nh trn, ta c cc test case TEST CASE1: X=TRUE, Y=TRUE, Z=TRUE TEST CASE2: X=FALSE, Y=FALSE, Z=FALSE TEST CASE3: X=FALSE, Y=FALSE, Z=TRUE TEST CASE4: X=FALSE, Y=TRUE, Z=TRUE TEST CASE5: X=FALSE, Y=TRUE, Z=FALSE TEST CASE6: X=TRUE, Y=FALSE, Z=TRUE .. Vi mt iu kin th ta gi l decision, nhng y c nhiu iu kin nh 23

vy th ta gi kt qu sau khi tnh ton trong if l decision outcome. In test case 3 decision outcome is FALSE In test case 4 decision outcome is TRUE In test case 5 decision outcome is FALSE In test case 6 decision outcome is TRUE Nu c n iu kin th c 2 m n test. Trong lc lm nhng v d trn, cc bn gp qua dng ny ri. By gi ta ly v d n gin cht. if (a>0 && c==1) {x=x+1;} if (b==3 || d2F.. ( s ch T/F lin nhau gp i C6 tc l 2) +C4 : 4T -> 4F -> 4T -> 4F. ( T/F lin nhau gp i c5 tc l 4) +C3 : 8T -> 8F -> 8T->8F . Dng th ny

-

-

Sau , iu kin no khng cn th xa T/F i ri thay bng du nh trn. Tuy nhin, quy tc trn gy nn s d tha, mt s trng hp c ri li c lp li, cch tt nht l gom nhng trng hp ging nhau li thnh mt rule. Nh bi tam gic trn, khng theo quy tc ca chng ta. Hy cch vit ca dng bi tam gic . Xt v d sau

36

Chng ta thy c tng cng 2^6 = 64 Rule. Nhng n li gom nhng trng hp chung li v cui cng cn 11 Rule. Rule count ch s lng Rule trng nhau. Rule 1 c username l F, 31 rule khc cng c username l F, cng c chung kt qu l khng phi tam gic. Cch vit chng ta hay dng : i t trn xung di, (Rule1,C1 ) =F, coi li thut ton thy ch cn F l n sai ri nn tip tc i xung di v in du ngch ngang vo cho tt c 5 cn li. Tip tc n (Rule2,C1) cho = T, ng th i xung 2 v cho n = False, coi li thut ton thy F l sai ri nn 4 cn li khng quan tm ti, in vo du ngch ngang, c th tip tc hon i vai tr ca tng . Khi ti Rule 4, th c1,c2,c3 u l T nn m bo l ta c mt tam gic, by gi ch cn xc nh n l tam gic g thi. Thm mt nhn xt, Rule1,2,3 m bo nhp ng format d liu, khi nhp ng ri th ta mi tnh ti thut ton chnh ( cc rule cn li) , nn cc rule v sau s khng c du gch ngang.

V d Cho cc iu kin v action nh di

in T/F theo kiu n gin 37

Nu khng thch cch n gin trn, dng cch hay hn, u tin ng no Rule1 cho name sai th khi ng nhp, cho nn b ht 2 iu kin cn li Rule2 cho name ng, nu ng th tnh ti mt khu sai, nu mt khu sai th khi ng nhp nn b k cn li. Rule 3 cho name ng, pass ng, cn ci cn li cho sai . Tip tc th t t vo l nu Rule1 l F, Rule5,6,7,8 l False cho username, nn ta gp li thnh mt trng hp chung, trng hp chung l rule1.( ni phn rule count) ch l mt s nhn xt khi nhn vo hnh, cn nhiu na, gn coi v nghin cu. Hnh di ch cn 4 Rule, gim i rt nhiu ri phi khng.

38

nh du cht to testcase

To test case

Ti liu c tng hp t nhiu ngun khc nhau. Mi vn lin quan, xin tham kho thm trn mng.

39

40