软件测试(二)——分析程序中的错误(Fault,Failure,Error)

分析程序中的错误(Fault,Failure,Error)

1. 综述

  软件测试的目标是发现软件中存在的错误,专业的角度,软件中的错误分为三种:Fault(故障),Failure(失效),Error(错误),要分清其中的区别。

 

2. Fault,Failure,Error

  Software Fault: A static defect in the software. 指程序代码中静态的错误,是错误的根源。

  Software Failure: External, incorrect behavior with respect to the requirements or other description of the expected behavior. 程序外在表现的错误、失效,由于有错误而导致无法完成相应功能,称其为“失效”很贴切。

  Software Error: An incorrent internal state that is the manifestation of some fault. 由程序故障导致的程序的不正常状态,如在代码里引用了一个空指针,这是一个故障,在程序运行时就会出现Error,因为尝试使用一片没有值的指针。

 

3. 举例说明

  如一个病人到医生那里看病,他会先说明自己的症状,比如头疼、手脚无力等,这是Failure

  医生希望找到病的根源,如病毒感染,这是Fault

  由于生病所导致的身体状态的异常,如体温升高,心跳加速等,这是Error

 

4. 实际问题

分析下面的程序(1):  

public int findLast (int[] x, int y) {
    // Effects: If x==null throw  NullPointerException
    // else return the index of the last element 
    // in x that equals y. 
    // If no such element exists, return -1
    for (int i=x.length-1; i > 0; i--) 
    {
        if (x[i] == y)
        {
            return i; 
        }
    }
    return -1; 
}
// test: x=[2, 3, 5]; y = 2
// Expected = 0    

 分析:

  1). 故障(Fault):循环控制元素应为i >= 0.

  2). 当x为空时代码不会抵达错误地点。

  3). 当x中等于y的最后一个元素不是x[0]时,如x = [1, 5, 9, 6, 87, 9, 54], y = 9时,虽然运行到了出错代码,但并不会出现错误结果。

  4). 当x中只有一个元素时,会永远返回-1;当x = [8], y = 8时,出现Error和Failure;当x = [8], y = 3时,出现Error,但没有Failure。

 

分析下面的程序(2):

public static int lastZero (int[] x) {
    // Effects: if x==null throw NullPointerException
    // else return the index of the LAST 0 in x.
    // Return -1 if 0 does not occur in x
    for (int i = 0; i < x.length; i++)
    {
        if (x[i] == 0)
        {
            return i;
        } 
    } 
    return -1;
}
// test: x=[0, 1, 0]
// Expected = 2

 分析:

  1). 故障(Fault):循环控制元素应为for (int i = x.length; i >=0; i--).

  2). 程序总会抵达错误地点。

  3). 当x中只有一个元素时,如x = [1]时,虽然运行到了出错代码,但并不会出现错误。

  4). 当x中只有一个0或没有0时,如当x = [8,0,5,4]时,程序运行出现Error,但没有Failure。

 

原文地址:https://www.cnblogs.com/yongheng20/p/5248174.html