软件测试学习笔记:Junit入门

软件测试的第一次上机课上,第一次使用JUint对项目进行测试。

安装是最开始要进行的工作,JUint的安装是比较容易的,只需将需要的jar包引入到项目中即可

最开始的Triangle代码如下:

package com.tju.scs;

public class Triangle {
    int a, b, c;
    public Triangle(){
        a = 0;
        b = 0;
        c = 0;
    }
    public Triangle(int a, int b, int c){
        if(a > b)
        {
            int temp = a;
            a = b;
            b = temp;
        }
        if(a > c)
        {
            int temp = a;
            a = c;
            c = temp;
        }
        if(b > c)
        {
            int temp = c;
            c = b;
            b = temp;
        }
        
        this.a = a;
        this.b = b;
        this.c = c;
    }
    int test(){
        int x = 0;
        if(a + b < c || a <=0 || b <= 0 || c <= 0)
            x = 0;
        else if(a == b && b == c)
        {
            x = 1;
        }
        else if(a == b || b == c)
        {
            x = 2;
        }
        else
            x = 3;
        return x;
    }
    int legal(){
        if(this.test() != 0)
            return 1;
        else
            return 0;
    }
    int isoscelesTriangle()
    {
        if(this.test() == 1 || this.test() == 2)
            return 1;
        else
            return 0;
    }
    int equilateralTriangle()
    {
        if(this.test() == 1)
            return 1;
        else
            return 0;
    }
    int normalTriangle()
    {
        if(this.test() == 3)
            return 1;
        else
            return 0;
    }
}

Junit生成的单元测试代码如下

package com.tju.scs;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class TriangleTest {
    
    Triangle tr = null;
    @Before
    public void setUp() throws Exception {
        int a = 0;
        int b = 0;
        int c = 0;
        tr = new Triangle(a, b, c);
    }

    @Test
    public void testLegal() {
        assertEquals(0, tr.legal());
    }

    @Test
    public void testIsoscelesTriangle() {
        assertEquals(0, tr.isoscelesTriangle());
    }

    @Test
    public void testEquilateralTriangle() {
        assertEquals(0, tr.equilateralTriangle());
    }

    @Test
    public void testNormalTriangle() {
        assertEquals(0, tr.normalTriangle());
    }

}

用 a = 0, b = 0, c = 0 作为三边后,上述测试代码结果如下:

即此三角形不合法,非等腰,非等边,非普通三角形。  

覆盖率如下图:

 

posted @ 2016-03-20 10:23  xzzily  阅读(192)  评论(0编辑  收藏  举报