【軟體測試】邊界值分析法

來源:互聯網
上載者:User

標籤:

  書中曾用懸崖形容軟體邊界:如果在懸崖峭壁邊可以自信而安全地行走而不掉下去,平地就幾乎不在話下了。邊界條件是特殊情況,因為編程在根本上說在邊界上容易產生問題。實踐表明,故障往往出現在定義域或範圍的邊界上。

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;                    }                }            }

 

 

  

 

【軟體測試】邊界值分析法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.