一、(49-7)使用下面的方法printPrimes()完成後面的問題:java
(a)爲printPrimes()方法畫控制流圖。數組
(b)考慮測試用例t1=(n=3)和t2=(n=5)。即便這些測試用例遊歷printPrimes()方法中的主路徑,它們不必定找出相同的錯誤。設計一個簡單的錯誤,使t2比t1更容易發現。測試
對於數組越界問題,t2比t1更容易發現spa
(c)針對printPrimes(),找到一個測試用例,使得相應的測試路徑訪問鏈接while語句開始到for語句的邊,而不經過while循環體。設計
測試用例t = (n = 1)知足條件。code
(d)針對printPrimes()的圖列舉出每一個節點覆蓋、邊覆蓋、和主路徑覆蓋的測試需求。blog
點覆蓋:tr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}it
邊覆蓋: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)}io
主路徑覆蓋: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),console
(1,2,3,4,5,6,7,8,9), (1,2,10,11,12), (1,2,10,11,13) }
二、基於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 }
測試結果爲:
實現了主路徑覆蓋。