点覆盖:即对程序的控制流图节点进行全面覆盖。
边覆盖:设计一条路径,使程序的控制流图中所有边被覆盖。
主路径覆盖:就是对程序设计测试用例,使测试用例尽可能多的经过控制流图中的边同时不形成环。
习题:
对以下代码进行分析:
package com.prime;
public class Prime {
public static Boolean isDivisable(int prime, int curPrime) {
if (curPrime % prime == 0) {
return true;
} else
return false;
}
public static void printPrimes(int n) {
int curPrime; // Value currently considered for primeness
int numPrimes; // Number of primes found so far.
boolean isPrime; // Is curPrime prime?
int[] primes = new int[100]; // The list of prime numbers.
// Initialize 2 into the list of primes.
primes[0] = 2;
numPrimes = 1;
curPrime = 2;
while (numPrimes < n) {
curPrime++; // next number to consider ...
isPrime = true;
for (int i = 0; i <= numPrimes - 1; i++) { // for each previous
// prime.
if (isDivisable(primes[i], curPrime)) { // Found a divisor,
// curPrime is not
// prime.
isPrime = false;
break; // out of loop through primes.
}
}
if (isPrime) { // save it!
primes[numPrimes] = curPrime;
numPrimes++;
}
} // End while
// Print all the primes out.
for (int i = 0; i <= numPrimes - 1; i++) {
System.out.println("Prime: " + primes[i]);
}
} // end printPrimes
}
问题a:画出程序的控制流图
问题b:设计一个错误,使测试用例t2(n=5)比t1(n=3)更容易发现的错误。
将程序中n换成4,这样测试用例n=3不能发现这个错误而n=5能发现。
问题c:测试用例 t(n=1)
问题d:
点覆盖:[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,7),(7,5),(6,8),(8,9),(5,9),(9,10),(10,11),(11,2),(2,12),(12,13),(13,14),(14,15),(15,13),(13,16)]
主路径覆盖:
[1,2,3,4,5,6,7]
[1,2,3,4,5,9,10,11]
[1,2,3,4,5,9,11]
[1,2,3,4,5,6,8,9,10,11]
[1,2,3,4,5,6,8,9,11]
[1,2,12,13,14,15]
[1,2,12,13,16]
[2,3,4,5,6,8,9,10,11,2]
[2,3,4,5,6,8,9,11,2]
[2,3,4,5,9,10,11,2]
[2,3,4,5,9,11,2]
[3,4,5,6,8,9,10,11,2,12,13,14,15]
[3,4,5,6,8,9,10,11,2,12,13,16]
[3,4,5,6,8,9,11,2,12,13,14,15]
[3,4,5,6,8,9,11,2,12,13,16]
[3,4,5,9,10,11,2,12,13,14,15]
[3,4,5,9,11,2,12,13,14,15]
[3,4,5,9,10,11,2,12,13,16]
[3,4,5,9,11,2,12,13,16]
[5,6,7,5]
[6,7,5,9,10,11,2,12,13,14,15]
[6,7,5,9,11,2,12,13,14,15]
[6,7,5,9,10,11,2,12,13,16]
[6,7,5,9,11,2,12,13,16]
[13,14,15,13]
[14,15,13,16]
设计测试代码如下:
package Primay;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
//import org.junit.internal.runners.TestClassRunner;
import org.junit.runner.RunWith;
import Primay.Primaies;
public class PrimaiesTest {
public static Primaies primay = new Primaies();
@Before
public void Start() throws Exception{
primay.Start();
}
@After
public void End() throws Exception{
}
@Test
public void TestNormal(){
String str=new String("Prime: 2\r\nPrime: 3\r\nPrime: 5\r\nPrime: 7\r\nPrime: 11\r\n");
primay.printPrimes(5);
assertEquals(str,primay.Getstr());
}
}
设计时将源代码输出到控制台改变为输出到Primaies类中一个私有成员变量,String类型,每次测试对此变量初始化。
然后测试比较成员变量中与预测结果是否一致。
具体代码上传至git中TestJunit项目中:https://github.com/klkjjhjkhjhg/junit
效果图如下:
程序代码率:
由于本项目中含有其他文件,所以代码率不是100%