軟體測試中的錯誤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].
轉載于:https://www.cnblogs.com/JasonLiuys/p/6485997.html