一、實驗目的
1、 掌握黑盒測試的基礎知識;
2、 掌握黑盒測試的檢查內容及測試目的;
3、 掌握黑盒測試的幾種基本測試方法:等價類划分方法、邊界值分析方法、因果圖法和決策表法;
二、實驗要求
1、 復習有關內容,理解黑盒測試;
2、 掌握等價類划分、邊界值分析方法、因果圖法和決策表法,並能設計出測試用例;
3、 對具體軟件,能分別使用相應的黑盒測試方法設計測試用例,並實施測試、分析測試結果。
三、實驗內容
1、設計函數實現輸入日期顯示星期幾,並用等價類及邊界值法測試
實驗步驟:
① 設計程序
② 划分等價類,得到等價類表。等價類表格式如下:
輸入條件 |
有效等價類 |
唯一標識 |
無效等價類 |
唯一標識 |
年 |
1900到2050內的閏年 |
(1) |
Year<1900 |
(10) |
|
1900到2050內的平年 |
(2) |
Year>2050 |
(11) |
|
|
|
非數字 |
(12) |
月 |
1,3,5,7,8,10,12 |
(3) |
Month<1 |
(13) |
|
4,6,9,11 |
(4) |
Month>12 |
(14) |
|
2 |
(5) |
非數字 |
(15) |
日 |
1~28 |
(6) |
Day<1 |
(16) |
|
29 |
(7) |
Day>31 |
(17) |
|
30 |
(8) |
Year為閏年 且Month 為2時,Day>29 |
(18) |
|
31 |
(9) |
Year為平年 且Month 為2時,Day>28 |
(19) |
|
|
|
Month=1,3,5,7,8,10,12時,Day>31 |
(20) |
|
|
|
Month=4,6,9,11 時,Day>30 |
(21) |
|
|
|
非數字 |
(22) |
③ 運用等價類划分法設計測試用例,得到測試用例表。測試用例表格式如下:
④ 運用邊界值法設計測試用例。
2、找零錢最佳組合
假設商店貨品價格(R) 都不大於100元(且為整數),若顧客付款(P)在100元內,現有一個程序能在每位顧客付款后給出找零錢的最佳組合(找給顧客貨幣張數最少)。假定此商店的貨幣面值只包括:50元(N50)、10元(N10)、 5元(N5)、1元(N1) 四種。
請結合等價類划分法和邊界值分析法為上述程序設計出相應的測試用例。
實驗步驟:
等價類表
測試用例
邊界值測試
3、有一個飲料自動售貨機(處理單價為5角錢)的控制處理軟件,它的軟件規格說明如下:
若投入5角錢的硬幣,按下“橙汁”或“啤酒”的按鈕,則相應的飲料就送出來。若投入1元錢的硬幣,同樣也是按“橙汁”或“啤酒”的按鈕,則自動售貨機在送出相應飲料的同時退回5角錢的硬幣。
用因果圖法測試該程序,並撰寫實驗報告。
實驗步驟:
①編寫程序
②分析原因與結果
③畫出因果圖
④轉化為決策表
⑤根據決策表設計測試用例,得到測試用例表
因果圖
決策表
測試用例
4、航空服務查詢問題:根據航線,倉位,飛行時間查詢航空服務。
假設一個中國的航空公司規定:
① 中國去歐美的航線所有座位都有食物供應,每個座位都可以播放電影。
② 中國去非歐美的國外航線都有食物供應,只有商務倉可以播放電影。
③ 中國國內的航班的商務倉有食物供應,但是不可以播放電影
④ 中國國內航班的經濟倉只有當飛行時間大於2小時時才有食物供應,但是不可以播放電影。
請用程序實現上述功能,並用決策表法設計測試用例,再執行測試,撰寫實驗報告。
實驗步驟:
① 編寫程序
② 構造決策表
③ 根據決策表設計測試用例,得到測試用例表
因果圖
決策表
測試用例
四、實驗思考
① 在實際的測試中,如何設計測試用例才能達到用最少的測試用例檢測出最多的缺陷;
② 在進行用例設計時,如何考慮軟件測試用例的充分性和減少軟件測試用例的冗余性;
1.使用等價類不同的測試用例來進行測試,做到減少測試用例的冗余性;
2.進行邊界測試查找程序缺陷;
3.根據過往經驗及個人直覺推測出軟件可能存在的缺陷,從而有針對性的設計測試用例。
五、代碼

