On the white-box testing

White-box testing basic requirements

  • A separate module to ensure that all paths are performed at least once;
  • Require testing of all logical values ​​true, false two branches;
  • All runs and circulates in the upper and lower boundaries operable range;
  • Check the internal data structures to ensure their effectiveness.

Test coverage criteria

  1. Statement coverage : is a relatively weak standard test, its meaning is: select enough test cases, so that each statement in the program can be executed at least once .
  • It is the weakest logical coverage, limited effects, interaction with other methods must be used.
  1. Decision coverage (also known as branch coverage ): The test performed sufficiently, such that each branch of the program through the at least once .
  • Decision Coverage is only slightly stronger than statement coverage, but the actual results show that the only decision coverage, it can not guarantee it will be to detect the presence of an error in judgment in the conditions. Thus, also require more coverage criteria to test the logic of the internal conditions is determined.
  1. Condition coverage perform sufficient test cases: every possible values at least once so that each program is determined for each condition .
  • Condition coverage deep into each condition determination, but may not meet the requirements of coverage determination.
  1. Determination / condition coverage : perform sufficient test cases, so that in each condition is determined to take all possible values, and each decision to take all possible results .
  2. Condition combination coverage : perform sufficient examples, so that each decision in a variety of possible combinations of conditions at least once .
  • This is a very strong coverage criteria, can effectively check the condition of the various possible values ​​of the combination is correct.
  • It not only covers all combinations of possible values ​​of the condition, preferably also cover all branches of the determination, but may be left out some paths.
  • Testing is not complete.

The main method

  1. Logic-driven testing
  • Statement Coverage
  • Decision Coverage
  • Condition coverage
  • Determination / condition coverage
  • Condition combination coverage
  1. Path testing
  • Path testing is to design a sufficient number of test cases, covering all possible paths are in the test object. This is the strongest coverage criteria.
  • Basic Path Testing: Design a sufficient number of test cases, run the test program, to cover all possible paths the program . However, when a large number of paths, truly complete coverage is very difficult, must be the number of paths covering compressed to a certain limit . E.g. loop program executed only once.

Statement Coverage

Example 1:

PROCEDURE  M(VAR A,B,X:REAL);  
BEGIN 
IF ((A>1) AND (B=0)) THEN X:=X/A; IF ((A=2) OR (X>1)) THEN X:=X+1; END. 
 
flow chart

For each statement in the program at least once, simply by designing a path ace examples can be, for example, selecting input data: A = 2, B = 0 , X = 3
in the example can be seen from the statement coverage is actually very weak, if the first conditional statement incorrectly written to the aND OR, the above test is not find this error; if the third conditional statement X> 1 mistakenly written X> 0 this test can not be exposed to it. in addition, along the path abd implementation of the value of X should remain unchanged, if there is an error in this regard, the test data can not find them.

Example 2:

