软件测试之“白盒测试” (2)

Consider the following:

1  IF (Age  > 16) and (Gender = Female) 2              Process License application 3 ELSE 4              Decline the License application process 5 END IF 1  IF (Age  > 16) or (Gender = Female) 2              Process License application 3 ELSE 4              Decline the License application process 5 END IF

Assign (Age =18 and Gender=Female) to test lines 1,2,5 and  (Age=14, Gender = Male) to test lines 1,3,4,5. So, 2 test cases are needed to execute every line of code(100% Statement coverage). And 2  test cases to test both the decisions – true and false (100% Decision Coverage).

In the above examples, we see why Decision coverage is inadequate, all conditions are not fully tested:

Age >16 & Female, Age > 16 & Male, Age <16 and Female and Age <16 & Male will cover all the combinations. Hence the need for other stronger metrics.

Nested Ifs or multiple IFs within an IF: 1 IF ( Age > 17) 2  IF Age <50 3 Print Age is between 17 and 50 4  ELSE IF ( Age > 50) 5  Print Age is greater than 50 6 END IF 7 ELSE 8   Print Age is less than 17. 9 END IF

Test cases for the above, assigning Age =16, Age =51 and Age =29 (3 test cases) will test every line/statement and every decision..

CASE Structure: The nested If can also be expressed as a CASE structure.

Case Age > 50: Report “Age is greater than 50” Case Grade > 17: Report “Age is between 17 and 50” Default: Report “Age is less than 17” End Case

Again, assigning Age =16, Age =51 and Age =29 (3 test cases) will test every line and every decision..

Iterations or loops: When an action needs to repeated until the Condition becomes  False, loops are used. There are 2 types of loops, While -Do and Repeat Until

DO WHILE condition sequence ENDWHILE

The loop is entered only if the condition is true. The “sequence” is performed for each iteration. At the conclusion of each iteration, the condition is evaluated and the loop continues as long as the condition is true.

Eg:

WHILE ( Shopping Trolley not empty) DO Add Cost of Item to Total. Print the item name and item cost END DO Print TotalCost

  Test cases for Do-While – the loop is entered if the condition is true, so one  test case that has a true value is enough to test all the lines of code and the decision.

  There is another variation of the loop structure known as a REPEAT UNTIL loop.

This loop is similar to the WHILE loop except that the test is performed at the bottom of the loop instead of at the top. Two keywords, REPEAT and UNTIL are used. The general form is:

REPEAT sequence UNTIL condition

Again, only one test case is sufficient to test all lines of code for both Statement and Decision Coverage.

白盒测试前期成本之高是无法想象的,一是白盒测试人员的培训,二是工具的开发。
人员培训包括要知道语句覆盖,条件覆盖,判断覆盖,条件判断覆盖,独立路径覆盖。再深入一点还要懂数据流测试(定义使用,程序切片…)。直接上来就设计白盒测试用例比较少,原因是设计的信息不足。一般是先按功能需求,以黑盒测试设计方法写用例。跑了用例,再用白盒的方法精简用例和增加用例。以白盒测试的方法增加用例,必要的技能是看懂代码吧。不然怎么保证用例能覆盖到某个if分支,while循环次数要求呢?要是对模块进行接口测试,要写驱动,要写桩程序吧。
要做上面的事还要写测试框架,把一些与具体用例无关,但是常用的公共功能,如参数化,结果断言,出报告等等实现。让测试人员专心为用例写测试代码。还好来源大潮提供了不少选择,如xunitgoogletest。还有覆盖工具,如Emma,codecover, ncover,不过大部分也只提供了语句覆盖,判断覆盖的信息,少部分有循环覆盖信息。开源的数据流测试的工具没见过。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgdpxs.html