import java.util.Scanner; public class Day { public static int which_week(int y, int m, int d){ int w = ((d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) % 7) +1; return w; } public static void main(String[] args) { int year,month,day; Scanner scanner = new Scanner(System.in); System.out.println("請輸入年"); try { year = scanner.nextInt(); } catch (Exception e){ System.out.println("輸入日期無效"); return; } System.out.println("請輸入月"); try { month = scanner.nextInt(); } catch (Exception e){ System.out.println("輸入日期無效"); return; } System.out.println("請輸入日"); try { day = scanner.nextInt(); } catch (Exception e){ System.out.println("輸入日期無效"); return; } if (year < 1900 || year > 2050){ System.out.println("輸入日期無效"); return; } if (month < 1 || month > 12){ System.out.println("輸入日期無效"); return; } if (day < 1 || day > 31){ System.out.println("輸入日期無效"); return; } if (year % 4 == 0 && month == 2 && day > 29){ System.out.println("輸入日期無效"); return; } if (year % 4 != 0 && month == 2 && day > 28){ System.out.println("輸入日期無效"); return; } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10){ if (day >31){ System.out.println("輸入日期無效"); return; } } if (month == 4 || month == 6 || month == 9 || month == 11){ if (day > 30) { System.out.println("輸入日期無效"); return; } } int week = which_week(year,month,day); System.out.println("星期"+week); } }

import java.util.Scanner; public class Back { public static void main(String[] args) { int N50 = 0,N10 = 0,N5 = 0,N1=0; double price,pay,value; Scanner scanner = new Scanner(System.in); System.out.println("請輸入商品價格"); try { price = scanner.nextDouble(); } catch (Exception e){ System.out.println("輸入價格無效"); return; } System.out.println("請輸入顧客支付金額"); try { pay = scanner.nextDouble(); } catch (Exception e){ System.out.println("輸入支付金額無效"); return; } if (price > pay){ System.out.println("無效,顧客付款小於商品價格"); return; } if ( price>100 || pay > 100 || pay < 0 || price < 0){ System.out.println("無效輸入"); return; } else { value = pay - price; if (value / 50 >= 1){ N50 = (int) (value / 50); value = value - 50 * N50; } if (value / 10 >= 1){ N10 = (int) (value / 10); value = value - 10 * N10; } if (value / 5 >= 1){ N5 = (int) (value / 5); value = value - 5 * N5; } N1 = (int) (value); } System.out.println("1元" + N1 + "張"); System.out.println("5元" + N5 + "張"); System.out.println("10元" + N10 + "張"); System.out.println("50元" + N50 + "張"); } }

import java.util.Scanner; public class Drinks { public static void main(String[] args) { int button1,button2; Scanner scanner = new Scanner(System.in); System.out.println("請投幣(一元請輸入1,五角請輸入2)"); try { button1 = scanner.nextInt(); if (button1 != 1 && button1 != 2){ System.out.println("輸入無效"); return; } } catch (Exception e){ System.out.println("輸入無效"); return; } System.out.println("請選擇商品(啤機請輸入1,橙汁請輸入2)"); try { button2 = scanner.nextInt(); if (button2 != 1 && button2 != 2){ System.out.println("輸入無效"); return; } } catch (Exception e){ System.out.println("輸入無效"); return; } if (button1 == 2 && button2 == 1 ){ System.out.println("請取走您的啤酒"); } else if (button1 == 2 && button2 == 2 ){ System.out.println("請取走您的橙汁"); } else if (button1 == 1 && button2 == 1 ){ System.out.println("請取走您的啤酒,將找零五角"); } else if (button1 == 1 && button2 == 2 ){ System.out.println("請取走您的橙汁,將找零五角"); } } }

import java.util.Scanner; public class Air { public static void query(int b1,int b2,int b3){ if (b1 == 1){ System.out.println("享受服務:食物供應、播放電影"); return; } if (b1 == 2 && b2 == 1){ System.out.println("享受服務:食物供應、播放電影"); return; } if (b1 == 2 && b2 == 2){ System.out.println("享受服務:食物供應"); return; } if (b1 == 3 && b2 == 1){ System.out.println("享受服務:食物供應"); return; } if (b1 == 3 && b2 == 2 && b3 == 2){ System.out.println("享受服務:食物供應"); return; } else System.out.println("享受服務:無"); } public static void main(String[] args) { int button1,button2,button3; System.out.println("歡迎使用航空服務查詢系統"); Scanner scanner = new Scanner(System.in); System.out.println("請輸入您的航線(歐美請輸入1,國外非歐美請輸入2,國內請輸入3)"); try { button1 = scanner.nextInt(); if (button1 != 1 && button1 != 2 && button1 !=3){ System.out.println("輸入無效"); return; } } catch (Exception e){ System.out.println("輸入無效"); return; } System.out.println("請輸入您的艙位(商務艙請輸入1,經濟艙請輸入2)"); try { button2 = scanner.nextInt(); if (button2 != 1 && button2 != 2){ System.out.println("輸入無效"); return; } } catch (Exception e){ System.out.println("輸入無效"); return; } System.out.println("請輸入您的飛行時間(兩小時以內請輸入1,超過兩小時請輸入2)"); try { button3 = scanner.nextInt(); if (button3 != 1 && button3 != 2){ System.out.println("輸入無效"); return; } } catch (Exception e){ System.out.println("輸入無效"); return; } query(button1,button2,button3); } }