軟件測試 | 單元測試 | 用JUnit實現 5種覆蓋方式

先解釋一下Assert

本質上,是一個封裝好的 if 語句.返回值爲void.傳入形參爲 (預期值,待測值,允許誤差範圍)

調用時,打印錯誤信息,比如

對於float類型的形參而言,Δ是必須的.否則調用一個廢棄的assertEqual().


一.源碼

1.需求:

佣金函數commision(int,int,int);

需求是: commission方法是用來計算銷售佣金的需求

有耳機(headphone)、手機殼(Mobile phone shell)、手機貼膜(Cellphone screen protector)三個部件,

每個部件單價爲:耳機80元,手機殼10元,手機貼膜8元,每月月末向製造商報告銷量,製造商根據銷量給銷售商佣金。

如果銷售額不足1000元按10%提取佣金,1000-1800元部分按15%提取佣金,超過1800元部分按20%提取佣金。

 

2.源代碼

package com.company;

//佣金函數
public class Commision {
    float total;    //記錄總銷售額
    float saraly;   //記錄要發的佣金(返回值)

    public float commosion(int headphone, int shell, int protector) {
        if (headphone<0||shell<0||protector<0){
            return (float)-1;
        }

        total = 80 * headphone + 10 * shell + 8 * protector;
        if (total <= 1000) {
            saraly = (float)0.1 * total;
        } else if (total <= 1800 && total > 1000) {
            saraly = 100 + (float)0.15 * (total - 1000);
        } else {
            saraly = 100*(float)0.1+800*(float)0.15+(total-1800)*(float)0.2;
        }
        return saraly;
    }
}

 

 

二.流程圖

 

三.測試用例的編寫

1.

1.語句覆蓋

路徑

要求

測試用例

預期結果

SABDE

total<=1000

(1,1,1)

9.8

SABCFE

1000<total<=1800

(10,30,5)

121.0

SABCGE

total>1800

(100,150,100)

1830.0

測試代碼:

@Test   //語句覆蓋
public void Commosion_state() throws Exception {
   
//期望值,實際值,允許誤差.該斷言接收double形參時必加delta,否則調用一個被棄用的斷言函數
    assertEquals(9.8, cc.commosion(1, 1, 1), 0.01);
    assertEquals(
121.0, cc.commosion(10, 30, 5), 0.01);
    assertEquals(
1830, cc.commosion(100, 150, 100), 0.01);
}

 

 

 

2.路徑覆蓋

路徑

要求

測試用例

預期結果

SABDE

total<=1000

(1,1,1)

9.8

SABCFE

1000<total<=1800

(10,30,5)

121.0

SABCGE

total>1800

(100,150,100)

1830.0

測試代碼:

@Test   //路徑覆蓋

public void Commosion_path() {

    assertEquals(9.8, cc.commosion(1, 1, 1), 0.01);

    assertEquals(121.0, cc.commosion(10, 30, 5), 0.01);

    assertEquals(1830, cc.commosion(100, 150, 100), 0.01);

}

 

 

 

3.判定覆蓋

路徑

要求

測試用例

預期結果

SABCGE

total>1800

(100,150,100)

1830.0


 

測試代碼:

@Test   //判定覆蓋

public void Commosion_dec() {

    assertEquals(1830, cc.commosion(100, 150, 100), 0.01);

}

 

 

 

4.條件覆蓋

三個條件B1,C1,C2

 

 

條件

路徑

要求

測試用例

預期結果

 

B1=T

SABDE

total<=1000

(1,1,1)

9.8

 

B1=F C1=T,C2=F

SABCGE

Total>1000&&

total>1800

(200,200,200)

3690

 

B1=F C1=F,C2=T

SABCGE

Total<1000&&

total>1800

不存在

---

測試代碼:

@Test   //條件覆蓋

public void Commosion_con() {

    assertEquals(9.8, cc.commosion(1, 1, 1), 0.01);

    assertEquals(3690.0, cc.commosion(200, 200, 200), 0.01);

}

 

 

 

5.判定/條件覆蓋

兩個判定B,C.三個條件B1,C1,C2

B=T   B1=T

C=F         C1=T C2=F

判定

條件

路徑

要求

測試用例

預期結果

B=T

B1=T

SABDE

total<=1000

(1,1,1)

9.8

B=F,C=T

B1=F,C1=F,C2=T

----

Total<=1000&&

Total>1800

---

---

B=F,C=F

B1=F,C1=T,C2=F

SABCGE

total>1800

(100,150,100)

1830.0


 

 

 

 

@Test   //判定/條件覆蓋 public void Commosion_dec_con() {    

assertEquals(9.8, cc.commosion(1, 1, 1), 0.01);    

assertEquals(3690.0, cc.commosion(200, 200, 200), 0.01);

}

6.條件組合覆蓋

判定B可能出現的條件組合爲:B=T,B=F

判定C可能出現的條件組合爲:

C1=T,C2=F

C1=F,C2=T

C1=T,C2=T

C1=F,C2=F

設計測試用例

 

條件

路徑

要求

測試用例

預期結果  

 

B=T

SABDE

total<=1000

(1,1,1)

9.8

 

B=F C1=F,C2=T

不存在

total<=1000&&

total>1800

------

------

 

B=F C1=T,C2=F

SABCGE

total>1800

(100,150,100)

1830.0

 

B=F C1=T,C2=T

SABCFE

1000<total<=1800

(10,30,5)

121.0

 

B=F C1=F,C2=F

不存在

Total<=1000&&

Total>1800

------

------

 


 

測試代碼:

@Test   //條件組合覆蓋
public void ommosion_mul_con() {
    assertEquals(
9.8, cc.commosion(1, 1, 1), 0.01);
    assertEquals(
121.0, cc.commosion(10, 30, 5), 0.01);
    assertEquals(
1830.0, cc.commosion(100, 150, 100), 0.01);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章