软件测试第三次作业
一.Use the following method printPrimes() for questions a–d.
(a) Draw the control ?ow graph for the printPrimes() method.

(b) Considertestcasest1=(n=3)andt2=(n=5).Although these tourthe same prime paths in printPrimes(), they do not necessarily find the same faults.Designasimplefaultthat t2 would bemorelikelytodiscover than t1 would.
答:令MAXPRIMES=4,t2会发生数组越界错误,但t1不会发生错误。
(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时,直接从while循环跳转到for循环,不经过while循环体内部。
(d) Enumerate the test requirements for node coverage, edge coverage, and prime path coverage for the graph for printPrimes().
答:节点覆盖:{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
边覆盖: { (0,1) (1,2), (1,10), (2,3), (3,4), (3,7), (4,5), (4,6), (6,3),(5,7), (7,8), (7,9), (8,9), (9,1), (10,11), (11,12), (11,14), (12,13), (13,11) }
主路径覆盖:{
[0, 1, 2, 3, 4, 5, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 7, 9]
[0, 1, 2, 3, 4, 6]
[0, 1, 2, 3, 7, 8, 9]
[0, 1, 2, 3, 7, 9]
[0, 1, 10, 11, 12, 13]
[0, 1, 10, 11, 14]
[2, 3, 4, 5, 7, 8, 9, 1, 10, 11, 14]
[2, 3, 4, 5, 7, 8, 9, 1, 10, 11, 12, 13]
[2, 3, 4, 5, 7, 9, 1, 10, 11, 14]
[2, 3, 4, 5, 7, 9, 1, 10, 11, 12, 13]
[2, 3, 7, 8, 9, 1, 10, 11, 14]
[2, 3, 7, 8, 9, 1, 10, 11, 12, 13]
[2, 3, 7, 9, 1, 10, 11, 14]
[2, 3, 7, 9, 1, 10, 11, 12, 13]
[4, 6, 3, 7, 8, 9, 1, 10, 11, 14]
[4, 6, 3, 7, 8, 9, 1, 10, 11, 12, 13]
[4, 6, 3, 7, 9, 1, 10, 11, 14]
[4, 6, 3, 7, 9, 1, 10, 11, 12, 13]
[12, 13, 11, 14]
[1, 2, 3, 4, 5, 7, 8, 9, 1]
[1, 2, 3, 4, 5, 7, 9, 1]
[1, 2, 3, 7, 8, 9, 1]
[1, 2, 3, 7, 1]
[3, 4, 6, 3]
[11, 12, 13, 11] }
二、基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试
1.homework3代码
package homework3;
public class homework3 {
private static final int MAXPRIMES = 50;
public homework3() {
}
public String printPrimes (int n) {
String prime = new String();
int curPrime;
int numPrimes;
boolean isPrime;
int [] primes = new int [MAXPRIMES];
primes [0] = 2;
numPrimes = 1;
curPrime = 2;
while (numPrimes < n) {
curPrime++;
isPrime = true;
for (int i = 0; i <= numPrimes-1; i++) {
if (curPrime%primes[i]==0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes[numPrimes] = curPrime;
numPrimes++;
}
}
for (int i = 0; i <= numPrimes-1; i++) {
prime += primes[i] + " ";
}
return prime;
}
}
2.homework3Test.java
package homework3;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class homework3Test {
private homework3 homework3;
@Before
public void setUp() throws Exception {
homework3 = new homework3();
}
@Test
public void testPrintPrimes1() {
assertEquals("2 3 5 7 ", homework3.printPrimes(4));
}
@Test
public void testPrintPrimes2() {
assertEquals("2 3 5 7 11 13 17 19 23 ", homework3.printPrimes(9));
}
}
3.运行结果





浙公网安备 33010602011771号