软件测试点覆盖,边覆盖,主路径覆盖及其练习题

点覆盖:即对程序的控制流图节点进行全面覆盖。

边覆盖:设计一条路径,使程序的控制流图中所有边被覆盖。

主路径覆盖:就是对程序设计测试用例,使测试用例尽可能多的经过控制流图中的边同时不形成环。

习题:

对以下代码进行分析:

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%

时间: 2024-10-12 18:55:43

软件测试点覆盖,边覆盖,主路径覆盖及其练习题的相关文章

软件测试homework3的主路径覆盖

package test; public class test { void printPrimes(int n){ int curPrime; int numPrimes; boolean isPrime; int [] primes = new int [43]; primes[0]=2; numPrimes = 1; curPrime = 2; while ( numPrimes < n) { curPrime ++ ; isPrime = true; for ( int i = 0 ;

软件测试:主路径覆盖

1.控制流图 2.使MAXPRIMES == 4时,n=5会引发越界 3.令n= 1,不满足numPrimes<n,所以不通过while的循环 4. 点覆盖{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16} 5. 边覆盖{(1,2),(2,3),(2,12),(3,4),(4,5),(5,6),(5,9),(6,7),(6,8),(7,5), (8,9),(9,10),(9,11),(10,11),(11,2),(12,13),(13,14),(13,16),(14

软件测试(四)主路径覆盖hw3

原题中代码 /******************************************************* * Finds and prints n prime integers * Jeff Offutt, Spring 2003 ******************************************************/ public static void printPrimes (int n) { int curPrime; // Value curr

实现一个主路径覆盖的测试

1.源代码: /** * Finds and prints n prime integers * Jeff Offutt, Spring 2003 */ private static void printPrimes(int n) { int curPrime; //Value currently considered for primeness int numPrimes; // Number of primes found so far; boolean isPrime; //Is curP

软件测试 覆盖部分作业

题目代码如下: 1 /******************************************************* 2 * Finds and prints n prime integers 3 * Jeff Offutt, Spring 2003 4 ******************************************************/ 5 public static void printPrimes (int n) 6 { 7 int curPrim

软件测试学习笔记:主路径测试

(a) (b)当将MAXPRIMES设置2到5直接时.t2=(n=5)会出现越界错误而t1=(n=3)不会 (c)当n=0或1时,程序不会经过while循环. (d) 节点覆盖 TR= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16} 边覆盖 TR= {(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,5),(6,8),(8,9),(5,9),  (9,10),(9,11),(10,11),(11,12),(2,12),(12,13),

软件测试(第三次作业)——图覆盖

(a) (b) 对于测试用例t1=(n=3)和t2=(n=5),MAXPRIMES = 4时,t1不能检查出错误,而t2则会发生数组越界错. (c) 对于测试用例t3=(n=1),测试路径不经过while的循环体. (d) 节点覆盖:TR={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18} 边覆盖:TR={(1,2), (2,3), (2,4), (3,5), (4,14), (5,6), (5,7), (6,8), (7,11), (7,12), (8

软件测试图覆盖

public static void printPrimes (int n) { int curPrime; int numPrimes; boolean isPrime; int [] primes = new int [MAXPRIMES]; primes [0] = 2; numPrimes = 1; curPrime = 2; while (numPrimes < n) { curPrime++; isPrime = true; for (int i = 0; i <= numPrim

软件测试作业3:图覆盖的理解及应用

程序代码如下: 1 /******************************************************* 2 * Finds and prints n prime integers 3 * Jeff Offutt, Spring 2003 4 ******************************************************/ 5 public static void printPrimes (int n) 6 { 7 int curPrim