書中曾用懸崖形容軟件邊界:如果在懸崖峭壁邊可以自信而安全地行走而不掉下去,平地就幾乎不在話下了。邊界條件是特殊情況,因為編程在根本上說在邊界上容易產生問題。實踐表明,故障往往出現在定義域或值域的邊界上。
1.邊界值分析法的概念
邊界值分析法就是對輸入的邊界值進行測試的一種黑盒測試方法,通常邊界值分析法是作為對等價類划分方法的補充,這種情況下,其測試用例來自等價類的邊界。
#include <cstring> #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> using namespace std; int a[10]; int main(){ memset(a, -1, sizeof(a)); for( int i = 1; i <= 10; i ++) a[i] = i; return 0; }
上述代碼是很常見的數組越界問題,數組的范圍為0~9,而上述賦值語句卻是在1~10范圍內操作,因此,在上述代碼實現中a[0]=-1,而非我們一開始希望的a[0]=0。這都不是我們希望得到的結果。
2.邊界值分析法選擇測試用例原則
1) 如果輸入條件規定了值的范圍,則應取剛達到這個范圍的邊界值、以及剛超越這個范圍邊界的值作為測試輸入數據
2) 如果輸入條件規定了值的個數,則選取最大個數、最小個數、比最大個數多一、比最小個數少一的數作為測試數據
3) 根據規格說明的每個輸出條件,使用規則1)
4) 根據規格說明的每個輸出條件,使用規則2)
5) 若輸入域是有序集合,則選取集合的第一個元素和最后一個元素作為測試用例
6) 如果程序使用了一個內部數據結構,則應當選擇內部數據結構上得邊界值作為測試用例
7) 分析規格說明,找出其他可能的邊界條件
3.使用邊界分析法設計測試用例
1)首先確定邊界情況數據
2)選取正好等於,剛剛大於或剛剛小於邊界的值作為測試,而不是選取等價類中的典型值或任意值。
4.小實例
- 問題描述:NextData函數包含三個變量:month,day,year,函數的輸出為輸入日期的后一天。
- 要求輸入變量month,day,year均為整數值,並且滿足下列條件:
- 1≤month≤12
- 1≤day≤31
- 1812≤year≤2012
- 等價類划分法:http://www.cnblogs.com/tju-crab/p/4354643.html在這篇博客中應用等價類划分法在這個實例上了
- 測試用例設計:
用例編號 | 輸入 | 預期輸出 | ||
year | month | day | ||
1 | 1811 | 2 | 1 | null |
2 | 2013 | 2 | 1 | null |
3 | 1812 | 0 | 1 | null |
4 | 1812 | 13 | 1 | null |
5 | 1812 | 2 | 0 | null |
6 | 1812 | 2 | 29 | 1812/3/1 |
7 | 1812 | 2 | 30 | null |
8 | 1812 | 3 | 0 | null |
9 | 1812 | 3 | 31 | 1812/4/1 |
10 | 1812 | 3 | 32 | null |
11 | 1812 | 4 | 0 | null |
12 | 1812 | 4 | 30 | 1812/5/1 |
13 | 1812 | 4 | 31 | null |
14 | 1813 | 2 | 28 | 1813/3/1 |
15 | 1813 | 2 | 29 | null |
16 | 1812 | 12 | 31 | 1813/1/1 |
17 | 2012 | 12 | 31 | 2013/1/1 |
- 代碼實現:
bool isLeap( int year){ if( year % 4 != 0 ) return false; else if( year % 100 != 0 ) return true; else if( year % 400 == 0 ) return true; else return false; } void NextDate( int month, int day, int year){ if( year >= 1812 && year <= 2012){ if(month >= 1 && month <= 12){ if(month == 2){ if(isLeap(year)){ if(day >= 1 && day < 29){ cout << year << "年" << month << "月" << day + 1 << "日" << endl; } else if(day == 29){ cout << year << "年" << 3 << "月" << 1 << "日" << endl; } } else{ if(day >= 1 && day < 28){ cout << year << "年" << month << "月" << day + 1 << "日" << endl; } else if(day == 28){ cout << year << "年" << 3 << "月" << 1 << "日" << endl; } } } else if(month == 2 || month == 4 || month == 6 || month == 9 || month == 11 ){ if(day >= 1 && day < 30){ cout << year << "年" << month << "月" << day + 1 << "日" << endl; } else if(day == 30){ cout << year << "年" << month + 1 << "月" << 1 << "日" << endl; } } else{ if(day >= 1 && day < 31){ cout << year << "年" << month << "月" << day + 1 << "日" << endl; } else if(day == 31){ if(month == 12){ cout << year + 1 << "年" << 1 << "月" << 1 << "日" << endl; } else{ cout << year << "年" << month + 1 << "月" << 1 << "日" << endl; } } }