【软件测试作业】用实例理解 fault / error / failure

Posted on 2018-03-12 11:44  XieJuan  阅读(1879)  评论(0)    收藏  举报

想要对这三者进行区分,首先要知道它们的定义。

fault: A static defect in the software.

error: External, incorrect behavior with respect to the requirements or other description of the expected behavior

failure: An incorrect internal state that is the manifestation of some fault.

我们老师上课的时候讲的例子我觉挺容易理解的。

一个人生病了,这是我们可以从ta 外部表现得出的结论——failure;ta 生病的症状,自己身体的反应——error;而生病的真正原因是 ta 被细菌感染了,细菌——fault,问题的根本所在。

在程序中,fault 就是 bug 所在,就像我老写错的语法错误;error 就是对程序内部造成了影响;failure 就是前面的进一步影响,导致直接可以程序运行结果看到程序的失败。

我们用两个例子来深入理解一下。

例1:

 1 public int findLast (int[] x, int y) { 
2 //Effects: If x==null throw NullPointerException 3 // else return the index of the last element 4 // in x that equals y. 5 // If no such element exists, return -1

6 for (int i=x.length-1; i > 0; i--) { 7 if (x[i] == y) { 8 return i; 9 } 10 } 11 return -1; 12 } 13 // test: x=[2, 3, 5]; y = 2 14 // Expected = 0

例2:

 1 public static int lastZero (int[] x) {
 2 
 3  //Effects: if x==null throw NullPointerException
 4 // else return the index of the LAST 0 in x. 
 5 // Return -1 if 0 does not occur in x 
 6 
 7     for (int i = 0; i < x.length; i++) { 
 8         if (x[i] == 0) { 
 9             return i; 
10         } 
11     }
12     return -1;
13 }
14 
15 // test: x=[0, 1, 0] 
16 // Expected = 2

1. 首先这两个程序是有 bug 的,不知道你有没有发现。

两个程序的错误都出现在 for 循环中。例1 中“ i > 0”, 使其不能完全遍历数组,其数组的第一位并不能被访问到。想要其运行结果达到预期,将其改为“i >= 0”,就可以了。

例2 中遍历的顺序反了,原程序运行的结果是找到第一个 0  的位置,与预设正好相反。将其改为“int  i = x.length - 1; i <= 0” 就可以啦.

 

2. 对于两个例子各设计不同的用例来理解fault、 error、failure 的不同。

2.1使程序运行时没有执行错误语句。对于二者来说,想要不执行错误语句只需要最开始便不满足循环条件。

用例:

例1:
Test:x=[], y =0
Expected: NullPointerException
Actual: NullPointerException

例2:
Test:x=[] 
Expected: NullPointerException
Actual: NullPointerException

2.2 程序运行时执行错误,但是并没有引发 error state 改变。对于例1 而言,只要程序运行停止在最终判断条件之前就不会发现数组没能遍历到第 0 位,也就不会引发error state 改变。而对于例2 来说,当 last = first 的时候,也没有所谓的顺序,也不会引发 error state 改变。因此测试用例可以用:

例1:
Test:x=[1,2,3], y =2 
Expected:1
Actual:1

例2:
Test:x=[0] 
Expected:0
Actual:0:

2.3 程序运行时执行错误,也引发了 error ,但没有造成 failure。对于例1 而言,要造成 error 程序必然已经完全跑完了 for 循环里面的所有条件,发现不能遍历到第 0  位便会发生 error,但没有造成 failure,说明数组内的数对于结果而言没有决定性作用。对于例2 而言,程序遍历了数组内内容,但不管何种顺序都不影响结果,说明数组中只有一个位置符合条件。因此测试用例可以用:

例1:
Test:x=[1,2,3], y = 4 
Expected: -1
Actual: -1

例2:
Test:x=[1,2,3,4,0,5],
Expected: 4
Actual: 4:

 

以上便是我对于 fault 、error、failure 的理解及区分,希望有用。