軟件測試中的錯誤Failure, Error, Fault的區別:
Failure: External, incorrect behavior with respect to the requirements or other description of the expected behavior(預期行為出錯或與其他的外部行為描述不符)。指軟件在運行時出現的功能的喪失,類似於看病時病人發病的症狀。
Fault: A static defect in the software(軟件中的靜態缺陷)。指可能導致系統或功能失效的異常條件,類似於病人發病的病因。
Error: An incorrect internal state that is the manifestation of some fault(不正確的內部狀態,是一些故障的表現)指計算、觀察或測量值或條件,與真實、規定或理論上正確的值或條件之間的差異。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 { 8 if (x[i] == y) 9 { 10 return i; 11 } 12 } 13 return -1; 14 } 15 // test: x=[2, 3, 5]; y = 2 16 // Expected = 0
1. 找到程序中的Fault:
Fault:循環條件出錯,i>0會忽略數組中的第一個值,故應改為i>=0.
2. 設計一個未執行Fault的測試用例:
x=null; y=5
3. 設計一個執行Fault,沒有觸發Error的測試用例:
數組x的第一個元素不是與y相等的唯一的元素即可避免Error,如x = [2, 3, 2, 6], y = 2.
4. 設計一個觸發Error,但不導致Failure的測試用例:
當數組只有一個元素的時候,循環無法進行,返回-1,觸發Error。但若x中唯一的元素與y不相等,則Failure不會產生。如x = [7], y = 4。
題目2:
1 public static int lastZero (int[] x) { 2 //Effects: if x==null throw NullPointerException 3 // else return the index of the LAST 0 in x. 4 // Return -1 if 0 does not occur in x 5 for (int i = 0; i < x.length; i++) 6 { 7 if (x[i] == 0) 8 { 9 return i; 10 } 11 } return -1; 12 } 13 // test: x=[0, 1, 0] 14 // Expected = 2
1. 找到程序中的Fault:
Fault:循環錯誤,程序為從前往后遍歷,應改為從后往前遍歷即 for (int i=x.length-1; i >= 0; i--)。
2. 設計一個未執行Fault的測試用例:
程序總會執行int i=0 故肯定會執行Fault,及時x=null也會執行Fault。
3. 設計一個執行Fault,沒有觸發Error的測試用例:
當x=null時執行Fault且會拋出異常但不會觸發Error。
4. 設計一個觸發Error,但不導致Failure的測試用例:
當數組中不為空且只有一個元素等於0時或者沒有元素為0時會觸發Error但不會導致Failure。如x=[1, 0, 2].
