您的位置:首页 > 其它

软件测试之二——程序中的错误及其理解分析

2016-03-09 21:24 387 查看
  一、错误的类型

  软件测试的工作之一就是发现错误,而对错误的理解也十分重要。错误根据其区别大概可分为以下三种:Fault,Failure,Error。

  二、具体的定义

  这三种类型的错误,翻译成中文可能区别不是很大,但是英文理解的话,还是有很大的区别的。在wiki上很难找到这三个单词的针对计算机软件的含义,不如就直接引用原义来解释着三个单词。


  Fault:wiki中将trap与fault同义,即 In computing and operating systems, a trap, also known as an exception or a fault, is typically[NB 1][1] a type of synchronous interrupt typically caused by anexceptional condition (e.g., breakpoint, division by zero, invalid memory access).


  理解上面几句话,fault是能够将计算机或系统强行中断错误,如果将层面上升到程序层次,那么就是说对于程序而言,Fault是程序错误的根源所在。而在程序代码中存在的编写代码的错误导致了Fault,代码是硬编码的,不会在运行时由人为的产生错误。所以Fault应该是特指编写代码时的静态错误,与运行时无关。


  Failure:Failure is the state or condition of not meeting a desirable or intended objective, and may be viewed as the opposite ofsuccess.


  简单的来说Failure现在所在的状态没有达到预计的期望,它对程序来说可能是异常,崩溃,结果出错等,它来源于上述的fault,fault导致了failure的出现。


  Error:An error (from the Latin error, meaning "wandering") is an action which is inaccurate or incorrect.In some usages, an error is synonymous with a mistake (for instance, a cook who misses a step from a recipe might describe it as either an error or a mistake), though in technical contexts the two are often distinguished. For instance, in statistics "error" refers to the difference between the value which has been computed and the correct value.



  从上面可以看出,error的定义可以从上述两个单词来定义。当一个程序编写的时候带有fault时,这个fault可能是任何一种编写错误,并且可能在特定的任何时间导致error,也就是说,程序编写的时候带有fault,但是并不一定会导致出错。可能只在特定的某个时候出现。那么当运行时fault出错时,会进入异常的状态,这种状态不会持续很久,可能会立即程序崩溃导致failure,也可能不会导致failure。

  简单的来说:fault是根源,error是表现的状态,failure是导致的结果。

  三、案例分析

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.调用函数传入空数组,会导致空指针异常,但不会到达错误地点

  3.如果等于y的最后一个元素本来就不是第一个元素,那么会到达错误点但不会导致错误

  4.进入error但不会导致failure的情况也会出现,例如数组本就不存在想要寻找的元素,那么会一直返回failure

  5.当数组中只有一个元素,等于y是导致error和failure,不等于时导致error

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,无法实现预期的功能,返回数组第一个0的索引

  2.存在空数组导致不会到达faul

  3.数组只有一个元素时,有fault但不会导致error

  4.数组只有一个0元素或没有时时导致error但不会failure

  四、总结

  错误的类型或许从中文的角度理解并无多大的区别,但是对于程序来说,理解错误的种类至关重要。理解在何时何地出现何种错误,理解错误的成因十分重要
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: