ST-4

1、(49-7)使用下面的方法printPrimes()完成后面的问题:

(a)为printPrimes()方法画控制流图。

ST-4ST-4ST-4

(b)考虑测试用例t1=(n=3)和t2=(n=5)。即使这些测试用例游历printPrimes()方法中的主路径,它们不一定找出相同的错误。设计一个简单的错误,使t2比t1更容易发现。

对于数组越界问题,t2比t1更容易发现

(c)针对printPrimes(),找到一个测试用例,使得相应的测试路径访问连接while语句开始到for语句的边,而不通过while循环体。

测试用例t = (n = 1)满足条件。

(d)针对printPrimes()的图列举出每个节点覆盖、边覆盖、和主路径覆盖的测试需求。

点覆盖:tr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}

边覆盖:tr2 = {(1,2), (2,3), (3,4), (4,5), (5,6), (6,4), (4,8), (5,7), (7,8), (8,9), (9,2), (8,2), (2,10), (10,11), (11,12), (12,11), (11,13)}

主路径覆盖:tr3 = {(11,12,11), (12,11,12), (4,5,6,4), (5,4,6,5),(6,4,5,6), (2,3,4,5,6,7,8,9,2), (2,3,4,8),

(1,2,3,4,5,6,7,8,9), (1,2,10,11,12), (1,2,10,11,13) }

2、基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试。

 package com.prime;

 import static org.junit.Assert.*;

 import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; public class testNumPrime {
PrintStream console = null;
ByteArrayOutputStream bytes = null;
numPrime np; @Before
public void setUp() throws Exception {
np = new numPrime();
bytes = new ByteArrayOutputStream();
console = System.out; System.setOut(new PrintStream(bytes));
} @After
public void tearDown() throws Exception {
System.setOut(console);
} @Test
public void test1() {
np.printPrimes(1);
assertEquals("2 ", bytes.toString());
}
@Test
public void test2() {
np.printPrimes(3);
assertEquals("2 3 5 ", bytes.toString());
}
@Test
public void test3() {
np.printPrimes(5);
assertEquals("2 3 5 7 11 ", bytes.toString());
}
}

测试结果为:

ST-4

实现了主路径覆盖。

上一篇:[Spark][Python]Mapping Single Rows to Multiple Pairs


下一篇:POJ 1488 Tex Quotes --- 水题