软件测试三
1) 绘制程序的控制流图:
package cn.tju.scs;
public class Print {
private static int MAXPRIMES=100;
public static boolean isDivisable(int a,int b)
{
if(b%a==0)
return true;
else
return false;
}
public static void printPrimes(int n)
{
int curPrime;
int numPrimes;
boolean isPrime;
int[] primes=new int[MAXPRIMES+15];
primes[0]=2;
numPrimes=1;
curPrime=2;
while(numPrimes < n)
{
curPrime++;
isPrime=true;
for(int i=0;i<numPrimes;i++)
{
if(isDivisable(primes[i],curPrime))
{
isPrime=false;
break;
}
}
if(isPrime)
{
primes[numPrimes] = curPrime;
numPrimes++;
}
}
for(int i=0;i<numPrimes;i++)
{
System.out.println("prime: "+primes[i]);
}
}
}
(b) Consider test cases t1=(n=3) and t2=(n=5). Although these tourthe same prime paths in printPrimes(), they do not necessarily find the same faults. Design a simple fault that t2 would be more likely to discover than t1 would.
MAXPRIMES数值为4时数组越界错误
(c) For printPrimes(), find a test case such that the corresponding test path visits the edge that connects the beginning of the while statement to the for statement without going through the body of the while loop.
当n=1时,程序跳过循环
(d) Enumerate the test requirements for node coverage, edge coverage, and prime path coverage for the graph for printPrimes().
点覆盖:{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
边覆盖:{(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,5),(6,8),(8,9),(9,10),(10,11),(9,11),(11,2),(2,12),(12,13),(13,14),(14,15),(15,13),(13,16)}
主路径覆盖:{(1,2,3,4,5,6,7),(1,2,3,4,5,6,8,9,10,11),(1,2,3,4,5,6,8,9,11),(1,2,12,13,14,15),(1,2,12,13,16),(1,2,3,4,5,9,10,11),
(1,2,3,4,5,9,11),
(2,3,4,5,6,8,9,10,11,2),(2,3,4,5,6,8,9,11,2),
(2,3,4,5,9,10,11,2),(2,3,4,5,9,11,2),
(3,4,5,6,7),(3,4,5,6,8,9,10,11,2,12,13,14,15),(3,4,5,6,8,9,10,11,2,12,13,16),(3,4,5,6,8,9,11,2,12,13,14,15),
(3,4,5,6,8,9,11,2,12,13,16),(3,4,5,9,10,11,2,12,13,14,15),(3,4,5,9,10,11,2,12,13,16),(3,4,5,9,11,2,12,13,14,15),
(3,4,5,9,11,2,12,13,16),(3,4,5,6,8,9,10,11,2,3),(3,4,5,6,8,9,11,2,3),(3,4,5,9,10,11,2,3),(3,4,5,9,11,2,3),
(4,5,6,8,9,10,11,2,3,4),(4,5,6,8,9,11,2,3,4),(4,5,9,10,11,2,3,4),(4,5,9,11,2,3,4)
(5,6,7,5),(5,6,8,9,10,11,2,3,4,5),(5,6,8,9,11,2,3,4,5),(5,9,10,11,2,3,4,5),(5,9,11,2,3,4,5),
(6,7,5,9,10,11,2,12,13,14,15),(6,7,5,9,10,11,2,12,13,16),(6,7,5,9,11,2,12,13,14,15),(6,7,5,9,11,2,3,4),
(6,7,5,9,10,11,2,3,4),(6,7,5,9,11,2,3,4),
(8,9,10,11,2,3,4,5,6,8),(8,9,11,2,3,4,5,6,8),(8,9,10,11,2,3,4,5,6,7),(8,9,11,2,3,4,5,6,7),
(7,6,5,7),(7,5,6,7,9,10,11,2,12,13,14,15),(7,5,6,7,9,11,12,13,16)
(9,10,11,2,3,4,5,6,8,9),(9,11,2,3,4,5,6,8,9),(9,10,11,2,3,4,5,9),(9,11,2,3,4,5,9),
(10,11,2,3,4,5,6,8,9,10),(10,11,2,3,4,5,9,10),
(11,2,3,4,5,6,8,9,10,11),(11,2,3,4,5,6,8,9,11),(11,2,3,4,5,9,10,11),(11,2,3,4,5,9,11),
(13,14,15,13),(14,15,13,14),(14,15,13,16),(15,13,14,15)
}
2、基于Junit 及Eclemma (jacoco)实现一个主路径覆盖的测试。
package cn.tju.scs;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
public class Testprime {
public Print pt;
@Before
public void setUp()
{
pt=new Print();
}
@Test
public void testcase()
{
Print.printPrimes(10);
}
}





浙公网安备 33010602011771号