天天看點

軟體測試基礎——fault、error and failure*************軟體測試基礎*************

*************軟體測試基礎*************

首先解釋一下fault、error以及failure的各自定義:

Fault:

可能導緻系統或功能失效的異常條件(Abnormal condition that can cause an element or an item to fail.),可譯為“故障”。

Error:

計算、觀察或測量值或條件,與真實、規定或理論上正确的值或條件之間的差異(Discrepancy between a computed, observed or measured value or condition and the true, specified, or theoretically correct value or condition.),可譯為“錯誤”。Error是能夠導緻系統出現Failure的系統内部狀态。

Failure:

當一個系統不能執行所要求的功能時,即為Failure,可譯為“失效”。(Termination of the ability of an element or an item to perform a function as required.)

Below are four faulty programs. Each includes a test case that results in failure. Answer the following questions about each program.

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. Identify the fault.

for循環中的條件判斷應為:(int i=x.length-1; i > =0;i–);

2. If possible, identify a test case that does not execute the fault. (Reachability)

test: x=[];

(抛出空指針異常,沒有執行下面的程式,則沒有執行fault)

3. If possible, identify a test case that executes the fault, but does not result in an error state.

test: x=[3, 2, 5]; y = 2

Expected= 1

(執行了含有fault的程式,但是并沒有産生錯誤,即執行了fault,沒有執行error)

4. If possible identify a test case that results in an error, but not a failure.

test: x=[3, 2, 5]; y = 1 Expected = -1

(沒有周遊x=1,直接傳回了-1,是以執行了error,沒有執行failure。)

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、 Identify the fault.

for循環中的條件判斷應為:(int i=x.length-1; i > =0; i–);

2、 If possible, identify a test case that does not execute the fault. (Reachability)

test: x=[];

(抛出空指針異常,沒有執行下面的程式,則沒有執行fault)

3、 If possible, identify a test case that executes the fault, but does not result in an error state.

test: x=[1, 2, 0]

Expected = 2

(執行了含有fault的程式,但是并沒有産生錯誤,即執行了fault,沒有執行error)

4、 If possible identify a test case that results in an error, but not a failure.

test: x=[3, 2, 5];

Expected = -1

(周遊到i=3時,傳回了-1,不符合設計目的,是以執行了error,沒有執行failure。)