【软件测试作业】3

	/**
     * 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 curPrime prime?int[] primes = new int[MAXPRIMES];// The list of primes.
        
        // 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; i++ ) {
                //for each previous prime.
                if(isDvisible(primes[i],curPrime)) {
                    //Found a divisor, curPrime is not prime.
                    isPrime = false;
                    break;
                }
            }
            if(isPrime) {
                // save it!
                primes[numPrimes] = curPrime;
                numPrimes++;
            
            }
        }// End while
        
        // print all the primes out
        for(int i = 0; i < numPrimes; i++) {
            System.out.println("Prime: " + primes[i] );

        }
        
    }// End printPrimes.

(a) Draw the control flow graph for the printPrime() method.

(b) Consider test cases ti = (n = 3) and t2 = ( n = 5). Although these tour the same prime paths in printPrime(), they don't necessarily find the same faults. Design a simple fault that t2 would be more likely to discover than t1 would.

The fault is the array out of bounds. The larger n is, more likely the array out of bounds occurred.

(c) For printPrime(), 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

(d) Enumerate the test requirements for node coverage, edge coverage, and prime path coverage for the path for printPrimes().

node coverage
TR = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
edge coverage
TR = { [1,2], [2,3], [2,12], [3,4], [4,5], [5,6], [5,9], [6,7], [6,8], [7,9], [8,5], [9,10], [9,11], [10,11], [11,2], [12,13], [13,14], [13,16], [14,15], [15,13] }
prime path coverage
TR = {[1,2,3,4,5,6,8.5], [1,2,3,4,5,6,7,9,10,11,2], [1,2,3,4,5,6,7,9,11,2],[1,2,3,4,5,9,10,11,2],[1,2,3,4,5,9,11,2],[1,2,12,13,14,15,13],[1,2,12,13,16]}

(e) List test paths that achieve node coverage but not edge coverage on the graph.

[1,2,3,4,5,6,8,5,7,9,10,11,2,12,13,14,15,13,16]
no [5,9],[9,11]

(f) List test paths that achieve edge coverage but not prime path coverage on the graph for printPrimes().

{[1,2,3,4,5,6,8,5,7,9,10,11,2,12,13,14,15,13,16], [1,2,3,4,5,9,11,2,12,13,16]}

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

因为使用Idea开发,所以没有Eclemma,但是Idea自带覆盖测试工具,故使用自带的测试工具完成主路径覆盖测试

  1. 待测类Triangle
/**
 * Created by tjliqy on 2018/3/22
 */
public class Triangle {

    //TriangleType
    public static final int EQUILATERAL = 1;
    public static final int ISOSCELES = 2;
    public static final int SCALENE = 3;

    //The length of three sides of the triangle
    private double sides[] = {0,0,0};

    private int triangleType ;

    /**
     * Construction method of Triangle
     * @param a 1st side length
     * @param b 2nd side length
     * @param c 3rd side length
     */
    public Triangle(double a,double b, double c){
        this.sides[0] = a;
        this.sides[1] = b;
        this.sides[2] = c;
        for (int i = 0; i < 3; i++) {
            int sum = 0;
            int difference = 0;
            boolean getMinuend = false;
            //得出其他两边和或差
            for (int j = 0; j < 3; j++) {
                if (j != i){
                    sum += sides[j];
                    if (!getMinuend){
                        difference += sides[j];
                        getMinuend = true;
                    }else {
                        difference -= sides[j];
                        difference = Math.abs(difference);
                    }
                }
            }
            if (sum <= sides[i] || difference >= sides[i]){
                throw new NotTriangleException();
            }
        }
        initType();
    }

    /**
     * init triangleType
     */
    private void initType(){
        if (sides[0] == sides[1] && sides[1] == sides[2]){
            triangleType = EQUILATERAL;
        }else if (sides[0] == sides[1] || sides[0] == sides[2] || sides[1] ==sides[2]){
            triangleType = ISOSCELES;
        }else {
            triangleType = SCALENE;
        }
    }

    public int getTriangleType() {
        return triangleType;
    }

    /**
     * Equals when two triangles are in same type.
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof  Triangle)){
            return false;
        }
        Triangle other = (Triangle)obj;
        return  this.getTriangleType() == other.getTriangleType();
    }
}
  1. 测试类TriangleTest

import org.junit.Test;
import org.hamcrest.*;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.*;
import static org.junit.Assert.assertEquals;


/**
 * Created by tjliqy on 2018/3/22
 */
public class TriangleTest {

    @Test(expected = NotTriangleException.class)
    public void triangleErrorTest() throws NotTriangleException {
        new Triangle(0, 0, 0);
    }

    @Test
    public void equilateralTest() {
        Triangle triangle = new Triangle(1, 1, 1);
        assertEquals(Triangle.EQUILATERAL, triangle.getTriangleType());
    }

    @Test
    public void isocelesTest() {
        Triangle triangle = new Triangle(2, 2, 3);
        assertEquals(Triangle.ISOSCELES, triangle.getTriangleType());
    }

    @Test
    public void scaleneTest() {
        Triangle triangle = new Triangle(3, 4, 5);
        assertEquals(Triangle.SCALENE, triangle.getTriangleType());
    }

    /**
     * this func use hamcrest
     */
    @Test
    public void sameTypeTest() {
        Triangle t1 = new Triangle(4, 4, 4);
        Triangle t2 = new Triangle(5,5,5);
        assertThat("failure - They are not same!", t1, equalTo(t2));

    }
}

调整idea参数,让它显示测试时覆盖率

点击这个按钮开始测试
得到测试结果

可见equals方法中有一行没有被覆盖到。所以在TriangleTest类中添加如下代码

    @Test
    public void notSameTypeTest() {
        Triangle t1 = new Triangle(1,1,1);
        assertThat("not same type!",t1,equalTo(new Integer(2)));
    }

可以得到全部覆盖的结果

posted @ 2018-03-26 20:29  KiweeLi  阅读(210)  评论(0编辑  收藏  举报