软件测试Homework3

一.Use the following method printPrimes() for questions a–d.

(a) Draw the control flow 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时,会通过numPrimes>=直接从上图的2节点跳转到12节点,不经过while循环体内部。

(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,8),(8,5),(6,7),(7,9),(5,9),(9,10),(9,11),(10,11),(11,2),(2,12),(12,13),(13,14),(14,15),(15,13),(13,16)}。

主路径覆盖:

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

 

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

题目代码:

 1 package com.printprimes;
 2 
 3 public class Print {
 4     private static final int MAXPRIMES = 100;
 5     
 6     public static void printPrimes (int n) 
 7     { 
 8         int curPrime; // Value currently considered for primeness 
 9         int numPrimes; // Number of primes found so far. 
10         boolean isPrime; // Is curPrime prime? 
11         int [] primes = new int [MAXPRIMES]; // The list of prime numbers. 
12         
13         // Initialize 2 into the list of primes. 
14         primes [0] = 2; 
15         numPrimes = 1; 
16         curPrime = 2; 
17         while (numPrimes < n) 
18         { 
19             curPrime++; // next number to consider ... 
20             isPrime = true; 
21             for (int i = 0; i <= numPrimes-1; i++) 
22             { // for each previous prime. 
23                 if (isDivisable(primes[i],curPrime)) 
24                 { // Found a divisor, curPrime is not prime. 
25                     isPrime = false; 
26                     break; // out of loop through primes. 
27                 } 
28             } 
29             if (isPrime) 
30             { // save it! 
31                 primes[numPrimes] = curPrime; 
32                 numPrimes++; 
33             } 
34         } // End while 
35         
36         // Print all the primes out. 
37         for (int i = 0; i <= numPrimes-1; i++) 
38         { 
39             System.out.println ("Prime: " + primes[i]); 
40         } 
41     } // end printPrimes
42 
43     private static boolean isDivisable(int a, int b) {
44         if(b%a==0)
45             return true;
46         else
47             return false;
48     }
49     
50     }

 

测试代码:

 1 package com.printprimes;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.Before;
 6 import org.junit.Test;
 7 
 8 public class PrintTest {
 9 
10     private Print pptest;
11 
12     @Before
13     public void setUp() throws Exception {
14         pptest = new Print();
15     }
16 
17     @Test
18     public void testPrintPrimes() {
19         pptest.printPrimes(5);     
20     }
21 
22 }

测试结果:

posted @ 2018-03-18 00:17  Alyssa_young  阅读(168)  评论(0编辑  收藏  举报