ST-4

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

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

    

(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)实现一个主路径覆盖的测试。

 1 package com.prime;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import java.io.ByteArrayOutputStream;
 6 import java.io.PrintStream;
 7 
 8 import org.junit.After;
 9 import org.junit.AfterClass;
10 import org.junit.Before;
11 import org.junit.BeforeClass;
12 import org.junit.Test;
13 
14 public class testNumPrime {
15     PrintStream console = null;
16     ByteArrayOutputStream bytes = null;
17     numPrime np;
18 
19     @Before
20     public void setUp() throws Exception {
21         np = new numPrime();
22         bytes = new ByteArrayOutputStream();
23         console = System.out;
24 
25         System.setOut(new PrintStream(bytes));
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30         System.setOut(console);
31     }
32 
33     @Test
34     public void test1() {
35         np.printPrimes(1);
36         assertEquals("2 ", bytes.toString());
37     }
38     @Test
39     public void test2() {
40         np.printPrimes(3);
41         assertEquals("2 3 5 ", bytes.toString());
42     }
43     @Test
44     public void test3() {
45         np.printPrimes(5);
46         assertEquals("2 3 5 7 11 ", bytes.toString());
47     }
50 }

测试结果为:

实现了主路径覆盖。

 

posted on 2016-03-28 13:14  moonLY  阅读(313)  评论(0编辑  收藏  举报