void  DoWork(int x,int y,int z) { int k=0,j=0; if((x>3)&&(z<10)) { k=x*y-1; //语句块1 j=sqrt(k); } if((x= =4)||(y>5)) { j=x*y+10; //语句块2 } j=j%3; //语句块3 } 
 
flow chart

To test the statement coverage designing such a test can be performed to three statement blocks the statement coverage. Test inputs: x = 4, y = 5 , z = 5, the program execution paths are: abd
the test while covering the executable statement, but it does not check logic determines whether there are problems, for example, in the first determination && error in the written ||, then the above test cases can still cover all executed statements.

Decision Coverage

例1:
如果设计两个例子,使它们能通过路径ace和abd,或者通过路径acd和abe,就可达到“判定覆盖”标准,为此,可以选择输入数据为:
① A=3,B=0,X=1(沿路径acd执行)
② A=2,B=1,X=3(沿路径abe执行)
例2:
如果设计两个测试用例则可以满足条件覆盖的要求。测试用例的输入为:
x=4、y=5、z=5【a b d】
x=2、y=5、z=5【a c e】
上面的两个测试用例虽然能够满足条件覆盖的要求,但是也不能对判断条件进行检查,例如把第二个条件y>5错误的写成y<5,、上面的测试用例同样满足了分支覆盖。
注意:程序中含有判定的语句包括IF-THEN-ELSE、DO-WHILE、REPEAT-UNTIL等,除了双值的判定语句外,还有多值的判定语句,如PASCAL中的CASE语句、FORTRAN中带有三个分支的IF语句等。所以“分支覆盖”更一般的含义是:使得每一个分支获得每一种可能的结果

条件覆盖

一个判定中往往包含了若干个条件,如例1的程序中,判定 (A>1) AND (B=0)包含了两个条件: A>1以及 B=0,所以可引进一个更强的覆盖标准——“条件覆盖”。

  • 例1的程序有四个条件:
    A>1、 B=0、A=2、X>1
    为了达到“条件覆盖”标准,需要执行足够的测试用例使得在a点有: A>1、A≤1、B=0、B≠0 等各种结果出现,以及在b点有: A=2、A≠2、X>1、X≤1 等各种结果出现。
    现在只需设计以下两个测试用例就可满足这一标准:
    ① A=2,B=0,X=4 (沿路径ace执行)
    ② A=1,B=1,X=1 (沿路径abd执行)

  • 对例2中的所有条件取值加以标记。

  • 对于第一个判断:
    条件x>3 取真值为T1,取假值为-T1
    条件z<10 取真值为T2,取假值为-T2

  • 对于第二个判断:
    条件x=4 取真值为T3,取假值为-T3
    条件y>5 取真值为T4,取假值为-T4

  • 则可以设计测试用例如下


     
    条件覆盖测试用例

    注意:

  • “条件覆盖”通常比“分支覆盖”强,因为它使一个判定中的每一个条件都取到了两个不同的结果,而判定覆盖则不保证这一点。

  • “条件覆盖”并不包含“分支覆盖”,如对语句IF(A AND B)THEN S 设计测试用例使其满足"条件覆盖",即使A为真并使B为假,以及使A为假而且B为真,但是它们都未能使语句S得以执行。

如对例2设计了下面的测试用例,则虽然满足了条件覆盖,但只覆盖了第一个条件的取假分支和第二个条件的取真分支,不满足分支覆盖的要求。


 
测试用例

分支(判定)/条件覆盖

针对上面的问题引出了另一种覆盖标准——“分支(判定)/条件覆盖”,它的含义是:执行足够的测试用例,使得分支中每个条件取到各种可能的值,并使每个分支取到各种可能的结果。

  • 对例1的程序,前面的两个例子
    ① A=2,B=0,X=4 (沿ace路径)
    ② A=1,B=1,X=1 (沿abd路径)
    是满足这一标准的。
  • 对例2,根据定义只需设计以下两个测试用例便可以覆盖8个条件值以及4个判断分支。


     
    分支(判定)/条件覆盖测试用例

    分支/条件覆盖从表面来看,它测试了所有条件的取值,但是实际上某些条件掩盖了另一些条件。

  • 例如对于条件表达式(x>3)&&(z<10)来说,必须两个条件都满足才能确定表达式为真。
  • 如果(x>3)为假则一般的编译器不在判断是否z<10了。对于第二个表达式(x==4)||(y>5)来说,若x==4测试结果为真,就认为表达式的结果为真,这时不再检查(y>5)条件了。
  • 因此,采用分支/条件覆盖,逻辑表达式中的错误不一定能够查出来了。

条件组合覆盖

针对上述问题又提出了另一种标准——“条件组合覆盖”。它的含义是:执行足够的例子,使得每个判定中条件的各种可能组合都至少出现一次。满足“条件组合覆盖”的测试用例是一定满足“分支覆盖”、“条件覆盖”和“分支/条件覆盖”的。

  • 再看例1的程序,我们需要选择适当的例子,使得下面8种条件组合都能够出现:
1) A>1, B=0    2) A>1, B≠0
3) A≤1, B=0    4) A≤1, B≠0
5) A=2, X>1    6) A=2, X≤1 
7) A≠2, X>1    8) A≠2, X≤1 

5)、 6)、 7)、8)四种情况是第二个 IF语句的条件组合,而X的值在该语句之前是要经过计算的,所以还必须根据程序的逻辑推算出在程序的入口点X的输入值应是什么。
下面设计的四个例子可以使上述 8种条件组合至少出现一次:

① A=2,B=0,X=4  
    使 1)、5)两种情况出现; 
② A=2,B=1,X=1
    使 2)、6)两种情况出现;
③ A=1,B=0,X=2
    使 3)、7)两种情况出现;
④ A=1,B=1,X=1
    使 4)、8)两种情况出现。

上面四个例子虽然满足条件组合覆盖,但并不能覆盖程序中的每一条路径,例如路径acd就没有执行,因此,条件组合覆盖标准仍然是不彻底。

  • 现对例2中的各个判断的条件取值组合加以标记如下:
1、x>3,z<10    记做T1 T2,第一个判断的取真分支
2、x>3,z>=10   记做T1 -T2,第一个判断的取假分支
3、x<=3,z<10   记做-T1 T2,第一个判断的取假分支
4、x<=3,z>=10  记做-T1 -T2,第一个判断的取假分支
5、x=4,y>5     记做T3 T4,第二个判断的取真分支
6、x=4,y<=5    记做T3 -T4,第二个判断的取真分支
7、x!=4,y>5    记做-T3 T4,第二个判断的取真分支
8、x!=4,y<=5   记做-T3 -T4,第二个判断的取假分支

根据定义取4个测试用例,就可以覆盖上面8种条件取值的组合。
测试用例如下表:


 
条件组合覆盖测试用例

上面的测试用例覆盖了所有条件的可能取值的组合,覆盖了所有判断的可取分支。

 



Guess you like

Origin www.cnblogs.com/zhouxuyang1/p/11271652.html