实验三 白盒测试
實驗目的
?
(1)?學習白盒測試方法
(2)?掌握語句覆蓋、條件覆蓋、分支覆蓋等邏輯覆蓋方法
(3)?掌握Java代碼分析工具的使用
?
實驗內容
1、?計算整數X和整數Y的最大公約數。(不允許采用課堂上所用的方式實現)
l?請用類和方法(寫一個求最大公約數的方法)實現,命名時請按照規范命名。
l?在main方式中獲取用戶輸入的兩個整數,調用之前寫的方法,輸出它們的最大公約數。
l?利用FindBugs查找程序中是否存在bug。
import?java.util.Scanner;
public?class?Common{
//求最大公約數
public?static?int?commonDivisor(int?n,int?m){
//輾轉相除是用大的除以小的。如果n<m,第一次相當n與m值交換
while(n%m!=0){
int?temp=n%m;
n=m;
m=temp;
}
return?m;
}
?
public?static?void?main(String[] args){
Scanner sc=new?Scanner(System.in);
System.out.println("請輸入第一個數:");
int?n=sc.nextInt();
System.out.println("請輸入第二個數:");
int?m=sc.nextInt();
?
System.out.println("最大公約數為:"+commonDivisor(n,m));
}
}
?
弄錯后
?
2、?邏輯覆蓋的應用
l?按照所給的程序流程圖,寫出Java代碼(用類和方法實現)
l?寫出語句覆蓋、分支覆蓋的測試用例,以及它所覆蓋的路徑,用JUnit編寫測試用例進行測試
?
import?java.util.Scanner;
public?class?Test1 {
?
/**
?* @param?args
?*/
?
public??void?test() {
?
Scanner sc=new?Scanner(System.in);
System.out.println("請輸入x的值:");
int??x=sc.nextInt();
System.out.println("請輸入y的值:");
?int?y=sc.nextInt();
if(x<4 || y>0){
if(y>1){
y=y+x;
}
else{}
?
}
else{
if(x>=5){
x=x-y;
}
else{
x=x+y;
}
}
System.out.println("x="+x);
System.out.println("y="+y);
}
?
}
?
?
測試類
?
import static org.junit.Assert.*;
?
import org.junit.Test;
?
?
public class Test1Test {
?
?Test1 test1 =new Test1();
@Test
public void test() {
?
??test1.test();
???
}
?
?
}
?
?
?
?
?
語句覆蓋:
X=4,y=0 ?路徑:aeg
X=5,y=0 ?路徑:aef
X=4,y=2 ?路徑:abc
分支覆蓋:
X=4,y=0 ?路徑:aeg
X=5,y=0 ?路徑:aef
X=4,y=2 ?路徑:abc
X=3,y=1 ?路徑:abd
?
轉載于:https://www.cnblogs.com/bushi2b/p/5398648.html
總結
- 上一篇: android配置文件说明
- 下一篇: 20150110--魔术方法魔术常量+面